Skip to content

Commit d0ecc7a

Browse files
ISSUE #92
1 parent bd9a6c1 commit d0ecc7a

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import json
2+
import os
3+
import unittest
4+
from unittest import (
5+
mock,
6+
)
7+
from uuid import (
8+
uuid4,
9+
)
10+
11+
from aiohttp.test_utils import (
12+
AioHTTPTestCase,
13+
unittest_run_loop,
14+
)
15+
from werkzeug.exceptions import (
16+
abort,
17+
)
18+
19+
from minos.api_gateway.rest import (
20+
ApiGatewayConfig,
21+
ApiGatewayRestService,
22+
)
23+
from tests.mock_servers.server import (
24+
MockServer,
25+
)
26+
from tests.utils import (
27+
BASE_PATH,
28+
)
29+
30+
31+
class TestApiGatewayAuthorization(AioHTTPTestCase):
32+
CONFIG_FILE_PATH = BASE_PATH / "config.yml"
33+
34+
@mock.patch.dict(os.environ, {"API_GATEWAY_REST_CORS_ENABLED": "true"})
35+
def setUp(self) -> None:
36+
self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH)
37+
38+
self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,)
39+
self.discovery.add_json_response(
40+
"/microservices", {"address": "localhost", "port": "5568", "status": True},
41+
)
42+
43+
self.microservice = MockServer(host="localhost", port=5568)
44+
self.microservice.add_json_response(
45+
"/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",)
46+
)
47+
self.microservice.add_json_response(
48+
"/merchants/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",)
49+
)
50+
self.microservice.add_json_response("/categories/5", "Microservice call correct!!!", methods=("GET",))
51+
self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",))
52+
53+
self.authentication_service = MockServer(host=self.config.rest.auth.host, port=self.config.rest.auth.port)
54+
55+
self.authentication_service.add_json_response("/auth/credentials", {"uuid": uuid4()}, methods=("POST",))
56+
self.authentication_service.add_json_response(
57+
"/auth/credentials/login", {"token": "credential-token-test"}, methods=("POST",)
58+
)
59+
self.authentication_service.add_json_response("/auth/credentials", {"uuid": uuid4()}, methods=("GET",))
60+
self.authentication_service.add_json_response(
61+
"/auth/token", {"uuid": uuid4(), "token": "token-test"}, methods=("POST",)
62+
)
63+
self.authentication_service.add_json_response("/auth/token/login", {"token": "token-test"}, methods=("POST",))
64+
self.authentication_service.add_json_response("/auth/token", {"uuid": uuid4()}, methods=("GET",))
65+
66+
self.authentication_service.add_json_response(
67+
"/auth/validate-token", {"uuid": uuid4(), "role": 3}, methods=("POST",)
68+
)
69+
70+
self.authentication_service.add_json_response("/auth", {"uuid": uuid4()}, methods=("POST", "GET",))
71+
72+
self.discovery.start()
73+
self.microservice.start()
74+
self.authentication_service.start()
75+
super().setUp()
76+
77+
def tearDown(self) -> None:
78+
self.discovery.shutdown_server()
79+
self.microservice.shutdown_server()
80+
self.authentication_service.shutdown_server()
81+
super().tearDown()
82+
83+
async def get_application(self):
84+
"""
85+
Override the get_app method to return your application.
86+
"""
87+
rest_service = ApiGatewayRestService(
88+
address=self.config.rest.host, port=self.config.rest.port, config=self.config
89+
)
90+
91+
return await rest_service.create_application()
92+
93+
@unittest_run_loop
94+
async def test_auth_unauthorized(self):
95+
await self.client.post(
96+
"/admin/rules",
97+
data=json.dumps({"service": "merchants", "rule": "*://*/merchants/*", "methods": ["GET", "POST"]}),
98+
)
99+
await self.client.post(
100+
"/admin/autz-rules",
101+
data=json.dumps(
102+
{"service": "merchants", "roles": ["2"], "rule": "*://*/merchants/*", "methods": ["GET", "POST"]}
103+
),
104+
)
105+
url = "/merchants/5"
106+
headers = {"Authorization": "Bearer credential-token-test"}
107+
108+
response = await self.client.request("POST", url, headers=headers)
109+
110+
self.assertEqual(401, response.status)
111+
self.assertIn("401: Unauthorized", await response.text())
112+
113+
114+
class TestAutzFailed(AioHTTPTestCase):
115+
CONFIG_FILE_PATH = BASE_PATH / "config.yml"
116+
117+
@mock.patch.dict(os.environ, {"API_GATEWAY_REST_CORS_ENABLED": "true"})
118+
def setUp(self) -> None:
119+
self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH)
120+
121+
self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,)
122+
self.discovery.add_json_response(
123+
"/microservices", {"address": "localhost", "port": "5568", "status": True},
124+
)
125+
126+
self.microservice = MockServer(host="localhost", port=5568)
127+
self.microservice.add_json_response(
128+
"/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",)
129+
)
130+
self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",))
131+
132+
self.authentication_service = MockServer(host=self.config.rest.auth.host, port=self.config.rest.auth.port)
133+
self.authentication_service.add_json_response("/auth/validate-token", lambda: abort(400), methods=("POST",))
134+
135+
self.discovery.start()
136+
self.microservice.start()
137+
self.authentication_service.start()
138+
super().setUp()
139+
140+
def tearDown(self) -> None:
141+
self.discovery.shutdown_server()
142+
self.microservice.shutdown_server()
143+
self.authentication_service.shutdown_server()
144+
super().tearDown()
145+
146+
async def get_application(self):
147+
"""
148+
Override the get_app method to return your application.
149+
"""
150+
rest_service = ApiGatewayRestService(
151+
address=self.config.rest.host, port=self.config.rest.port, config=self.config
152+
)
153+
154+
return await rest_service.create_application()
155+
156+
@unittest_run_loop
157+
async def test_auth_unauthorized(self):
158+
await self.client.post(
159+
"/admin/autz-rules",
160+
data=json.dumps(
161+
{"service": "merchants", "roles": ["Customer"], "rule": "*://*/merchants/*", "methods": ["GET", "POST"]}
162+
),
163+
)
164+
url = "/merchants/jksdksdjskd"
165+
headers = {"Authorization": "Bearer credential-token-test_01"}
166+
167+
response = await self.client.request("POST", url, headers=headers)
168+
169+
self.assertEqual(401, response.status)
170+
self.assertIn("The given request does not have authorization to be forwarded", await response.text())
171+
172+
173+
if __name__ == "__main__":
174+
unittest.main()

0 commit comments

Comments
 (0)