Skip to content

Commit ae96bd9

Browse files
committed
Added more integration tests to ensure that this works as expected
1 parent 25b1042 commit ae96bd9

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed

poetry.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ flake8 = ">= 4.0.0"
3737
types-python-dateutil = ">= 2.8.19.14"
3838
mypy = ">= 1.5"
3939
testcontainers = "3.7.1"
40+
python-dotenv = "1.0.1"
4041

4142
[build-system]
4243
requires = ["poetry-core>=1.0.0"]
@@ -46,6 +47,8 @@ build-backend = "poetry.core.masonry.api"
4647
envlist = ["py3"]
4748

4849
[tool.pytest.ini_options]
50+
testpaths = ["test", "spec"]
51+
python_files = "test_*.py *_spec.py"
4952
addopts = [
5053
"--cov=zitadel_client",
5154
"--cov-report=html:build/coverage/html",
@@ -68,8 +71,6 @@ files = [
6871
"zitadel_client",
6972
"tests",
7073
]
71-
# TODO: enable "strict" once all these individual checks are passing
72-
# strict = true
7374

7475
# List from: https://mypy.readthedocs.io/en/stable/existing_code.html#introduce-stricter-options
7576
warn_unused_configs = true

spec/__init__.py

Whitespace-only changes.

spec/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
from dotenv import load_dotenv
3+
4+
@pytest.fixture(scope="session", autouse=True)
5+
def load_env():
6+
"""Load the .env file for the entire test session."""
7+
load_dotenv()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import pytest
2+
import uuid
3+
import os
4+
import zitadel_client as zitadel
5+
from zitadel_client.auth.personal_access_token_authenticator import PersonalAccessTokenAuthenticator
6+
from zitadel_client.exceptions import UnauthorizedException
7+
8+
@pytest.fixture
9+
def valid_token():
10+
"""Fixture to return a valid personal access token."""
11+
return os.getenv("AUTH_TOKEN")
12+
13+
@pytest.fixture
14+
def invalid_token():
15+
"""Fixture to return an invalid token."""
16+
return "whoops"
17+
18+
@pytest.fixture
19+
def base_url():
20+
"""Fixture to return the base URL."""
21+
return os.getenv("BASE_URL")
22+
23+
@pytest.fixture
24+
def user_id(valid_token, base_url):
25+
"""Fixture to create a user and return their ID."""
26+
with zitadel.Zitadel(PersonalAccessTokenAuthenticator(base_url, valid_token)) as client:
27+
try:
28+
response = client.users.add_human_user(
29+
body=zitadel.V2AddHumanUserRequest(
30+
username=uuid.uuid4().hex,
31+
profile=zitadel.V2SetHumanProfile(given_name="John", family_name="Doe"),
32+
email=zitadel.V2SetHumanEmail(email=f"johndoe{uuid.uuid4().hex}@caos.ag")
33+
)
34+
)
35+
print("User created:", response)
36+
return response.user_id
37+
except Exception as e:
38+
pytest.fail(f"Exception while creating user: {e}")
39+
40+
def test_should_deactivate_and_reactivate_user_with_valid_token(user_id, valid_token, base_url):
41+
"""Test to (de)activate the user with a valid token."""
42+
with zitadel.Zitadel(PersonalAccessTokenAuthenticator(base_url, valid_token)) as client:
43+
try:
44+
deactivate_response = client.users.deactivate_user(user_id=user_id)
45+
print("User deactivated:", deactivate_response)
46+
47+
reactivate_response = client.users.reactivate_user(user_id=user_id)
48+
print("User reactivated:", reactivate_response)
49+
# Adjust based on actual response format
50+
# assert reactivate_response["status"] == "success"
51+
except Exception as e:
52+
pytest.fail(f"Exception when calling deactivate_user or reactivate_user with valid token: {e}")
53+
54+
def test_should_not_deactivate_or_reactivate_user_with_invalid_token(user_id, invalid_token, base_url):
55+
"""Test to attempt (de)activating the user with an invalid token."""
56+
with zitadel.Zitadel(PersonalAccessTokenAuthenticator(base_url, invalid_token)) as client:
57+
try:
58+
client.users.deactivate_user(user_id=user_id)
59+
pytest.fail("Expected exception when deactivating user with invalid token, but got response.")
60+
except UnauthorizedException as e:
61+
print("Caught expected UnauthorizedException:", e)
62+
except Exception as e:
63+
pytest.fail(f"Invalid exception when calling the function: {e}")
64+
65+
try:
66+
client.users.reactivate_user(user_id=user_id)
67+
pytest.fail("Expected exception when reactivating user with invalid token, but got response.")
68+
except UnauthorizedException as e:
69+
print("Caught expected UnauthorizedException:", e)
70+
except Exception as e:
71+
pytest.fail(f"Invalid exception when calling the function: {e}")

0 commit comments

Comments
 (0)