forked from grafana/django-saml2-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user.py
524 lines (442 loc) · 18.6 KB
/
test_user.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
"""
Tests for user.py
"""
from typing import Any, Dict, Union
import pytest
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, User
from django_saml2_auth.exceptions import SAMLAuthError
from django_saml2_auth.user import (create_custom_or_default_jwt, create_new_user,
decode_custom_or_default_jwt, get_or_create_user,
get_user, get_user_id)
from jwt.exceptions import PyJWTError
from pytest_django.fixtures import SettingsWrapper
private_key = """-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,098DF8240D954EE0
uW/IYor+xm5vOLHhDovanTaYWf+N/f+Yae80KJyuXaJ45jVtZBmbhQKy6MIc3pHG
QICO4x8esHkOgkzicnjGjaWscTIEy6yZzOzAGsA8t4uLTKix9TM31QQnaUVVWXtn
mSJN6rY3Qdudgzss47qcFjS+Rhr+X+u8PB3FhOd6Tgkphpl5Vdkb2K+bcbaXuUlN
DUUigoR0+7N5wCJHGKKQd6YRDKvSC4yo0VHbN3Hb55+84m1MGE+pPU8krkmL0u3M
7b37nOVXq+4bE/2t0UIVwZyz+pAqAC3tRpSKfe8EN/R2VcfRi4QNrtAkLxYaLWCr
SpQA7/qLuEXH1LeUFvDk/SjmCBvNz5vm8hSrj0O5eXN4uIBTg7tdof4KOxozaYKM
4SSnfEWjwDwGr/fE6Om5wKrwUjOm3ZTkOHEz5AarVFOsrccLFxSl7+RRKCeIlHSl
uAmqBW6pVxS65fwCwocHJ0jVKxEKGz3j++aqmQ0omEhtGWcDcfQMP6sAV1tY/FDd
NkVTD/cv23SeHZtjtCxz0W8/Vsqs6U6HMLTb0uJVVdMBiPnTYPKOeACx6GxnwH/e
VrjKEy9xqxzdo59lExl06ZBLd9x9u2CAhLqUIlqQu4EtGStpcDuEHj8LY7Y2Jt/G
w2IGG72YXrrewCFjgYcvsIjbw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cYC0Td5/bALhDSbJ0I3+xEAlg
5cqgltGLvG7KORfMYNatKrL3AtxISCxK27B3ezWk+w6U6wNGM6S98ibm8sBe1U1K
/XUBdqEXlp3yLsZTqnMR6LECgYBRrl9WuCCB/2TT12NZNOLLX5i7fvfecupyXPZ6
2g0yDljC/9jRRgDhKjRDjMm8K/EvIr6IVn2Z0Trt60ke9zBX0JueWzdP7EZPz37M
GeKTiO5dkE1atJNnC/4VBlMB4qUpwGj0L0JkaMmh6pR0j0SzVkpW8NF8fTBPvDNE
C+ksGQKBgDvsvOc/8OfDVvu3OgUJnACQdKUD56ppYUZ8XLo34vUm2JAXeb5nRAdZ
Fo4X5nN+G7W2lV5W5384zfjN3IREeZtg4ZKw0w+vJrhz6bixBIOfMCu0O9TYT11B
G2RcH+T0kcs7QbXTY4QrMEYYQj4viihDDo3Ndt5eNJKPz2s+F2h3
-----END RSA PRIVATE KEY-----"""
public_key = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5EBnGco/VgirZAUk1qtP
R1pkgEMdqBZImTPp6Xf4MDhB7zevveqlXAdaFcvrG4rdO4jHr5snJzJpY52en27a
/z2gtQhR9f/05ZjpX9eKCpNt8c/2SDf/P+omEbvbvoUSjkyGeWBacRuHHj6C1voU
rl1aKokRfV9GaSkN7+lOxCBCjXFnxEsLlbJZFSsAQPf1282v5SuE2FxMjbs/Xrmj
l1Q3h8YsV+vvrRK6/OczPVUb6M4HKHqIpH5UTjP/44Mr5sbanjEEYCOsxiM7Q8JU
EYczyjn/o2j8/w0Lms8LB8tnVpxdpwp7QoPVRIr1slYlilP/fL6ZzL5k87XUsi4y
GwIDAQAB
-----END PUBLIC KEY-----"""
def trigger_change_first_name(user: Union[str, Dict[str, str]]) -> None:
"""Trigger function to change user's first name.
Args:
user (Union[str, Dict[str, str]]): User information
"""
_user = get_user(user)
_user.first_name = "CHANGED_FIRSTNAME"
_user.save()
def trigger_get_user(user: Dict) -> User:
"""Trigger function to get a user.
Args:
user (Union[str, Dict[str, str]]): User information
"""
user_model = get_user_model()
return user_model.objects.get(email=user['username'])
@pytest.mark.django_db
def test_create_new_user_success(settings: SettingsWrapper):
"""Test create_new_user function to verify if it works and correctly joins the user to the
respective group.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"NEW_USER_PROFILE": {
"USER_GROUPS": ["users"],
}
}
# Create a group for the users to join
Group.objects.create(name="users")
user = create_new_user("[email protected]", "John", "Doe")
# It can also be email depending on USERNAME_FIELD setting
assert user.username == "[email protected]"
assert user.is_active is True
assert user.has_usable_password() is False
assert user.groups.get(name="users") == Group.objects.get(name="users")
@pytest.mark.django_db
def test_create_new_user_with_dict_success(settings: SettingsWrapper):
"""Test create_new_user function to verify if it works and correctly joins the user to the
respective group.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"NEW_USER_PROFILE": {
"USER_GROUPS": ["users"],
}
}
# Create a group for the users to join
Group.objects.create(name="users")
params = {
"first_name": "test_John",
"last_name": "test_Doe"
}
user = create_new_user("[email protected]", **params)
# It can also be email depending on USERNAME_FIELD setting
assert user.username == "[email protected]"
assert user.is_active is True
assert user.has_usable_password() is False
assert user.groups.get(name="users") == Group.objects.get(name="users")
@pytest.mark.django_db
def test_create_new_user_with_dict_success__no_first_and_last_name(settings: SettingsWrapper):
"""Test create_new_user function to verify if it works and correctly joins the user to the
respective group.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"NEW_USER_PROFILE": {
"USER_GROUPS": ["users"],
}
}
# Create a group for the users to join
Group.objects.create(name="users")
# Create a user without first and last name (valid use-case)
user = create_new_user("[email protected]")
# It can also be email depending on USERNAME_FIELD setting
assert user.username == "[email protected]"
assert user.is_active is True
assert user.has_usable_password() is False
assert user.groups.get(name="users") == Group.objects.get(name="users")
@pytest.mark.django_db
def test_create_new_user_no_group_error(settings: SettingsWrapper):
"""Test create_new_user function to verify if it creates the user, but fails to join the user
to the respective group.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"NEW_USER_PROFILE": {
"USER_GROUPS": ["users"],
}
}
with pytest.raises(SAMLAuthError) as exc_info:
create_new_user("[email protected]", "John", "Doe")
assert str(exc_info.value) == "There was an error joining the user to the group."
assert exc_info.value.extra is not None
assert exc_info.value.extra["exc_type"] == Group.DoesNotExist
def test_create_new_user_value_error():
"""Test create_new_user function to verify if it raises an exception upon passing invalid value
as user_id."""
with pytest.raises(SAMLAuthError) as exc_info:
create_new_user("", "John", "Doe")
assert str(exc_info.value) == "There was an error creating the new user."
assert exc_info.value.extra["exc_type"] == ValueError
@pytest.mark.django_db
def test_get_or_create_user_success(settings: SettingsWrapper):
"""Test get_or_create_user function to verify if it creates a new user and joins it to the
correct group based on the given SAML group and its mapping with internal groups.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"ATTRIBUTES_MAP": {
"groups": "groups",
},
"GROUPS_MAP": {
"consumers": "users"
}
}
Group.objects.create(name="users")
created, user = get_or_create_user({
"username": "[email protected]",
"first_name": "John",
"last_name": "Doe",
"user_identity": {
"user.username": "[email protected]",
"user.first_name": "John",
"user.last_name": "Doe",
"groups": ["consumers"]
}
})
assert created
assert user.username == "[email protected]"
assert user.is_active is True
assert user.has_usable_password() is False
assert user.groups.get(name="users") == Group.objects.get(name="users")
@pytest.mark.django_db
def test_get_or_create_user_trigger_error(settings: SettingsWrapper):
"""Test get_or_create_user function to verify if it raises an exception in case the CREATE_USER
trigger function is nonexistent.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"TRIGGER": {
"CREATE_USER": "django_saml2_auth.tests.test_user.nonexistent_trigger",
}
}
with pytest.raises(SAMLAuthError) as exc_info:
get_or_create_user({
"username": "[email protected]",
"first_name": "John",
"last_name": "Doe"
})
assert str(exc_info.value) == (
"module 'django_saml2_auth.tests.test_user' has no attribute 'nonexistent_trigger'")
assert exc_info.value.extra is not None
assert isinstance(exc_info.value.extra["exc"], AttributeError)
@pytest.mark.django_db
def test_get_user_trigger_error(settings: SettingsWrapper):
"""Test get_user function to verify if it raises an exception in case the GET_USER
trigger function is nonexistent.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"TRIGGER": {
"GET_USER": "django_saml2_auth.tests.test_user.nonexistent_trigger",
}
}
with pytest.raises(SAMLAuthError) as exc_info:
get_user({
"username": "[email protected]",
"first_name": "John",
"last_name": "Doe"
})
assert str(exc_info.value) == (
"module 'django_saml2_auth.tests.test_user' has no attribute 'nonexistent_trigger'")
assert exc_info.value.extra is not None
assert isinstance(exc_info.value.extra["exc"], AttributeError)
@pytest.mark.django_db
def test_get_user_trigger(settings: SettingsWrapper):
settings.SAML2_AUTH = {
"TRIGGER": {
"GET_USER": "django_saml2_auth.tests.test_user.trigger_get_user",
}
}
user_model = get_user_model()
user_model.objects.create(
username="test_example_com", email="[email protected]")
created, user = get_or_create_user({
"username": "[email protected]",
"first_name": "John",
"last_name": "Doe"
})
assert created is False
assert user.username == "test_example_com"
@pytest.mark.django_db
def test_get_or_create_user_trigger_change_first_name(settings: SettingsWrapper):
"""Test get_or_create_user function to verify if it correctly triggers the CREATE_USER function
and the trigger updates the user's first name.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"TRIGGER": {
"CREATE_USER": "django_saml2_auth.tests.test_user.trigger_change_first_name",
}
}
created, user = get_or_create_user({
"username": "[email protected]",
"first_name": "John",
"last_name": "Doe"
})
assert created
assert user.username == "[email protected]"
assert user.first_name == "CHANGED_FIRSTNAME"
assert user.is_active is True
assert user.has_usable_password() is False
@pytest.mark.django_db
def test_get_or_create_user_should_not_create_user(settings: SettingsWrapper):
"""Test get_or_create_user function to verify if it raise an exception while creating a new user
is prohibited by settings.
Args:
settings (SettingsWrapper): Fixture for django settings
"""
settings.SAML2_AUTH = {
"CREATE_USER": False,
}
with pytest.raises(SAMLAuthError) as exc_info:
get_or_create_user({
"username": "[email protected]",
"first_name": "John",
"last_name": "Doe"
})
assert str(exc_info.value) == "Cannot create user."
assert exc_info.value.extra is not None
assert exc_info.value.extra["reason"] == (
"Due to current config, a new user should not be created.")
def test_get_user_id_success():
"""Test get_user_id function to verify if it correctly returns the user_id based on the
User.USERNAME_FIELD."""
assert get_user_id({"username": "[email protected]"}) == "[email protected]"
assert get_user_id("[email protected]") == "[email protected]"
@pytest.mark.django_db
def test_get_user_success():
"""Test get_user function by first creating a new user and then trying to fetch it."""
create_new_user("[email protected]", "John", "Doe")
user_1 = get_user({"username": "[email protected]"})
user_2 = get_user("[email protected]")
assert user_1.username == "[email protected]"
assert user_2.username == "[email protected]"
assert user_1 == user_2
@pytest.mark.parametrize("saml2_settings", [
{
"JWT_ALGORITHM": "HS256",
"JWT_SECRET": "secret"
},
{
"JWT_ALGORITHM": "RS256",
"JWT_PRIVATE_KEY": private_key,
"JWT_PRIVATE_KEY_PASSPHRASE": passphrase,
"JWT_PUBLIC_KEY": public_key},
{
"JWT_ALGORITHM": "RS256",
"JWT_PRIVATE_KEY": unencrypted_private_key,
"JWT_PUBLIC_KEY": public_key},
])
def test_create_and_decode_jwt_token_success(
settings: SettingsWrapper, saml2_settings: Dict[str, Any]):
"""Test create_jwt_token and decode_jwt_token functions by verifying if the newly created
JWT token using is valid.
Args:
settings (SettingsWrapper): Fixture for django settings
saml2_settings (Dict[str, Any]): Fixture for SAML2 settings
"""
settings.SAML2_AUTH = saml2_settings
jwt_token = create_custom_or_default_jwt("[email protected]")
user_id = decode_custom_or_default_jwt(jwt_token)
assert user_id == "[email protected]"
@pytest.mark.parametrize('saml2_settings,error_msg', [
({
"JWT_ALGORITHM": None
}, "Cannot encode/decode JWT token. Specify an algorithm."),
({
"JWT_ALGORITHM": "HS256",
"JWT_SECRET": None
}, "Cannot encode/decode JWT token. Specify a secret."),
({
"JWT_ALGORITHM": "HS256",
"JWT_SECRET": "",
}, "Cannot encode/decode JWT token. Specify a secret."),
({
"JWT_ALGORITHM": "HS256",
"JWT_PRIVATE_KEY": "-- PRIVATE KEY --"
}, "Cannot encode/decode JWT token. Specify a secret."),
({
"JWT_ALGORITHM": "RS256",
}, "Cannot encode/decode JWT token. Specify a private key."),
({
"JWT_ALGORITHM": "RS256",
"JWT_SECRET": "A_SECRET_PHRASE"
}, "Cannot encode/decode JWT token. Specify a private key."),
])
def test_create_jwt_token_with_incorrect_jwt_settings(
settings: SettingsWrapper, saml2_settings: Dict[str, str], error_msg: str):
"""Test create_jwt_token function by trying to create a JWT token with incorrect settings.
Args:
settings (SettingsWrapper): Fixture for django settings
saml2_settings (Dict[str, str]): Fixture for SAML2 settings
error_msg (str): Expected error message
"""
settings.SAML2_AUTH = saml2_settings
with pytest.raises(SAMLAuthError) as exc_info:
create_custom_or_default_jwt("[email protected]")
assert str(exc_info.value) == error_msg
@pytest.mark.parametrize('saml2_settings,error_msg', [
({
"JWT_ALGORITHM": None
}, "Cannot encode/decode JWT token. Specify an algorithm."),
({
"JWT_ALGORITHM": "HS256",
"JWT_SECRET": None
}, "Cannot encode/decode JWT token. Specify a secret."),
({
"JWT_ALGORITHM": "HS256",
"JWT_SECRET": "",
}, "Cannot encode/decode JWT token. Specify a secret."),
({
"JWT_ALGORITHM": "HS256",
"JWT_PRIVATE_KEY": "-- PRIVATE KEY --"
}, "Cannot encode/decode JWT token. Specify a secret."),
({
"JWT_ALGORITHM": "HS256",
"JWT_SECRET": "secret",
"JWT_EXP": -60
}, "Cannot decode JWT token."),
({
"JWT_ALGORITHM": "RS256",
}, "Cannot encode/decode JWT token. Specify a public key."),
({
"JWT_ALGORITHM": "RS256",
"JWT_SECRET": "A_SECRET_PHRASE"
}, "Cannot encode/decode JWT token. Specify a public key.",),
])
def test_decode_jwt_token_with_incorrect_jwt_settings(
settings: SettingsWrapper, saml2_settings: Dict[str, str], error_msg: str):
"""Test decode_jwt_token function by trying to create a JWT token with incorrect settings.
Args:
settings (SettingsWrapper): Fixture for django settings
saml2_settings (Dict[str, str]): Fixture for SAML2 settings
error_msg (str): Expected error message
"""
settings.SAML2_AUTH = saml2_settings
with pytest.raises(SAMLAuthError) as exc_info:
decode_custom_or_default_jwt("WHATEVER")
assert str(exc_info.value) == error_msg
def test_decode_jwt_token_failure():
"""Test decode_jwt_token function by passing an invalid JWT token (None, in this case)."""
with pytest.raises(SAMLAuthError) as exc_info:
decode_custom_or_default_jwt(None)
assert str(exc_info.value) == "Cannot decode JWT token."
assert isinstance(exc_info.value.extra["exc"], PyJWTError)