|
@ -1,3 +1,5 @@ |
|
|
|
|
|
"""Fixtures for tests.""" |
|
|
|
|
|
|
|
|
import os |
|
|
import os |
|
|
import uuid |
|
|
import uuid |
|
|
|
|
|
|
|
@ -7,6 +9,18 @@ from keycloak import KeycloakAdmin, KeycloakOpenID |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class KeycloakTestEnv(object): |
|
|
class KeycloakTestEnv(object): |
|
|
|
|
|
"""Wrapper for test Keycloak connection configuration. |
|
|
|
|
|
|
|
|
|
|
|
:param host: Hostname |
|
|
|
|
|
:type host: str |
|
|
|
|
|
:param port: Port |
|
|
|
|
|
:type port: str |
|
|
|
|
|
:param username: Admin username |
|
|
|
|
|
:type username: str |
|
|
|
|
|
:param password: Admin password |
|
|
|
|
|
:type password: str |
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
def __init__( |
|
|
def __init__( |
|
|
self, |
|
|
self, |
|
|
host: str = os.environ["KEYCLOAK_HOST"], |
|
|
host: str = os.environ["KEYCLOAK_HOST"], |
|
@ -14,6 +28,7 @@ class KeycloakTestEnv(object): |
|
|
username: str = os.environ["KEYCLOAK_ADMIN"], |
|
|
username: str = os.environ["KEYCLOAK_ADMIN"], |
|
|
password: str = os.environ["KEYCLOAK_ADMIN_PASSWORD"], |
|
|
password: str = os.environ["KEYCLOAK_ADMIN_PASSWORD"], |
|
|
): |
|
|
): |
|
|
|
|
|
"""Init method.""" |
|
|
self.KEYCLOAK_HOST = host |
|
|
self.KEYCLOAK_HOST = host |
|
|
self.KEYCLOAK_PORT = port |
|
|
self.KEYCLOAK_PORT = port |
|
|
self.KEYCLOAK_ADMIN = username |
|
|
self.KEYCLOAK_ADMIN = username |
|
@ -21,44 +36,54 @@ class KeycloakTestEnv(object): |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def KEYCLOAK_HOST(self): |
|
|
def KEYCLOAK_HOST(self): |
|
|
|
|
|
"""Hostname getter.""" |
|
|
return self._KEYCLOAK_HOST |
|
|
return self._KEYCLOAK_HOST |
|
|
|
|
|
|
|
|
@KEYCLOAK_HOST.setter |
|
|
@KEYCLOAK_HOST.setter |
|
|
def KEYCLOAK_HOST(self, value: str): |
|
|
def KEYCLOAK_HOST(self, value: str): |
|
|
|
|
|
"""Hostname setter.""" |
|
|
self._KEYCLOAK_HOST = value |
|
|
self._KEYCLOAK_HOST = value |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def KEYCLOAK_PORT(self): |
|
|
def KEYCLOAK_PORT(self): |
|
|
|
|
|
"""Port getter.""" |
|
|
return self._KEYCLOAK_PORT |
|
|
return self._KEYCLOAK_PORT |
|
|
|
|
|
|
|
|
@KEYCLOAK_PORT.setter |
|
|
@KEYCLOAK_PORT.setter |
|
|
def KEYCLOAK_PORT(self, value: str): |
|
|
def KEYCLOAK_PORT(self, value: str): |
|
|
|
|
|
"""Port setter.""" |
|
|
self._KEYCLOAK_PORT = value |
|
|
self._KEYCLOAK_PORT = value |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def KEYCLOAK_ADMIN(self): |
|
|
def KEYCLOAK_ADMIN(self): |
|
|
|
|
|
"""Admin username getter.""" |
|
|
return self._KEYCLOAK_ADMIN |
|
|
return self._KEYCLOAK_ADMIN |
|
|
|
|
|
|
|
|
@KEYCLOAK_ADMIN.setter |
|
|
@KEYCLOAK_ADMIN.setter |
|
|
def KEYCLOAK_ADMIN(self, value: str): |
|
|
def KEYCLOAK_ADMIN(self, value: str): |
|
|
|
|
|
"""Admin username setter.""" |
|
|
self._KEYCLOAK_ADMIN = value |
|
|
self._KEYCLOAK_ADMIN = value |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def KEYCLOAK_ADMIN_PASSWORD(self): |
|
|
def KEYCLOAK_ADMIN_PASSWORD(self): |
|
|
|
|
|
"""Admin password getter.""" |
|
|
return self._KEYCLOAK_ADMIN_PASSWORD |
|
|
return self._KEYCLOAK_ADMIN_PASSWORD |
|
|
|
|
|
|
|
|
@KEYCLOAK_ADMIN_PASSWORD.setter |
|
|
@KEYCLOAK_ADMIN_PASSWORD.setter |
|
|
def KEYCLOAK_ADMIN_PASSWORD(self, value: str): |
|
|
def KEYCLOAK_ADMIN_PASSWORD(self, value: str): |
|
|
|
|
|
"""Admin password setter.""" |
|
|
self._KEYCLOAK_ADMIN_PASSWORD = value |
|
|
self._KEYCLOAK_ADMIN_PASSWORD = value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
@pytest.fixture |
|
|
def env(): |
|
|
def env(): |
|
|
|
|
|
"""Fixture for getting the test environment configuration object.""" |
|
|
return KeycloakTestEnv() |
|
|
return KeycloakTestEnv() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
@pytest.fixture |
|
|
def admin(env: KeycloakTestEnv): |
|
|
def admin(env: KeycloakTestEnv): |
|
|
|
|
|
"""Fixture for initialized KeycloakAdmin class.""" |
|
|
return KeycloakAdmin( |
|
|
return KeycloakAdmin( |
|
|
server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", |
|
|
server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", |
|
|
username=env.KEYCLOAK_ADMIN, |
|
|
username=env.KEYCLOAK_ADMIN, |
|
@ -68,11 +93,20 @@ def admin(env: KeycloakTestEnv): |
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
@pytest.fixture |
|
|
def oid(env: KeycloakTestEnv, realm: str, admin: KeycloakAdmin): |
|
|
def oid(env: KeycloakTestEnv, realm: str, admin: KeycloakAdmin): |
|
|
|
|
|
"""Fixture for initialized KeycloakOpenID class.""" |
|
|
# Set the realm |
|
|
# Set the realm |
|
|
admin.realm_name = realm |
|
|
admin.realm_name = realm |
|
|
# Create client |
|
|
# Create client |
|
|
client = str(uuid.uuid4()) |
|
|
client = str(uuid.uuid4()) |
|
|
client_id = admin.create_client(payload={"name": client, "clientId": client}) |
|
|
|
|
|
|
|
|
client_id = admin.create_client( |
|
|
|
|
|
payload={ |
|
|
|
|
|
"name": client, |
|
|
|
|
|
"clientId": client, |
|
|
|
|
|
"enabled": True, |
|
|
|
|
|
"publicClient": True, |
|
|
|
|
|
"protocol": "openid-connect", |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
# Return OID |
|
|
# Return OID |
|
|
yield KeycloakOpenID( |
|
|
yield KeycloakOpenID( |
|
|
server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", |
|
|
server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", |
|
@ -83,16 +117,61 @@ def oid(env: KeycloakTestEnv, realm: str, admin: KeycloakAdmin): |
|
|
admin.delete_client(client_id=client_id) |
|
|
admin.delete_client(client_id=client_id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
|
|
def oid_with_credentials(env: KeycloakTestEnv, realm: str, admin: KeycloakAdmin): |
|
|
|
|
|
"""Fixture for an initialized KeycloakOpenID class and a random user credentials.""" |
|
|
|
|
|
# Set the realm |
|
|
|
|
|
admin.realm_name = realm |
|
|
|
|
|
# Create client |
|
|
|
|
|
client = str(uuid.uuid4()) |
|
|
|
|
|
client_id = admin.create_client( |
|
|
|
|
|
payload={ |
|
|
|
|
|
"name": client, |
|
|
|
|
|
"clientId": client, |
|
|
|
|
|
"enabled": True, |
|
|
|
|
|
"publicClient": True, |
|
|
|
|
|
"protocol": "openid-connect", |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
# Create user |
|
|
|
|
|
username = str(uuid.uuid4()) |
|
|
|
|
|
password = str(uuid.uuid4()) |
|
|
|
|
|
user_id = admin.create_user( |
|
|
|
|
|
payload={ |
|
|
|
|
|
"username": username, |
|
|
|
|
|
"email": f"{username}@test.test", |
|
|
|
|
|
"enabled": True, |
|
|
|
|
|
"credentials": [{"type": "password", "value": password}], |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
yield ( |
|
|
|
|
|
KeycloakOpenID( |
|
|
|
|
|
server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", |
|
|
|
|
|
realm_name=realm, |
|
|
|
|
|
client_id=client, |
|
|
|
|
|
), |
|
|
|
|
|
username, |
|
|
|
|
|
password, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Cleanup |
|
|
|
|
|
admin.delete_client(client_id=client_id) |
|
|
|
|
|
admin.delete_user(user_id=user_id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
@pytest.fixture |
|
|
def realm(admin: KeycloakAdmin) -> str: |
|
|
def realm(admin: KeycloakAdmin) -> str: |
|
|
|
|
|
"""Fixture for a new random realm.""" |
|
|
realm_name = str(uuid.uuid4()) |
|
|
realm_name = str(uuid.uuid4()) |
|
|
admin.create_realm(payload={"realm": realm_name}) |
|
|
|
|
|
|
|
|
admin.create_realm(payload={"realm": realm_name, "enabled": True}) |
|
|
yield realm_name |
|
|
yield realm_name |
|
|
admin.delete_realm(realm_name=realm_name) |
|
|
admin.delete_realm(realm_name=realm_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
@pytest.fixture |
|
|
def user(admin: KeycloakAdmin, realm: str) -> str: |
|
|
def user(admin: KeycloakAdmin, realm: str) -> str: |
|
|
|
|
|
"""Fixture for a new random user.""" |
|
|
admin.realm_name = realm |
|
|
admin.realm_name = realm |
|
|
username = str(uuid.uuid4()) |
|
|
username = str(uuid.uuid4()) |
|
|
user_id = admin.create_user(payload={"username": username, "email": f"{username}@test.test"}) |
|
|
user_id = admin.create_user(payload={"username": username, "email": f"{username}@test.test"}) |
|
@ -102,6 +181,7 @@ def user(admin: KeycloakAdmin, realm: str) -> str: |
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
@pytest.fixture |
|
|
def group(admin: KeycloakAdmin, realm: str) -> str: |
|
|
def group(admin: KeycloakAdmin, realm: str) -> str: |
|
|
|
|
|
"""Fixture for a new random group.""" |
|
|
admin.realm_name = realm |
|
|
admin.realm_name = realm |
|
|
group_name = str(uuid.uuid4()) |
|
|
group_name = str(uuid.uuid4()) |
|
|
group_id = admin.create_group(payload={"name": group_name}) |
|
|
group_id = admin.create_group(payload={"name": group_name}) |
|
@ -111,6 +191,7 @@ def group(admin: KeycloakAdmin, realm: str) -> str: |
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
@pytest.fixture |
|
|
def client(admin: KeycloakAdmin, realm: str) -> str: |
|
|
def client(admin: KeycloakAdmin, realm: str) -> str: |
|
|
|
|
|
"""Fixture for a new random client.""" |
|
|
admin.realm_name = realm |
|
|
admin.realm_name = realm |
|
|
client = str(uuid.uuid4()) |
|
|
client = str(uuid.uuid4()) |
|
|
client_id = admin.create_client(payload={"name": client, "clientId": client}) |
|
|
client_id = admin.create_client(payload={"name": client, "clientId": client}) |
|
|