|
| 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() |
0 commit comments