Skip to content

Commit 8e659c1

Browse files
committed
- Added User to Request.
issue #40
1 parent 8ec87b8 commit 8e659c1

File tree

5 files changed

+193
-106
lines changed

5 files changed

+193
-106
lines changed

minos/api_gateway/rest/handler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ async def call(address: str, port: int, original_req: web.Request, **kwargs) ->
6969
:return: The web response to be retrieved to the client.
7070
"""
7171

72-
headers = original_req.headers
72+
headers = original_req.headers.copy()
73+
if "Authorization" in headers and "Bearer" in headers["Authorization"]:
74+
parts = headers["Authorization"].split()
75+
if len(parts) == 2:
76+
headers["User"] = parts[1]
77+
else:
78+
headers["User"] = "None"
79+
else:
80+
headers["User"] = "None"
81+
7382
url = original_req.url.with_scheme("http").with_host(address).with_port(port)
7483
method = original_req.method
7584
content = await original_req.text()

poetry.lock

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
from unittest import (
3+
mock,
4+
)
5+
6+
from aiohttp.test_utils import (
7+
AioHTTPTestCase,
8+
unittest_run_loop,
9+
)
10+
11+
from minos.api_gateway.common import (
12+
MinosConfig,
13+
)
14+
from minos.api_gateway.rest import (
15+
ApiGatewayRestService,
16+
)
17+
from tests.mock_servers.server import (
18+
MockServer,
19+
)
20+
from tests.utils import (
21+
BASE_PATH,
22+
)
23+
24+
25+
class TestApiGatewayCORS(AioHTTPTestCase):
26+
CONFIG_FILE_PATH = BASE_PATH / "config.yml"
27+
28+
@mock.patch.dict(os.environ, {"API_GATEWAY_CORS_ENABLED": "true"})
29+
def setUp(self) -> None:
30+
self.config = MinosConfig(self.CONFIG_FILE_PATH)
31+
32+
self.discovery = MockServer(
33+
host=self.config.discovery.connection.host, port=self.config.discovery.connection.port,
34+
)
35+
self.discovery.add_json_response(
36+
"/microservices", {"address": "localhost", "port": "5568", "status": True},
37+
)
38+
39+
self.microservice = MockServer(host="localhost", port=5568)
40+
self.microservice.add_json_response(
41+
"/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",)
42+
)
43+
self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",))
44+
45+
self.discovery.start()
46+
self.microservice.start()
47+
super().setUp()
48+
49+
def tearDown(self) -> None:
50+
self.discovery.shutdown_server()
51+
self.microservice.shutdown_server()
52+
super().tearDown()
53+
54+
async def get_application(self):
55+
"""
56+
Override the get_app method to return your application.
57+
"""
58+
rest_service = ApiGatewayRestService(
59+
address=self.config.rest.connection.host, port=self.config.rest.connection.port, config=self.config
60+
)
61+
62+
return await rest_service.create_application()
63+
64+
@unittest_run_loop
65+
async def test_auth_headers(self):
66+
url = "/order"
67+
headers = {"Authorization": "Bearer test_token"}
68+
response = await self.client.request("POST", url, headers=headers)
69+
70+
self.assertEqual(200, response.status)
71+
self.assertIn("Microservice call correct!!!", await response.text())
72+
73+
@unittest_run_loop
74+
async def test_wrong_auth_headers(self):
75+
url = "/order"
76+
headers = {"Authorization": "Bearer"}
77+
response = await self.client.request("POST", url, headers=headers)
78+
79+
self.assertEqual(200, response.status)
80+
self.assertIn("Microservice call correct!!!", await response.text())
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import os
2+
from unittest import (
3+
mock,
4+
)
5+
6+
import attr
7+
from aiohttp.test_utils import (
8+
AioHTTPTestCase,
9+
unittest_run_loop,
10+
)
11+
from aiohttp_middlewares.cors import (
12+
ACCESS_CONTROL_ALLOW_HEADERS,
13+
ACCESS_CONTROL_ALLOW_METHODS,
14+
ACCESS_CONTROL_ALLOW_ORIGIN,
15+
DEFAULT_ALLOW_HEADERS,
16+
DEFAULT_ALLOW_METHODS,
17+
)
18+
19+
from minos.api_gateway.common import (
20+
MinosConfig,
21+
)
22+
from minos.api_gateway.rest import (
23+
ApiGatewayRestService,
24+
)
25+
from tests.mock_servers.server import (
26+
MockServer,
27+
)
28+
from tests.utils import (
29+
BASE_PATH,
30+
)
31+
32+
33+
class TestApiGatewayCORS(AioHTTPTestCase):
34+
CONFIG_FILE_PATH = BASE_PATH / "config.yml"
35+
TEST_DENIED_ORIGIN = "https://www.google.com"
36+
TEST_ORIGIN = "http://localhost:3000"
37+
38+
@mock.patch.dict(os.environ, {"API_GATEWAY_CORS_ENABLED": "true"})
39+
def setUp(self) -> None:
40+
self.config = MinosConfig(self.CONFIG_FILE_PATH)
41+
42+
self.discovery = MockServer(
43+
host=self.config.discovery.connection.host, port=self.config.discovery.connection.port,
44+
)
45+
self.discovery.add_json_response(
46+
"/microservices", {"address": "localhost", "port": "5568", "status": True},
47+
)
48+
49+
self.microservice = MockServer(host="localhost", port=5568)
50+
self.microservice.add_json_response(
51+
"/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",)
52+
)
53+
self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",))
54+
55+
self.discovery.start()
56+
self.microservice.start()
57+
super().setUp()
58+
59+
def tearDown(self) -> None:
60+
self.discovery.shutdown_server()
61+
self.microservice.shutdown_server()
62+
super().tearDown()
63+
64+
async def get_application(self):
65+
"""
66+
Override the get_app method to return your application.
67+
"""
68+
rest_service = ApiGatewayRestService(
69+
address=self.config.rest.connection.host, port=self.config.rest.connection.port, config=self.config
70+
)
71+
72+
return await rest_service.create_application()
73+
74+
@staticmethod
75+
def check_allow_origin(
76+
response, origin, *, allow_headers=DEFAULT_ALLOW_HEADERS, allow_methods=DEFAULT_ALLOW_METHODS,
77+
):
78+
assert response.headers[ACCESS_CONTROL_ALLOW_ORIGIN] == origin
79+
if allow_headers:
80+
assert response.headers[ACCESS_CONTROL_ALLOW_HEADERS] == ", ".join(allow_headers)
81+
if allow_methods:
82+
assert response.headers[ACCESS_CONTROL_ALLOW_METHODS] == ", ".join(allow_methods)
83+
84+
@unittest_run_loop
85+
async def test_cors_enabled(self):
86+
method = "GET"
87+
extra_headers = {}
88+
expected_origin = "*"
89+
expected_allow_headers = None
90+
expected_allow_methods = None
91+
url = "/order/5?verb=GET&path=12324"
92+
93+
kwargs = {}
94+
if expected_allow_headers is not attr.NOTHING:
95+
kwargs["allow_headers"] = expected_allow_headers
96+
if expected_allow_methods is not attr.NOTHING:
97+
kwargs["allow_methods"] = expected_allow_methods
98+
99+
self.check_allow_origin(
100+
await self.client.request(method, url, headers={"Origin": self.TEST_ORIGIN, **extra_headers}),
101+
expected_origin,
102+
**kwargs,
103+
)

tests/test_api_gateway/test_rest/test_service.py

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
"""tests.test_api_gateway.test_rest.service module."""
22

3-
import os
43
import unittest
5-
from unittest import (
6-
mock,
7-
)
84

9-
import attr
105
from aiohttp.test_utils import (
116
AioHTTPTestCase,
127
unittest_run_loop,
138
)
14-
from aiohttp_middlewares.cors import (
15-
ACCESS_CONTROL_ALLOW_HEADERS,
16-
ACCESS_CONTROL_ALLOW_METHODS,
17-
ACCESS_CONTROL_ALLOW_ORIGIN,
18-
DEFAULT_ALLOW_HEADERS,
19-
DEFAULT_ALLOW_METHODS,
20-
)
219

2210
from minos.api_gateway.common import (
2311
MinosConfig,
@@ -212,78 +200,5 @@ async def test_get(self):
212200
self.assertIn("The requested endpoint is not available.", await response.text())
213201

214202

215-
class TestApiGatewayCORS(AioHTTPTestCase):
216-
CONFIG_FILE_PATH = BASE_PATH / "config.yml"
217-
TEST_DENIED_ORIGIN = "https://www.google.com"
218-
TEST_ORIGIN = "http://localhost:3000"
219-
220-
@mock.patch.dict(os.environ, {"API_GATEWAY_CORS_ENABLED": "true"})
221-
def setUp(self) -> None:
222-
self.config = MinosConfig(self.CONFIG_FILE_PATH)
223-
224-
self.discovery = MockServer(
225-
host=self.config.discovery.connection.host, port=self.config.discovery.connection.port,
226-
)
227-
self.discovery.add_json_response(
228-
"/microservices", {"address": "localhost", "port": "5568", "status": True},
229-
)
230-
231-
self.microservice = MockServer(host="localhost", port=5568)
232-
self.microservice.add_json_response(
233-
"/order/5", "Microservice call correct!!!", methods=("GET", "PUT", "PATCH", "DELETE",)
234-
)
235-
self.microservice.add_json_response("/order", "Microservice call correct!!!", methods=("POST",))
236-
237-
self.discovery.start()
238-
self.microservice.start()
239-
super().setUp()
240-
241-
def tearDown(self) -> None:
242-
self.discovery.shutdown_server()
243-
self.microservice.shutdown_server()
244-
super().tearDown()
245-
246-
async def get_application(self):
247-
"""
248-
Override the get_app method to return your application.
249-
"""
250-
rest_service = ApiGatewayRestService(
251-
address=self.config.rest.connection.host, port=self.config.rest.connection.port, config=self.config
252-
)
253-
254-
return await rest_service.create_application()
255-
256-
@staticmethod
257-
def check_allow_origin(
258-
response, origin, *, allow_headers=DEFAULT_ALLOW_HEADERS, allow_methods=DEFAULT_ALLOW_METHODS,
259-
):
260-
assert response.headers[ACCESS_CONTROL_ALLOW_ORIGIN] == origin
261-
if allow_headers:
262-
assert response.headers[ACCESS_CONTROL_ALLOW_HEADERS] == ", ".join(allow_headers)
263-
if allow_methods:
264-
assert response.headers[ACCESS_CONTROL_ALLOW_METHODS] == ", ".join(allow_methods)
265-
266-
@unittest_run_loop
267-
async def test_cors_enabled(self):
268-
method = "GET"
269-
extra_headers = {}
270-
expected_origin = "*"
271-
expected_allow_headers = None
272-
expected_allow_methods = None
273-
url = "/order/5?verb=GET&path=12324"
274-
275-
kwargs = {}
276-
if expected_allow_headers is not attr.NOTHING:
277-
kwargs["allow_headers"] = expected_allow_headers
278-
if expected_allow_methods is not attr.NOTHING:
279-
kwargs["allow_methods"] = expected_allow_methods
280-
281-
self.check_allow_origin(
282-
await self.client.request(method, url, headers={"Origin": self.TEST_ORIGIN, **extra_headers}),
283-
expected_origin,
284-
**kwargs,
285-
)
286-
287-
288203
if __name__ == "__main__":
289204
unittest.main()

0 commit comments

Comments
 (0)