|
| 1 | +import os |
| 2 | +import uuid |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import zitadel_client as zitadel |
| 7 | +from zitadel_client.auth.client_credentials_authenticator import ClientCredentialsAuthenticator |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def client_id(): |
| 12 | + """Fixture to return a valid personal access token.""" |
| 13 | + return os.getenv("CLIENT_ID") |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def client_secret(): |
| 18 | + """Fixture to return a valid personal access token.""" |
| 19 | + return os.getenv("CLIENT_SECRET") |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def base_url(): |
| 24 | + """Fixture to return the base URL.""" |
| 25 | + return os.getenv("BASE_URL") |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def user_id(client_id, client_secret, base_url): |
| 30 | + """Fixture to create a user and return their ID.""" |
| 31 | + with zitadel.Zitadel(ClientCredentialsAuthenticator.builder(base_url, client_id, client_secret).build()) as client: |
| 32 | + try: |
| 33 | + response = client.users.add_human_user( |
| 34 | + body=zitadel.models.V2AddHumanUserRequest( |
| 35 | + username=uuid.uuid4().hex, |
| 36 | + profile=zitadel.models.V2SetHumanProfile(given_name="John", family_name="Doe"), |
| 37 | + email=zitadel.models.V2SetHumanEmail(email=f"johndoe{uuid.uuid4().hex}@caos.ag") |
| 38 | + ) |
| 39 | + ) |
| 40 | + print("User created:", response) |
| 41 | + return response.user_id |
| 42 | + except Exception as e: |
| 43 | + pytest.fail(f"Exception while creating user: {e}") |
| 44 | + |
| 45 | + |
| 46 | +def test_should_deactivate_and_reactivate_user_with_valid_token(user_id, client_id, client_secret, base_url): |
| 47 | + """Test to (de)activate the user with a valid token.""" |
| 48 | + with zitadel.Zitadel(ClientCredentialsAuthenticator.builder(base_url, client_id, client_secret).build()) as client: |
| 49 | + try: |
| 50 | + deactivate_response = client.users.deactivate_user(user_id=user_id) |
| 51 | + print("User deactivated:", deactivate_response) |
| 52 | + |
| 53 | + reactivate_response = client.users.reactivate_user(user_id=user_id) |
| 54 | + print("User reactivated:", reactivate_response) |
| 55 | + # Adjust based on actual response format |
| 56 | + # assert reactivate_response["status"] == "success" |
| 57 | + except Exception as e: |
| 58 | + pytest.fail(f"Exception when calling deactivate_user or reactivate_user with valid token: {e}") |
| 59 | + |
| 60 | + |
| 61 | +def test_should_not_deactivate_or_reactivate_user_with_invalid_token(user_id, base_url): |
| 62 | + """Test to attempt (de)activating the user with an invalid token.""" |
| 63 | + with zitadel.Zitadel(ClientCredentialsAuthenticator.builder(base_url, "id", "secret").build()) as client: |
| 64 | + try: |
| 65 | + client.users.deactivate_user(user_id=user_id) |
| 66 | + pytest.fail("Expected exception when deactivating user with invalid token, but got response.") |
| 67 | + except Exception as e: |
| 68 | + print("Caught expected UnauthorizedException:", e) |
| 69 | + |
| 70 | + try: |
| 71 | + client.users.reactivate_user(user_id=user_id) |
| 72 | + pytest.fail("Expected exception when reactivating user with invalid token, but got response.") |
| 73 | + except Exception as e: |
| 74 | + print("Caught expected UnauthorizedException:", e) |
0 commit comments