Skip to content

Commit 418c35b

Browse files
committed
Added a test to ensure that the hostname is correct and that the authentication token is sent.
1 parent 3587295 commit 418c35b

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

test/test_api_client.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import json
2+
import time
3+
import unittest
4+
import urllib
5+
6+
from testcontainers.core.container import DockerContainer
7+
8+
from zitadel_client import ApiClient, Configuration
9+
from zitadel_client.auth.personal_access_token_authenticator import PersonalAccessTokenAuthenticator
10+
11+
12+
class TestApiClient(unittest.TestCase):
13+
"""
14+
Test case for interacting with the WireMock mock OAuth2 server.
15+
"""
16+
17+
@classmethod
18+
def setUpClass(cls):
19+
"""
20+
Starts the WireMock Docker container and exposes the required port.
21+
Sets up the OAuth server URL.
22+
"""
23+
cls.mock_oauth2_server = DockerContainer("wiremock/wiremock") \
24+
.with_exposed_ports(8080)
25+
cls.mock_oauth2_server.start()
26+
27+
host = cls.mock_oauth2_server.get_container_host_ip()
28+
port = cls.mock_oauth2_server.get_exposed_port(8080)
29+
cls.oauth_host = f"http://{host}:{port}"
30+
31+
@classmethod
32+
def tearDownClass(cls):
33+
"""
34+
Stops the WireMock Docker container.
35+
"""
36+
if cls.mock_oauth2_server is not None:
37+
cls.mock_oauth2_server.stop()
38+
39+
def test_assert_headers_and_content_type(self):
40+
"""
41+
Test the behavior of API client when sending requests to the mock OAuth2 server,
42+
asserting headers and content type.
43+
"""
44+
time.sleep(20)
45+
46+
with urllib.request.urlopen(
47+
urllib.request.Request(
48+
self.oauth_host + "/__admin/mappings",
49+
data=json.dumps({
50+
"request": {
51+
"method": "GET",
52+
"url": "/your/endpoint",
53+
"headers": {
54+
"Authorization": {
55+
"equalTo": "Bearer mm"
56+
}
57+
}
58+
},
59+
"response": {
60+
"status": 200,
61+
"body": "{\"key\": \"value\"}",
62+
"headers": {
63+
"Content-Type": "application/json"
64+
}
65+
}
66+
}).encode('utf-8'),
67+
headers={'Content-Type': 'application/json'},
68+
method='POST'
69+
)
70+
) as response:
71+
response.read().decode()
72+
73+
api_client = ApiClient(
74+
Configuration(authenticator=PersonalAccessTokenAuthenticator(self.oauth_host, "mm"))
75+
)
76+
77+
api_response = api_client.call_api(*(api_client.param_serialize(
78+
method='GET',
79+
resource_path='/your/endpoint',
80+
)))
81+
82+
if api_response.status != 200:
83+
self.fail(f"Expected status 200, but got {api_response.status}")
84+
85+
86+
if __name__ == '__main__':
87+
unittest.main()

zitadel_client/api_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ def param_serialize(
166166
if body:
167167
body = self.sanitize_for_serialization(body)
168168

169-
url = _host + resource_path
169+
if _host is None:
170+
url = self.configuration.host + resource_path
171+
else:
172+
url = _host + resource_path
170173

171174
if query_params:
172175
query_params = self.sanitize_for_serialization(query_params)

0 commit comments

Comments
 (0)