|
1
|
|
|
import pytest |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
|
|
4
|
|
|
from django.contrib.auth.tokens import default_token_generator |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
from djoser import constants, exceptions, pipelines, signals, utils |
|
7
|
|
|
from tests.common import catch_signal, mock |
|
8
|
|
|
|
|
9
|
|
|
User = get_user_model() |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
@pytest.mark.django_db(transaction=False) |
|
13
|
|
|
def test_valid_serialize_request(test_user): |
|
|
|
|
|
|
14
|
|
|
request = mock.MagicMock() |
|
15
|
|
|
request.data = { |
|
16
|
|
|
'uid': utils.encode_uid(test_user.pk), |
|
17
|
|
|
'token': default_token_generator.make_token(test_user), |
|
18
|
|
|
'new_password': 'cool-new-password123', |
|
19
|
|
|
} |
|
20
|
|
|
context = {'request': request} |
|
21
|
|
|
result = pipelines.password_reset_confirm.serialize_request(**context) |
|
22
|
|
|
validated_data = result['serializer'].validated_data |
|
23
|
|
|
|
|
24
|
|
|
assert 'serializer' in result |
|
25
|
|
|
assert 'user' in validated_data |
|
26
|
|
|
assert 'new_password' in validated_data |
|
27
|
|
|
assert validated_data['user'] == test_user |
|
28
|
|
|
assert validated_data['new_password'] == request.data['new_password'] |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
@pytest.mark.django_db(transaction=False) |
|
32
|
|
|
def test_invalid_serialize_request_wrong_uid(): |
|
|
|
|
|
|
33
|
|
|
request = mock.MagicMock() |
|
34
|
|
|
request.data = { |
|
35
|
|
|
'uid': utils.encode_uid(1), |
|
36
|
|
|
'token': 'whatever', |
|
37
|
|
|
'new_password': 'whatever-again', |
|
38
|
|
|
} |
|
39
|
|
|
context = {'request': request} |
|
40
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
|
|
41
|
|
|
pipelines.password_reset_confirm.serialize_request(**context) |
|
42
|
|
|
|
|
43
|
|
|
assert e.value.errors == { |
|
44
|
|
|
'non_field_errors': [constants.INVALID_UID_ERROR] |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
@pytest.mark.django_db(transaction=False) |
|
49
|
|
|
def test_invalid_serialize_request_not_existent_user(test_user): |
|
|
|
|
|
|
50
|
|
View Code Duplication |
request = mock.MagicMock() |
|
|
|
|
|
|
51
|
|
|
request.data = { |
|
52
|
|
|
'uid': utils.encode_uid(test_user.pk + 1), |
|
53
|
|
|
'token': default_token_generator.make_token(test_user), |
|
54
|
|
|
'new_password': 'whatever-123', |
|
55
|
|
|
} |
|
56
|
|
|
context = {'request': request} |
|
57
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
|
|
58
|
|
|
pipelines.password_reset_confirm.serialize_request(**context) |
|
59
|
|
|
|
|
60
|
|
|
assert e.value.errors == { |
|
61
|
|
|
'non_field_errors': [constants.INVALID_UID_ERROR] |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
@pytest.mark.django_db(transaction=False) |
|
66
|
|
|
def test_invalid_serialize_request_invalid_token(test_user): |
|
|
|
|
|
|
67
|
|
|
request = mock.MagicMock() |
|
68
|
|
|
request.data = { |
|
69
|
|
|
'uid': utils.encode_uid(test_user.pk), |
|
70
|
|
|
'token': 'invalid-token', |
|
71
|
|
|
'new_password': 'whatever-123', |
|
72
|
|
|
} |
|
73
|
|
|
context = {'request': request} |
|
74
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
|
|
75
|
|
|
pipelines.password_reset_confirm.serialize_request(**context) |
|
76
|
|
|
|
|
77
|
|
|
assert e.value.errors == { |
|
78
|
|
|
'non_field_errors': [constants.INVALID_TOKEN_ERROR] |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
@pytest.mark.django_db(transaction=False) |
|
|
|
|
|
|
83
|
|
|
def test_invalid_serialize_request_retype_mismatch(test_user, settings): |
|
|
|
|
|
|
84
|
|
|
settings.DJOSER = dict( |
|
85
|
|
|
settings.DJOSER, **{'PASSWORD_RESET_CONFIRM_REQUIRE_RETYPE': True} |
|
86
|
|
|
) |
|
87
|
|
|
request = mock.MagicMock() |
|
88
|
|
|
request.data = { |
|
89
|
|
|
'uid': utils.encode_uid(test_user.pk), |
|
90
|
|
|
'token': default_token_generator.make_token(test_user), |
|
91
|
|
|
'new_password': 'whatever-123', |
|
92
|
|
|
're_new_password': 'meh', |
|
93
|
|
|
} |
|
94
|
|
|
context = {'request': request} |
|
95
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
|
|
96
|
|
|
pipelines.password_reset_confirm.serialize_request(**context) |
|
97
|
|
|
|
|
98
|
|
|
assert e.value.errors == { |
|
99
|
|
|
'non_field_errors': [constants.PASSWORD_MISMATCH_ERROR] |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
@pytest.mark.django_db(transaction=False) |
|
104
|
|
|
def test_invalid_serialize_request_password_validation_fail(test_user): |
|
|
|
|
|
|
105
|
|
|
request = mock.MagicMock() |
|
106
|
|
|
request.data = { |
|
107
|
|
|
'uid': utils.encode_uid(test_user.pk), |
|
108
|
|
|
'token': default_token_generator.make_token(test_user), |
|
109
|
|
|
'new_password': '666', |
|
110
|
|
|
} |
|
111
|
|
|
context = {'request': request} |
|
112
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
|
|
113
|
|
|
pipelines.password_reset_confirm.serialize_request(**context) |
|
114
|
|
|
|
|
115
|
|
|
assert e.value.errors == { |
|
116
|
|
|
'new_password': ['Password 666 is not allowed.'] |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
@pytest.mark.django_db(transaction=False) |
|
121
|
|
|
def test_valid_perform(test_user): |
|
|
|
|
|
|
122
|
|
|
serializer = mock.MagicMock() |
|
123
|
|
|
serializer.validated_data = { |
|
124
|
|
|
'user': test_user, |
|
125
|
|
|
'new_password': 'cool-new-password123', |
|
126
|
|
|
} |
|
127
|
|
|
context = {'serializer': serializer} |
|
128
|
|
|
result = pipelines.password_reset_confirm.perform(**context) |
|
129
|
|
|
|
|
130
|
|
|
assert result['user'] == test_user |
|
131
|
|
|
assert test_user.check_password(serializer.validated_data['new_password']) |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
def test_valid_signal(test_user): |
|
|
|
|
|
|
135
|
|
|
request = mock.MagicMock() |
|
136
|
|
|
context = {'request': request, 'user': test_user} |
|
137
|
|
|
|
|
138
|
|
|
with catch_signal(signals.password_reset_completed) as handler: |
|
139
|
|
|
pipelines.password_reset_confirm.signal(**context) |
|
140
|
|
|
|
|
141
|
|
|
handler.assert_called_once_with( |
|
142
|
|
|
sender=mock.ANY, |
|
143
|
|
|
signal=signals.password_reset_completed, |
|
144
|
|
|
user=test_user, |
|
145
|
|
|
request=request |
|
146
|
|
|
) |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
@pytest.mark.django_db(transaction=False) |
|
150
|
|
|
def test_valid_pipeline(test_user): |
|
|
|
|
|
|
151
|
|
|
request = mock.MagicMock() |
|
152
|
|
|
request.data = { |
|
153
|
|
|
'uid': utils.encode_uid(test_user.pk), |
|
154
|
|
|
'token': default_token_generator.make_token(test_user), |
|
155
|
|
|
'new_password': 'cool-new-password123', |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
pipeline = pipelines.password_reset_confirm.Pipeline(request) |
|
159
|
|
|
result = pipeline.run() |
|
160
|
|
|
|
|
161
|
|
|
test_user.refresh_from_db() |
|
162
|
|
|
assert result['user'] == test_user |
|
163
|
|
|
assert test_user.check_password(request.data['new_password']) |
|
164
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.