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