def testGenerateApi_Operation(self): self.targetview.__doc__ = 'DocDescription' self.app.add_url_rule("/test/<float:id>", 'test_function', api_view(self.targetview), methods=['GET']) def generateEndpointDoc(): from flask_jigger.livedoc import swagger return swagger.SwaggerDoc().generate_resource_description( ['test_function']) self.swaggerview.side_effect = generateEndpointDoc with self.app.test_request_context( '/swagger', headers={'Accept': 'application/json'}): response = self.app.full_dispatch_request() swaggerdescription = json.loads(response.data) operation = swaggerdescription['apis'][0]['operations'][0] self.assertEqual(operation['httpMethod'], 'GET') self.assertEqual(operation['notes'], "DocDescription") self.assertEqual(operation['summary'], 'Test It') parameter = operation['parameters'][0] self.assertEqual(parameter['dataType'], 'float') self.assertEqual(parameter['name'], 'id') self.assertEqual(parameter['paramType'], 'path')
def setUp(self): self.targetview = mock.MagicMock() self.targetview.__name__ = 'test_it' self.targetview.return_value = {'testkey':'testvalue'} self.swaggerview = mock.MagicMock() self.app = flask.Flask(__name__) self.app.add_url_rule("/swagger", 'swagger', api_view(self.swaggerview))
def setUp(self): self.targetview = mock.MagicMock() self.targetview.__name__ = 'test_it' self.targetview.return_value = {'testkey': 'testvalue'} self.swaggerview = mock.MagicMock() self.app = flask.Flask(__name__) self.app.add_url_rule("/swagger", 'swagger', api_view(self.swaggerview))
def testGenerateApi(self): self.app.add_url_rule("/test/<float:id>", 'test_function', api_view(self.targetview),methods=['GET']) def generateEndpointDoc(): from flask_jigger.livedoc import swagger return swagger.SwaggerDoc().generate_resource_description(['test_function']) self.swaggerview.side_effect = generateEndpointDoc with self.app.test_request_context('/swagger', headers={'Accept':'application/json'}): response = self.app.full_dispatch_request() swaggerdescription = json.loads(response.data) self.assertEqual(swaggerdescription['resourcePath'],'/swagger') self.assertEqual(swaggerdescription['apis'][0]['path'], '/test/{id}')
def test_api_view(self): targetview = mock.MagicMock() targetview.return_value = {'testkey':'testvalue'} targetview.__name__ = "TestName" renderer = mock.MagicMock() renderer.render = mock.MagicMock() renderer.render.return_value = 'ALL_OK' renderer.media_type = 'application/testmediatype' app = flask.Flask(__name__) app.add_url_rule("/test/<id>", None, api_view(targetview,[renderer])) with app.test_request_context('/test/123', headers={'Accept':'application/testmediatype'}): response = app.full_dispatch_request() self.assertEqual(response.data, 'ALL_OK') targetview.assert_called_once_with(id='123')
def test_api_view(self): targetview = mock.MagicMock() targetview.return_value = {'testkey': 'testvalue'} targetview.__name__ = "TestName" renderer = mock.MagicMock() renderer.render = mock.MagicMock() renderer.render.return_value = 'ALL_OK' renderer.media_type = 'application/testmediatype' app = flask.Flask(__name__) app.add_url_rule("/test/<id>", None, api_view(targetview, [renderer])) with app.test_request_context( '/test/123', headers={'Accept': 'application/testmediatype'}): response = app.full_dispatch_request() self.assertEqual(response.data, 'ALL_OK') targetview.assert_called_once_with(id='123')
def test_format_suffix_url_rules_calls_original_endpoint_without_format_suffix_urls(self): with mock.MagicMock() as targetview: def check_request_effect(*args,**kwargs): self.assertEqual(flask.request.endpoint, 'test_function') self.assertEqual(flask.request.url, 'http://localhost/test') targetview.side_effect = check_request_effect app = flask.Flask(__name__) app.add_url_rule("/test", 'test_function', api_view(targetview)) format_suffix_url_rules(app) with app.test_request_context('/test.json'): app.full_dispatch_request() targetview.assert_called_once_with()
def test_format_suffix_url_rules(self): with mock.MagicMock() as targetview: def check_request_effect(*args,**kwargs): #Should only accept json, nothing else self.assertTrue(flask.request.accept_mimetypes.accept_json) self.assertFalse(flask.request.accept_mimetypes.accept_html) targetview.side_effect = check_request_effect app = flask.Flask(__name__) app.add_url_rule("/test", 'test_function', api_view(targetview)) format_suffix_url_rules(app) with app.test_request_context('/test.json', headers={'Accept':'application/noexist'}): app.full_dispatch_request() targetview.assert_called_once_with()
def test_format_suffix_url_rules_calls_original_endpoint_without_format_suffix_urls( self): with mock.MagicMock() as targetview: def check_request_effect(*args, **kwargs): self.assertEqual(flask.request.endpoint, 'test_function') self.assertEqual(flask.request.url, 'http://localhost/test') targetview.side_effect = check_request_effect app = flask.Flask(__name__) app.add_url_rule("/test", 'test_function', api_view(targetview)) format_suffix_url_rules(app) with app.test_request_context('/test.json'): app.full_dispatch_request() targetview.assert_called_once_with()
def testGenerateApi(self): self.app.add_url_rule("/test/<float:id>", 'test_function', api_view(self.targetview), methods=['GET']) def generateEndpointDoc(): from flask_jigger.livedoc import swagger return swagger.SwaggerDoc().generate_resource_description( ['test_function']) self.swaggerview.side_effect = generateEndpointDoc with self.app.test_request_context( '/swagger', headers={'Accept': 'application/json'}): response = self.app.full_dispatch_request() swaggerdescription = json.loads(response.data) self.assertEqual(swaggerdescription['resourcePath'], '/swagger') self.assertEqual(swaggerdescription['apis'][0]['path'], '/test/{id}')
def test_format_suffix_url_rules(self): with mock.MagicMock() as targetview: def check_request_effect(*args, **kwargs): #Should only accept json, nothing else self.assertTrue(flask.request.accept_mimetypes.accept_json) self.assertFalse(flask.request.accept_mimetypes.accept_html) targetview.side_effect = check_request_effect app = flask.Flask(__name__) app.add_url_rule("/test", 'test_function', api_view(targetview)) format_suffix_url_rules(app) with app.test_request_context( '/test.json', headers={'Accept': 'application/noexist'}): app.full_dispatch_request() targetview.assert_called_once_with()
def testGenerateApi_Operation(self): self.targetview.__doc__ = 'DocDescription' self.app.add_url_rule("/test/<float:id>", 'test_function', api_view(self.targetview),methods=['GET']) def generateEndpointDoc(): from flask_jigger.livedoc import swagger return swagger.SwaggerDoc().generate_resource_description(['test_function']) self.swaggerview.side_effect = generateEndpointDoc with self.app.test_request_context('/swagger', headers={'Accept':'application/json'}): response = self.app.full_dispatch_request() swaggerdescription = json.loads(response.data) operation = swaggerdescription['apis'][0]['operations'][0] self.assertEqual(operation['httpMethod'], 'GET') self.assertEqual(operation['notes'], "DocDescription") self.assertEqual(operation['summary'],'Test It') parameter = operation['parameters'][0] self.assertEqual(parameter['dataType'], 'float') self.assertEqual(parameter['name'], 'id') self.assertEqual(parameter['paramType'], 'path')