1
|
|
|
""" |
2
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
3
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from pytest import raises |
7
|
|
|
|
8
|
|
|
from byceps.services.authentication.exceptions import AuthenticationFailed |
9
|
|
|
from byceps.services.authentication import service as authn_service |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
CORRECT_PASSWORD = 'opensesame' |
13
|
|
|
WRONG_PASSWORD = '123456' |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_uninitialized_user_is_rejected(make_user): |
17
|
|
|
user = create_user(make_user, initialized=False) |
18
|
|
|
|
19
|
|
|
with raises(AuthenticationFailed): |
20
|
|
|
authn_service.authenticate(user.screen_name, CORRECT_PASSWORD) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def test_suspended_user_is_rejected(make_user): |
24
|
|
|
user = create_user(make_user, suspended=True) |
25
|
|
|
|
26
|
|
|
with raises(AuthenticationFailed): |
27
|
|
|
authn_service.authenticate(user.screen_name, CORRECT_PASSWORD) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def test_deleted_user_is_rejected(make_user): |
31
|
|
|
user = create_user(make_user, deleted=True) |
32
|
|
|
|
33
|
|
|
with raises(AuthenticationFailed): |
34
|
|
|
authn_service.authenticate(user.screen_name, CORRECT_PASSWORD) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def test_with_wrong_password_is_rejected(make_user): |
38
|
|
|
user = create_user(make_user) |
39
|
|
|
|
40
|
|
|
with raises(AuthenticationFailed): |
41
|
|
|
authn_service.authenticate(user.screen_name, WRONG_PASSWORD) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def test_active_user_with_screen_name_and_correct_password_is_accepted( |
45
|
|
|
make_user, |
46
|
|
|
): |
47
|
|
|
user = create_user(make_user) |
48
|
|
|
|
49
|
|
|
authenticated_user = authn_service.authenticate( |
50
|
|
|
user.screen_name, CORRECT_PASSWORD |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
assert authenticated_user is not None |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def test_active_user_with_email_address_and_correct_password_is_accepted( |
57
|
|
|
make_user, |
58
|
|
|
): |
59
|
|
|
user = create_user(make_user, email_address='[email protected]') |
60
|
|
|
|
61
|
|
|
authenticated_user = authn_service.authenticate( |
62
|
|
|
'[email protected]', CORRECT_PASSWORD |
63
|
|
|
) |
64
|
|
|
|
65
|
|
|
assert authenticated_user is not None |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def create_user(make_user, **kwargs): |
69
|
|
|
return make_user(password=CORRECT_PASSWORD, **kwargs) |
70
|
|
|
|