1
|
|
|
import pytest |
|
|
|
|
2
|
|
|
|
3
|
|
|
from django.conf import settings |
|
|
|
|
4
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
5
|
|
|
from django.test.utils import override_settings |
|
|
|
|
6
|
|
|
|
7
|
|
|
from djoser import exceptions, pipelines, signals |
8
|
|
|
from tests.common import catch_signal, mock |
9
|
|
|
|
10
|
|
|
User = get_user_model() |
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
View Code Duplication |
@pytest.mark.django_db(transaction=False) |
|
|
|
|
14
|
|
|
def test_valid_serialize_request(test_user): |
|
|
|
|
15
|
|
|
request = mock.MagicMock() |
16
|
|
|
request.user = test_user |
17
|
|
|
request.data = { |
18
|
|
|
User.USERNAME_FIELD: 'new_username', |
19
|
|
|
'current_password': 'testing123', |
20
|
|
|
} |
21
|
|
|
context = {'request': request} |
22
|
|
|
result = pipelines.username_update.serialize_request(**context) |
23
|
|
|
|
24
|
|
|
assert 'serializer' in result |
25
|
|
|
assert result['serializer'].validated_data == { |
26
|
|
|
User.USERNAME_FIELD: 'new_username', |
27
|
|
|
'current_password': 'testing123', |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
View Code Duplication |
@pytest.mark.django_db(transaction=False) |
|
|
|
|
32
|
|
|
def test_invalid_serialize_request_same_username(test_user): |
|
|
|
|
33
|
|
|
request = mock.MagicMock() |
34
|
|
|
request.user = test_user |
35
|
|
|
request.data = { |
36
|
|
|
User.USERNAME_FIELD: getattr(test_user, User.USERNAME_FIELD), |
37
|
|
|
'current_password': 'testing123', |
38
|
|
|
} |
39
|
|
|
context = {'request': request} |
40
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
41
|
|
|
pipelines.username_update.serialize_request(**context) |
42
|
|
|
|
43
|
|
|
assert e.value.errors == { |
44
|
|
|
'username': ['A user with that username already exists.'] |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
View Code Duplication |
@pytest.mark.django_db(transaction=False) |
|
|
|
|
49
|
|
|
def test_invalid_serialize_request_invalid_username(test_user): |
|
|
|
|
50
|
|
|
request = mock.MagicMock() |
51
|
|
|
request.user = test_user |
52
|
|
|
request.data = { |
53
|
|
|
User.USERNAME_FIELD: '$ lolwut #', |
54
|
|
|
'current_password': 'testing123', |
55
|
|
|
} |
56
|
|
|
context = {'request': request} |
57
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
58
|
|
|
pipelines.username_update.serialize_request(**context) |
59
|
|
|
|
60
|
|
|
assert 'username' in e.value.errors |
61
|
|
|
assert len(e.value.errors['username']) == 1 |
62
|
|
|
assert 'Enter a valid username.' in e.value.errors['username'][0] |
63
|
|
|
|
64
|
|
|
|
65
|
|
View Code Duplication |
@pytest.mark.django_db(transaction=False) |
|
|
|
|
66
|
|
|
@override_settings( |
67
|
|
|
DJOSER=dict(settings.DJOSER, **{'USERNAME_UPDATE_REQUIRE_RETYPE': True}) |
68
|
|
|
) |
|
|
|
|
69
|
|
|
def test_invalid_serialize_request_username_retype_mismatch(test_user): |
70
|
|
|
request = mock.MagicMock() |
71
|
|
|
request.user = test_user |
72
|
|
|
request.data = { |
73
|
|
|
User.USERNAME_FIELD: 'new_username', |
74
|
|
|
're_' + User.USERNAME_FIELD: 'spanish_inquisition', |
75
|
|
|
'current_password': 'testing123', |
76
|
|
|
} |
77
|
|
|
context = {'request': request} |
78
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
79
|
|
|
pipelines.username_update.serialize_request(**context) |
80
|
|
|
|
81
|
|
|
assert e.value.errors == { |
82
|
|
|
'non_field_errors': ["The two username fields didn't match."] |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
@pytest.mark.django_db(transaction=False) |
87
|
|
|
def test_valid_perform(test_user): |
|
|
|
|
88
|
|
|
request = mock.MagicMock() |
89
|
|
|
request.user = test_user |
90
|
|
|
serializer = mock.MagicMock() |
91
|
|
|
serializer.validated_data = { |
92
|
|
|
User.USERNAME_FIELD: 'new_username', |
93
|
|
|
'current_password': 'testing123', |
94
|
|
|
} |
95
|
|
|
context = {'request': request, 'serializer': serializer} |
96
|
|
|
result = pipelines.username_update.perform(**context) |
97
|
|
|
|
98
|
|
|
assert 'user' in result |
99
|
|
|
test_user.refresh_from_db() |
100
|
|
|
assert test_user == result['user'] |
101
|
|
|
assert getattr(test_user, User.USERNAME_FIELD) == 'new_username' |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def test_valid_signal(test_user): |
|
|
|
|
105
|
|
|
request = mock.MagicMock() |
106
|
|
|
context = {'request': request, 'user': test_user} |
107
|
|
|
|
108
|
|
|
with catch_signal(signals.username_updated) as handler: |
109
|
|
|
pipelines.username_update.signal(**context) |
110
|
|
|
|
111
|
|
|
handler.assert_called_once_with( |
112
|
|
|
sender=mock.ANY, |
113
|
|
|
signal=signals.username_updated, |
114
|
|
|
user=test_user, |
115
|
|
|
request=request |
116
|
|
|
) |
117
|
|
|
|
118
|
|
|
|
119
|
|
View Code Duplication |
@pytest.mark.django_db(transaction=False) |
|
|
|
|
120
|
|
|
def test_valid_pipeline(test_user): |
|
|
|
|
121
|
|
|
request = mock.MagicMock() |
122
|
|
|
request.user = test_user |
123
|
|
|
request.data = { |
124
|
|
|
User.USERNAME_FIELD: 'new_username', |
125
|
|
|
'current_password': 'testing123', |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
pipeline = pipelines.username_update.Pipeline(request) |
129
|
|
|
with catch_signal(signals.username_updated) as handler: |
130
|
|
|
result = pipeline.run() |
131
|
|
|
|
132
|
|
|
handler.assert_called_once_with( |
133
|
|
|
sender=mock.ANY, |
134
|
|
|
signal=signals.username_updated, |
135
|
|
|
user=result['user'], |
136
|
|
|
request=request |
137
|
|
|
) |
138
|
|
|
|
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.