diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/__init__.py | 1 | ||||
| -rw-r--r-- | tests/api/__init__.py | 17 | ||||
| -rw-r--r-- | tests/api/test_eval.py | 49 | 
3 files changed, 66 insertions, 1 deletions
| diff --git a/tests/__init__.py b/tests/__init__.py index 792d600..e69de29 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +0,0 @@ -# diff --git a/tests/api/__init__.py b/tests/api/__init__.py new file mode 100644 index 0000000..fd4679a --- /dev/null +++ b/tests/api/__init__.py @@ -0,0 +1,17 @@ +from unittest import mock + +from falcon import testing + +from snekbox.api import SnekAPI + + +class SnekAPITestCase(testing.TestCase): +    def setUp(self): +        super().setUp() + +        self.patcher = mock.patch("snekbox.api.resources.eval.NsJail", autospec=True) +        self.mock_nsjail = self.patcher.start() +        self.mock_nsjail.return_value.python3.return_value = "test output" +        self.addCleanup(self.patcher.stop) + +        self.app = SnekAPI() diff --git a/tests/api/test_eval.py b/tests/api/test_eval.py new file mode 100644 index 0000000..a5b83fd --- /dev/null +++ b/tests/api/test_eval.py @@ -0,0 +1,49 @@ +from tests.api import SnekAPITestCase + + +class TestEvalResource(SnekAPITestCase): +    PATH = "/eval" + +    def test_post_valid_200(self): +        body = {"input": "foo"} +        result = self.simulate_post(self.PATH, json=body) + +        self.assertEqual(result.status_code, 200) +        self.assertEqual(body["input"], result.json["input"]) +        self.assertEqual("test output", result.json["output"]) + +    def test_post_invalid_schema_400(self): +        body = {"stuff": "foo"} +        result = self.simulate_post(self.PATH, json=body) + +        self.assertEqual(result.status_code, 400) + +        expected = { +            "title": "Request data failed validation", +            "description": "'input' is a required property" +        } + +        self.assertEqual(expected, result.json) + +    def test_post_invalid_content_type_415(self): +        body = "{\"input\": \"foo\"}" +        headers = {"Content-Type": "application/xml"} +        result = self.simulate_post(self.PATH, body=body, headers=headers) + +        self.assertEqual(result.status_code, 415) + +        expected = { +            "title": "Unsupported media type", +            "description": "application/xml is an unsupported media type." +        } + +        self.assertEqual(expected, result.json) + +    def test_disallowed_method_405(self): +        result = self.simulate_get(self.PATH) +        self.assertEqual(result.status_code, 405) + +    def test_options_allow_post_only(self): +        result = self.simulate_options(self.PATH) +        self.assertEqual(result.status_code, 200) +        self.assertEqual(result.headers.get("Allow"), "POST") | 
