def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.add_json_response( "/microservices", { "address": "localhost", "port": "5568", "status": True }, ) self.microservice = MockServer(host="localhost", port=5568) self.microservice.add_json_response("/order/5", "Microservice call correct!!!", methods=( "GET", "PUT", "PATCH", "DELETE", )) self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST", )) self.discovery.start() self.microservice.start() super().setUp()
def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.start() super().setUp()
def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.add_callback_response("/microservices", lambda: abort(400), methods=("GET", )) self.discovery.start() super().setUp()
def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.add_json_response( "/microservices", { "address": "localhost", "port": "5568", "status": True }, ) self.discovery.start() super().setUp()
class TestApiGatewayRestServiceNotFoundDiscovery(AioHTTPTestCase): CONFIG_FILE_PATH = BASE_PATH / "config.yml" def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.start() super().setUp() def tearDown(self) -> None: self.discovery.shutdown_server() super().tearDown() async def get_application(self): """ Override the get_app method to return your application. """ rest_service = ApiGatewayRestService(address=self.config.rest.host, port=self.config.rest.port, config=self.config) return await rest_service.create_application() @unittest_run_loop async def test_get(self): url = "/order/5?verb=GET&path=12324" response = await self.client.request("GET", url) self.assertEqual(404, response.status) self.assertIn("The '/order/5' path is not available for 'GET' method.", await response.text())
class TestApiGatewayRestServiceUnreachableMicroservice(AioHTTPTestCase): CONFIG_FILE_PATH = BASE_PATH / "config.yml" def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.add_json_response( "/microservices", { "address": "localhost", "port": "5568", "status": True }, ) self.discovery.start() super().setUp() def tearDown(self) -> None: self.discovery.shutdown_server() super().tearDown() async def get_application(self): """ Override the get_app method to return your application. """ rest_service = ApiGatewayRestService(address=self.config.rest.host, port=self.config.rest.port, config=self.config) return await rest_service.create_application() @unittest_run_loop async def test_get(self): url = "/order/5?verb=GET&path=12324" response = await self.client.request("GET", url) self.assertEqual(503, response.status) self.assertIn("The requested endpoint is not available.", await response.text())
class TestApiGatewayRestServiceFailedDiscovery(AioHTTPTestCase): CONFIG_FILE_PATH = BASE_PATH / "config.yml" def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.add_callback_response("/microservices", lambda: abort(400), methods=("GET", )) self.discovery.start() super().setUp() def tearDown(self) -> None: self.discovery.shutdown_server() super().tearDown() async def get_application(self): """ Override the get_app method to return your application. """ rest_service = ApiGatewayRestService(address=self.config.rest.host, port=self.config.rest.port, config=self.config) return await rest_service.create_application() @unittest_run_loop async def test_get(self): url = "/order/5?verb=GET&path=12324" response = await self.client.request("GET", url) self.assertEqual(502, response.status) self.assertIn("The Discovery Service response is wrong.", await response.text())
class TestApiGatewayRestService(AioHTTPTestCase): CONFIG_FILE_PATH = BASE_PATH / "config.yml" def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer( host=self.config.discovery.host, port=self.config.discovery.port, ) self.discovery.add_json_response( "/microservices", { "address": "localhost", "port": "5568", "status": True }, ) self.microservice = MockServer(host="localhost", port=5568) self.microservice.add_json_response("/order/5", "Microservice call correct!!!", methods=( "GET", "PUT", "PATCH", "DELETE", )) self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST", )) self.discovery.start() self.microservice.start() super().setUp() def tearDown(self) -> None: self.discovery.shutdown_server() self.microservice.shutdown_server() super().tearDown() async def get_application(self): """ Override the get_app method to return your application. """ rest_service = ApiGatewayRestService(address=self.config.rest.host, port=self.config.rest.port, config=self.config) return await rest_service.create_application() @unittest_run_loop async def test_get(self): url = "/order/5?verb=GET&path=12324" response = await self.client.request("GET", url) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text()) @unittest_run_loop async def test_post(self): url = "/order" response = await self.client.request("POST", url) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text()) @unittest_run_loop async def test_put(self): url = "/order/5" response = await self.client.request("PUT", url) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text()) @unittest_run_loop async def test_patch(self): url = "/order/5" response = await self.client.request("PATCH", url) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text()) @unittest_run_loop async def test_delete(self): url = "/order/5" response = await self.client.request("DELETE", url) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text())
class TestApiGatewayAuthentication(AioHTTPTestCase): CONFIG_FILE_PATH = BASE_PATH / "config.yml" @mock.patch.dict(os.environ, {"API_GATEWAY_REST_CORS_ENABLED": "true"}) def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,) self.discovery.add_json_response( "/microservices", {"address": "localhost", "port": "5568", "status": True}, ) self.microservice = MockServer(host="localhost", port=5568) self.microservice.add_json_response( "/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",) ) self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",)) self.authentication_service = MockServer(host="localhost", port=8082) self.authentication_service.add_json_response("/token", {"sub": uuid4()}, methods=("POST",)) self.discovery.start() self.microservice.start() self.authentication_service.start() super().setUp() def tearDown(self) -> None: self.discovery.shutdown_server() self.microservice.shutdown_server() self.authentication_service.shutdown_server() super().tearDown() async def get_application(self): """ Override the get_app method to return your application. """ rest_service = ApiGatewayRestService( address=self.config.rest.host, port=self.config.rest.port, config=self.config ) return await rest_service.create_application() @unittest_run_loop async def test_auth_headers(self): url = "/order" headers = {"Authorization": "Bearer test_token"} response = await self.client.request("POST", url, headers=headers) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text()) @unittest_run_loop async def test_wrong_auth_headers(self): url = "/order" headers = {"Authorization": "Bearer"} # Missing token response = await self.client.request("POST", url, headers=headers) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text()) @unittest_run_loop async def test_request_has_token(self): url = "/order" headers = {"Authorization": "Bearer"} # Missing token response = await self.client.request("POST", url, headers=headers) self.assertEqual(200, response.status) self.assertIn("Microservice call correct!!!", await response.text())
class TestAuthUnreachable(AioHTTPTestCase): CONFIG_FILE_PATH = BASE_PATH / "config.yml" def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,) self.discovery.add_json_response( "/microservices", {"address": "localhost", "port": "5568", "status": True}, ) self.microservice = MockServer(host="localhost", port=5568) self.microservice.add_json_response( "/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",) ) self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",)) self.discovery.start() self.microservice.start() super().setUp() def tearDown(self) -> None: self.discovery.shutdown_server() self.microservice.shutdown_server() super().tearDown() async def get_application(self): """ Override the get_app method to return your application. """ rest_service = ApiGatewayRestService( address=self.config.rest.host, port=self.config.rest.port, config=self.config ) return await rest_service.create_application() @unittest_run_loop async def test_auth_unreachable(self): url = "/order" headers = {"Authorization": "Bearer test_token"} response = await self.client.request("POST", url, headers=headers) self.assertEqual(504, response.status) self.assertIn("The Authentication Service is not available", await response.text())
class TestApiGatewayCORS(AioHTTPTestCase): CONFIG_FILE_PATH = BASE_PATH / "config.yml" TEST_DENIED_ORIGIN = "https://www.google.com" TEST_ORIGIN = "http://*****:*****@mock.patch.dict(os.environ, {"API_GATEWAY_REST_CORS_ENABLED": "true"}) def setUp(self) -> None: self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH) self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,) self.discovery.add_json_response( "/microservices", {"address": "localhost", "port": "5568", "status": True}, ) self.microservice = MockServer(host="localhost", port=5568) self.microservice.add_json_response( "/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",) ) self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",)) self.discovery.start() self.microservice.start() super().setUp() def tearDown(self) -> None: self.discovery.shutdown_server() self.microservice.shutdown_server() super().tearDown() async def get_application(self): """ Override the get_app method to return your application. """ rest_service = ApiGatewayRestService( address=self.config.rest.host, port=self.config.rest.port, config=self.config ) return await rest_service.create_application() @staticmethod def check_allow_origin( response, origin, *, allow_headers=DEFAULT_ALLOW_HEADERS, allow_methods=DEFAULT_ALLOW_METHODS, ): assert response.headers[ACCESS_CONTROL_ALLOW_ORIGIN] == origin if allow_headers: assert response.headers[ACCESS_CONTROL_ALLOW_HEADERS] == ", ".join(allow_headers) if allow_methods: assert response.headers[ACCESS_CONTROL_ALLOW_METHODS] == ", ".join(allow_methods) @unittest_run_loop async def test_cors_enabled(self): method = "GET" extra_headers = {} expected_origin = "*" expected_allow_headers = None expected_allow_methods = None url = "/order/5?verb=GET&path=12324" kwargs = {} if expected_allow_headers is not attr.NOTHING: kwargs["allow_headers"] = expected_allow_headers if expected_allow_methods is not attr.NOTHING: kwargs["allow_methods"] = expected_allow_methods self.check_allow_origin( await self.client.request(method, url, headers={"Origin": self.TEST_ORIGIN, **extra_headers}), expected_origin, **kwargs, )