Beispiel #1
0
    def test_live_reload(self) -> None:
        # Force the reload by making the last update date < the file's last
        # modified date
        openapi_spec.last_update = 0
        get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)

        # Check that the file has been reloaded by verifying that the last
        # update date isn't zero anymore
        self.assertNotEqual(openapi_spec.last_update, 0)

        # Now verify calling it again doesn't call reload
        with mock.patch('zerver.openapi.openapi.openapi_spec.reload') as mock_reload:
            get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)
            self.assertFalse(mock_reload.called)
Beispiel #2
0
    def test_live_reload(self) -> None:
        # Force the reload by making the last update date < the file's last
        # modified date
        openapi_spec.mtime = 0
        get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)

        # Check that the file has been reloaded by verifying that the last
        # update date isn't zero anymore
        self.assertNotEqual(openapi_spec.mtime, 0)

        # Now verify calling it again doesn't call reload
        old_openapi = openapi_spec.openapi()
        get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)
        new_openapi = openapi_spec.openapi()
        self.assertIs(old_openapi, new_openapi)
Beispiel #3
0
 def test_get_openapi_fixture(self) -> None:
     actual = get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD, TEST_RESPONSE_BAD_REQ)
     expected = {
         "code": "BAD_REQUEST",
         "msg": "You don't have permission to edit this message",
         "result": "error",
     }
     self.assertEqual(actual, expected)
Beispiel #4
0
 def test_get_openapi_fixture(self) -> None:
     actual = get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD,
                                  TEST_RESPONSE_BAD_REQ)
     expected = {
         'code': 'BAD_REQUEST',
         'msg': 'You don\'t have permission to edit this message',
         'result': 'error',
     }
     self.assertEqual(actual, expected)
Beispiel #5
0
    def render_fixture(self, function: str, name: Optional[str] = None) -> List[str]:
        fixture = []

        path, method = function.rsplit(":", 1)
        fixture_dict = get_openapi_fixture(path, method, name)
        fixture_json = json.dumps(fixture_dict, indent=4, sort_keys=True, separators=(",", ": "))

        fixture.append("``` json")
        fixture.extend(fixture_json.splitlines())
        fixture.append("```")

        return fixture
    def render_fixture(self, function: str, name: Optional[str]=None) -> List[str]:
        fixture = []

        # We assume that if the function we're rendering starts with a slash
        # it's a path in the endpoint and therefore it uses the new OpenAPI
        # format.
        if function.startswith('/'):
            path, method = function.rsplit(':', 1)
            fixture_dict = get_openapi_fixture(path, method, name)
        else:
            fixture_dict = zerver.openapi.python_examples.FIXTURES[function]

        fixture_json = json.dumps(fixture_dict, indent=4, sort_keys=True,
                                  separators=(',', ': '))

        fixture.append('``` json')
        fixture.extend(fixture_json.splitlines())
        fixture.append('```')

        return fixture