Skip to content
Snippets Groups Projects
fixtures.py 2.91 KiB
Newer Older
from http import server
Juan Pedro Sánchez's avatar
Juan Pedro Sánchez committed
import pytest

from keycloak import KeycloakAdmin, KeycloakOpenID


@pytest.fixture
def ov_ccee_client():
    '''
    OV CCEE client example
    '''
    return {
        'clientId': 'ov_ccee_client',
        'protocol': 'openid-connect',
        'enabled': True,
        'secret': '7srwMecg9qw23sAM1JSoB0aFtSqYFJAD',
        'directAccessGrantsEnabled': True,
        'redirectUris': ['http://localhost:8080/auth/*']
    }


@pytest.fixture
def emma_goldman():
    return {
        'email': 'emma.goldman@ccee.coop',
        'username': 'emma.goldman',
        'enabled': True,
        'firstName': 'Emma',
        'lastName': 'Goldman',
        'credentials': [{
            'type': 'password',
            'value': '1234',
            'temporary': False
        }]
    } 


@pytest.fixture
def enrico_malatesta():
    return {
        'email': 'e.malatesta@ccee.coop',
        'username': 'e.malatesta',
        'enabled': True,
        'firstName': 'Errico',
        'lastName': 'Malatesta',
        'credentials': [{
            'type': 'password',
            'value': '1234',
            'temporary': False
        }]
    }


@pytest.fixture
def ov_ccee_clients(ov_ccee_client):
    '''
    Test client for ccee realm
    '''
    return [ov_ccee_client]


@pytest.fixture
def ov_ccee_users(emma_goldman, enrico_malatesta):
    '''
    Base users for ccee realm
    '''
    return [emma_goldman, enrico_malatesta]


@pytest.fixture
def ov_ccee_realm(ov_ccee_clients, ov_ccee_users):
    '''
    OV CCEE realm sample
    '''
    realm = {
        'realm': 'ccee',
        'enabled': True,
        'users': ov_ccee_users,
        'clients': ov_ccee_clients,
    }
    return realm


@pytest.fixture
def keycloak_admin(settings):
    '''
    Keycloak admin client
    '''
Juan Pedro Sánchez's avatar
Juan Pedro Sánchez committed
    admin = KeycloakAdmin(**settings.KEYCLOAK['admin'])
    yield admin


@pytest.fixture
def ccee_base_setup(keycloak_admin, ov_ccee_realm):
    '''
    Creates the minimal users and realm configuration to test
    against keycloak auth server
    '''
    # create test ccee realm
    keycloak_admin.create_realm(
        payload=ov_ccee_realm
    )
    ccee_realm = keycloak_admin.get_realm(ov_ccee_realm['realm'])
    yield ccee_realm
    keycloak_admin.delete_realm(ccee_realm['realm'])
Juan Pedro Sánchez's avatar
Juan Pedro Sánchez committed


@pytest.fixture
def keycloak(keycloak_admin, ccee_base_setup, ov_ccee_client, ov_ccee_realm):
    '''
    keycloak ccee client
    '''
    keycloak = KeycloakOpenID(
        server_url=keycloak_admin._server_url,
        client_id=ov_ccee_client['clientId'],
        realm_name=ov_ccee_realm['realm'],
        client_secret_key=ov_ccee_client['secret']
    )
    yield keycloak
Juan Pedro Sánchez's avatar
Juan Pedro Sánchez committed


@pytest.fixture
def emma_auth_token(keycloak, emma_goldman):
    '''
    Emma Goldman authetication token. This token is the same as the
    returned to the frontend
    '''
    password = emma_goldman['credentials'][0]['value']
    token = keycloak.token(emma_goldman['username'], password)
    return token