forked from elixir-luxembourg/daisy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
237 lines (187 loc) · 6.16 KB
/
conftest.py
1
2
3
4
5
6
7
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import os
import pytest
from django.apps import apps
from django.conf import settings
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.db.backends.postgresql.features import DatabaseFeatures
from guardian.shortcuts import assign_perm
from core.constants import Groups as GroupConstants
from core.management.commands.load_initial_data import Command as CommandLoadInitialData
from core.permissions import GROUP_PERMISSIONS
FIXTURE_DIR = os.path.join(settings.BASE_DIR, "core", "fixtures")
## FAKE LDAP DIRECTORY
LCSB = ("OU=LCSB,OU=Faculties,OU=UNI-Users,DC=uni,DC=lux", {"ou": ["LCSB"]})
disabled_lcsb = (
"OU=LCSB,OU=Faculties,OU=UNI-DisabledUsers,DC=uni,DC=lux",
{"ou": ["LCSB"]},
)
administration = (
"OU=Administration,OU=LCSB,OU=Faculties,OU=UNI-Users,DC=uni,DC=lux",
{"ou": ["Administration"]},
)
def make_fake_ldap_user(
firstname, lastname, password="password", title="Test user", is_external=False
):
"""
Create a fake LDAP user
"""
enc = lambda x: x.encode("utf-8")
dn = "LCSB,OU=Faculties,OU=UNI-Users,DC=uni,DC=lux"
if is_external:
dn = "Administration,OU=FSTC,OU=Faculties,OU=UNI-Users,DC=uni,DC=lux"
small = "%s.%s" % (firstname.lower(), lastname.lower())
return (
"CN=%s,OU=%s" % (small, dn),
{
"cn": [enc("%s.%s" % (firstname, lastname))],
"userPassword": [password],
"accountExpires": ["0".encode("utf-8")],
"title": title,
"userprincipalname": [enc("%[email protected]" % small)],
"objectClass": "person",
"givenName": [enc(firstname)],
"sn": [enc(lastname)],
"displayName": [enc("%s %s" % (firstname, lastname))],
"name": [enc("%s.%s" % (firstname, lastname))],
"mail": [enc("%[email protected]" % small)],
"manager": [
"cn=Superman,OU=Administration,OU=FSTC,OU=Faculties,OU=UNI-Users,DC=uni,DC=lux".encode(
"utf-8"
)
],
"telephoneNumber": ["9999".encode("utf-8")],
},
)
# This is the content of our mock LDAP directory. It takes the form
# {dn: {attr: [value, ...], ...}, ...}.
directory = dict(
[
administration,
LCSB,
disabled_lcsb,
make_fake_ldap_user("Normal", "User"),
make_fake_ldap_user("PI", "number1"),
make_fake_ldap_user("PI", "number2"),
make_fake_ldap_user("Data", "Steward"),
make_fake_ldap_user("External", "User", is_external=True),
]
)
@pytest.fixture(autouse=True)
def configure_mock_ldap():
from mockldap import MockLdap
mockldap = MockLdap(directory)
mockldap.start()
yield
mockldap.stop()
@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db):
pass
# solr_process = solr_process(
# executable=None,
# host='solr',
# port=8983,
# core='daisy_test',
# version='7.3',
# timeout=60
# )
# daisy_test = solr_core('solr_process', 'daisy_test')
# solr = solr('daisy_test')
# @pytest.fixture(autouse=True)
# def enable_solr_access_for_all_tests(solr):
# pass
@pytest.mark.transactional_db
@pytest.fixture(scope="function")
def celery_session_worker(celery_session_worker):
yield celery_session_worker
# clients / users fixtures
# thoses users must correspond to those created in the LDAP tree
@pytest.fixture
def user_normal(django_user_model):
u = django_user_model.objects.create(username="normal.user", password="password")
u.save()
return u
@pytest.fixture
def user_vip(django_user_model):
u = django_user_model.objects.create(username="pi.number1", password="password")
g, _ = Group.objects.get_or_create(name=GroupConstants.VIP.value)
u.groups.add(g)
return u
@pytest.fixture
def user_data_steward(django_user_model):
u = django_user_model.objects.create(username="data.steward", password="password")
g, _ = Group.objects.get_or_create(name=GroupConstants.DATA_STEWARD.value)
u.groups.add(g)
return u
@pytest.fixture
def user_legal(django_user_model):
u = django_user_model.objects.create_user(
username="user.legal", password="password", email="[email protected]"
)
g, _ = Group.objects.get_or_create(name=GroupConstants.LEGAL.value)
u.groups.add(g)
return u
@pytest.fixture
def user_auditor(django_user_model):
u = django_user_model.objects.create_user(
username="user.auditor", password="password", email="[email protected]"
)
g, _ = Group.objects.get_or_create(name=GroupConstants.AUDITOR.value)
u.groups.add(g)
return u
@pytest.fixture
def user_admin(django_user_model):
u = django_user_model.objects.create_superuser(
username="test.admin", email="[email protected]", password="password"
)
return u
@pytest.fixture
def users(django_user_model, user_normal, user_vip, user_data_steward):
"""
Fixture that create users based on the ldap directory created.
"""
password = "password"
u = django_user_model.objects.create(username="pi.number2", password=password)
g, _ = Group.objects.get_or_create(name=GroupConstants.VIP.value)
u.groups.add(g)
u.save()
u = django_user_model.objects.create(username="external.user", password=password)
u.save()
@pytest.fixture
def permissions():
"""
Create basic permission mapping
"""
CommandLoadInitialData.create_roles_and_permissions()
@pytest.fixture
def contact_types():
"""
Create storage resources
"""
CommandLoadInitialData.create_contact_types()
@pytest.fixture
def gdpr_roles():
CommandLoadInitialData.create_gdpr_roles()
@pytest.fixture
def storage_resources():
"""
Create storage resources
"""
CommandLoadInitialData.create_storage_resources()
@pytest.fixture
def data_types():
"""
Create data types
"""
CommandLoadInitialData.create_datatypes()
@pytest.fixture
def partners():
"""
Create partners
"""
CommandLoadInitialData.create_elu_institutions()
@pytest.fixture
def can_defer_constraint_checks():
DatabaseFeatures.can_defer_constraint_checks = True
yield
DatabaseFeatures.can_defer_constraint_checks = False