You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

  1. import os
  2. import uuid
  3. import pytest
  4. from keycloak import KeycloakAdmin
  5. @pytest.fixture
  6. def env():
  7. class KeycloakTestEnv(object):
  8. KEYCLOAK_HOST = os.environ["KEYCLOAK_HOST"]
  9. KEYCLOAK_PORT = os.environ["KEYCLOAK_PORT"]
  10. KEYCLOAK_ADMIN = os.environ["KEYCLOAK_ADMIN"]
  11. KEYCLOAK_ADMIN_PASSWORD = os.environ["KEYCLOAK_ADMIN_PASSWORD"]
  12. return KeycloakTestEnv()
  13. @pytest.fixture
  14. def admin(env):
  15. return KeycloakAdmin(
  16. server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}",
  17. username=env.KEYCLOAK_ADMIN,
  18. password=env.KEYCLOAK_ADMIN_PASSWORD,
  19. )
  20. @pytest.fixture
  21. def realm(admin: KeycloakAdmin) -> str:
  22. realm_name = str(uuid.uuid4())
  23. admin.create_realm(payload={"realm": realm_name})
  24. yield realm_name
  25. admin.delete_realm(realm_name=realm_name)
  26. @pytest.fixture
  27. def user(admin: KeycloakAdmin, realm: str) -> str:
  28. admin.realm_name = realm
  29. username = str(uuid.uuid4())
  30. user_id = admin.create_user(payload={"username": username, "email": f"{username}@test.test"})
  31. yield user_id
  32. admin.delete_user(user_id=user_id)
  33. @pytest.fixture
  34. def group(admin: KeycloakAdmin, realm: str) -> str:
  35. admin.realm_name = realm
  36. group_name = str(uuid.uuid4())
  37. group_id = admin.create_group(payload={"name": group_name})
  38. yield group_id
  39. admin.delete_group(group_id=group_id)
  40. @pytest.fixture
  41. def client(admin: KeycloakAdmin, realm: str) -> str:
  42. admin.realm_name = realm
  43. client = str(uuid.uuid4())
  44. client_id = admin.create_client(payload={"name": client, "clientId": client})
  45. yield client_id
  46. admin.delete_client(client_id=client_id)