|
| 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