11import os
2+ import unittest
23from unittest import (
34 mock ,
45)
1011 AioHTTPTestCase ,
1112 unittest_run_loop ,
1213)
14+ from werkzeug .exceptions import (
15+ abort ,
16+ )
1317
1418from minos .api_gateway .rest import (
1519 ApiGatewayConfig ,
@@ -131,7 +135,7 @@ async def get_application(self):
131135 return await rest_service .create_application ()
132136
133137 @unittest_run_loop
134- async def test_auth_unreachable (self ):
138+ async def test_auth_disabled (self ):
135139 url = "/order"
136140 headers = {"Authorization" : "Bearer test_token" }
137141 response = await self .client .request ("POST" , url , headers = headers )
@@ -140,6 +144,59 @@ async def test_auth_unreachable(self):
140144 self .assertIn ("Microservice call correct!!!" , await response .text ())
141145
142146
147+ class TestAuthFailed (AioHTTPTestCase ):
148+ CONFIG_FILE_PATH = BASE_PATH / "config.yml"
149+
150+ @mock .patch .dict (os .environ , {"API_GATEWAY_REST_CORS_ENABLED" : "true" })
151+ def setUp (self ) -> None :
152+ self .config = ApiGatewayConfig (self .CONFIG_FILE_PATH )
153+
154+ self .discovery = MockServer (host = self .config .discovery .host , port = self .config .discovery .port ,)
155+ self .discovery .add_json_response (
156+ "/microservices" , {"address" : "localhost" , "port" : "5568" , "status" : True },
157+ )
158+
159+ self .microservice = MockServer (host = "localhost" , port = 5568 )
160+ self .microservice .add_json_response (
161+ "/order/5" , "Microservice call correct!!!" , methods = ("GET" , "PUT" , "PATCH" , "DELETE" ,)
162+ )
163+ self .microservice .add_json_response ("/order" , "Microservice call correct!!!" , methods = ("POST" ,))
164+
165+ self .authentication_service = MockServer (host = "localhost" , port = 8082 )
166+
167+ self .authentication_service .add_callback_response ("/token" , lambda : abort (400 ), methods = ("POST" ,))
168+
169+ self .discovery .start ()
170+ self .microservice .start ()
171+ self .authentication_service .start ()
172+ super ().setUp ()
173+
174+ def tearDown (self ) -> None :
175+ self .discovery .shutdown_server ()
176+ self .microservice .shutdown_server ()
177+ self .authentication_service .shutdown_server ()
178+ super ().tearDown ()
179+
180+ async def get_application (self ):
181+ """
182+ Override the get_app method to return your application.
183+ """
184+ rest_service = ApiGatewayRestService (
185+ address = self .config .rest .host , port = self .config .rest .port , config = self .config
186+ )
187+
188+ return await rest_service .create_application ()
189+
190+ @unittest_run_loop
191+ async def test_auth_unauthorized (self ):
192+ url = "/order"
193+ headers = {"Authorization" : "Bearer test_token" }
194+ response = await self .client .request ("POST" , url , headers = headers )
195+
196+ self .assertEqual (401 , response .status )
197+ self .assertIn ("The given request does not have authorization to be forwarded" , await response .text ())
198+
199+
143200class TestAuthUnreachable (AioHTTPTestCase ):
144201 CONFIG_FILE_PATH = BASE_PATH / "config.yml"
145202
@@ -182,5 +239,9 @@ async def test_auth_unreachable(self):
182239 headers = {"Authorization" : "Bearer test_token" }
183240 response = await self .client .request ("POST" , url , headers = headers )
184241
185- self .assertEqual (200 , response .status )
186- self .assertIn ("Microservice call correct!!!" , await response .text ())
242+ self .assertEqual (504 , response .status )
243+ self .assertIn ("The Authentication Service is not available" , await response .text ())
244+
245+
246+ if __name__ == "__main__" :
247+ unittest .main ()
0 commit comments