Skip to content

Commit 654e8ea

Browse files
committed
Added some tests to ensure that the auth works
1 parent c8ef007 commit 654e8ea

File tree

296 files changed

+479
-16512
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

296 files changed

+479
-16512
lines changed

.openapi-generator-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ poetry.lock
1717
requirements.txt
1818
test-requirements.txt
1919
tox.ini
20+
test/*.py

poetry.lock

Lines changed: 341 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ tox = ">= 3.9.0"
3636
flake8 = ">= 4.0.0"
3737
types-python-dateutil = ">= 2.8.19.14"
3838
mypy = ">= 1.5"
39+
testcontainers = "3.7.1"
3940

4041
[build-system]
4142
requires = ["poetry-core>=1.0.0"]

test/auth/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import time
2+
from datetime import datetime, timezone
3+
4+
from test.auth.test_oauth_authenticator import OAuthAuthenticatorTest
5+
from zitadel_client.auth.client_credentials_authenticator import ClientCredentialsAuthenticator
6+
7+
8+
class ClientCredentialsAuthenticatorTest(OAuthAuthenticatorTest):
9+
"""
10+
Test for ClientCredentialsAuthenticator to verify token refresh functionality.
11+
Extends the base OAuthAuthenticatorTest class.
12+
"""
13+
14+
def test_refresh_token(self):
15+
time.sleep(20)
16+
17+
authenticator = ClientCredentialsAuthenticator.builder(self.oauth_host, "dummy-client", "dummy-secret") \
18+
.scopes({"openid", "foo"}) \
19+
.build()
20+
21+
self.assertTrue(authenticator.get_auth_token(), "Access token should not be empty")
22+
token = authenticator.refresh_token()
23+
self.assertEqual({"Authorization": "Bearer " + token.access_token}, authenticator.get_auth_headers())
24+
self.assertTrue(token.access_token, "Access token should not be null")
25+
self.assertTrue(token.expires_at > datetime.now(timezone.utc), "Token expiry should be in the future")
26+
self.assertEqual(token.access_token, authenticator.get_auth_token())
27+
self.assertEqual(self.oauth_host, authenticator.get_host())
28+
self.assertNotEqual(authenticator.refresh_token().access_token, authenticator.refresh_token().access_token,
29+
"Two refreshToken calls should produce different tokens")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
3+
from zitadel_client.auth.no_auth_authenticator import NoAuthAuthenticator
4+
5+
6+
class NoAuthAuthenticatorTest(unittest.TestCase):
7+
def test_returns_empty_headers_and_default_host(self):
8+
auth = NoAuthAuthenticator()
9+
self.assertEqual({}, auth.get_auth_headers())
10+
self.assertEqual("http://localhost", auth.get_host())
11+
12+
def test_returns_empty_headers_and_custom_host(self):
13+
auth = NoAuthAuthenticator("https://custom-host")
14+
self.assertEqual({}, auth.get_auth_headers())
15+
self.assertEqual("https://custom-host", auth.get_host())
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
3+
from testcontainers.core.container import DockerContainer
4+
5+
6+
class OAuthAuthenticatorTest(unittest.TestCase):
7+
"""
8+
Base test class for OAuth authenticators.
9+
10+
This class starts a Docker container running the mock OAuth2 server
11+
(ghcr.io/navikt/mock-oauth2-server:2.1.10) before any tests run and stops it after all tests.
12+
It sets the class variable `oauth_host` to the container’s accessible URL.
13+
14+
The container is configured to wait for an HTTP response from the "/" endpoint
15+
with a status code of 405, using HttpWaitStrategy.
16+
"""
17+
oauth_host: str = None
18+
mock_oauth2_server: DockerContainer = None
19+
20+
@classmethod
21+
def setUpClass(cls):
22+
cls.mock_oauth2_server = DockerContainer("ghcr.io/navikt/mock-oauth2-server:2.1.10") \
23+
.with_exposed_ports(8080)
24+
cls.mock_oauth2_server.start()
25+
host = cls.mock_oauth2_server.get_container_host_ip()
26+
port = cls.mock_oauth2_server.get_exposed_port(8080)
27+
cls.oauth_host = f"http://{host}:{port}"
28+
29+
@classmethod
30+
def tearDownClass(cls):
31+
if cls.mock_oauth2_server is not None:
32+
cls.mock_oauth2_server.stop()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
3+
from zitadel_client.auth.personal_access_token_authenticator import PersonalAccessTokenAuthenticator
4+
5+
6+
class PersonalAccessTokenAuthenticatorTest(unittest.TestCase):
7+
def test_returns_expected_headers_and_host(self):
8+
auth = PersonalAccessTokenAuthenticator("https://api.example.com", "my-secret-token")
9+
self.assertEqual({"Authorization": "Bearer my-secret-token"}, auth.get_auth_headers())
10+
self.assertEqual("https://api.example.com", auth.get_host())
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import time
2+
from datetime import datetime, timezone
3+
4+
from cryptography.hazmat.primitives.asymmetric import rsa
5+
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption
6+
7+
from test.auth.test_oauth_authenticator import OAuthAuthenticatorTest
8+
from zitadel_client.auth.web_token_authenticator import WebTokenAuthenticator
9+
10+
11+
class WebTokenAuthenticatorTest(OAuthAuthenticatorTest):
12+
"""
13+
Test for WebTokenAuthenticator to verify JWT token refresh functionality using the builder.
14+
"""
15+
16+
def test_refresh_token_using_builder(self):
17+
time.sleep(20)
18+
19+
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
20+
private_key_pem = key.private_bytes(
21+
encoding=Encoding.PEM,
22+
format=PrivateFormat.PKCS8,
23+
encryption_algorithm=NoEncryption()
24+
).decode('utf-8')
25+
26+
authenticator = WebTokenAuthenticator.builder(self.oauth_host, "dummy-client", private_key_pem) \
27+
.token_lifetime_seconds(3600) \
28+
.build()
29+
30+
self.assertTrue(authenticator.get_auth_token(), "Access token should not be empty")
31+
token = authenticator.refresh_token()
32+
self.assertEqual({"Authorization": "Bearer " + token.access_token}, authenticator.get_auth_headers())
33+
self.assertTrue(token.access_token, "Access token should not be null")
34+
self.assertTrue(token.expires_at > datetime.now(timezone.utc), "Token expiry should be in the future")
35+
self.assertEqual(token.access_token, authenticator.get_auth_token())
36+
self.assertEqual(self.oauth_host, authenticator.get_host())
37+
self.assertNotEqual(authenticator.refresh_token().access_token, authenticator.refresh_token().access_token,
38+
"Two refreshToken calls should produce different tokens")

test/test_add_organization_request_admin.py

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)