Newer
Older
import pytest
from keycloak import KeycloakAdmin, KeycloakOpenID
@pytest.fixture
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
'''
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'])
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
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