|
1
|
|
|
from django.conf import settings |
|
|
|
|
|
|
2
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
|
|
3
|
|
|
from django.test.utils import override_settings |
|
|
|
|
|
|
4
|
|
|
from djet import assertions, restframework |
|
|
|
|
|
|
5
|
|
|
from rest_framework import status |
|
|
|
|
|
|
6
|
|
|
import djoser.constants |
|
7
|
|
|
import djoser.utils |
|
8
|
|
|
import djoser.views |
|
9
|
|
|
|
|
10
|
|
|
from .common import create_user, mock, perform_create_mock |
|
11
|
|
|
|
|
12
|
|
|
User = get_user_model() |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class UserCreateViewTest(restframework.APIViewTestCase, |
|
|
|
|
|
|
16
|
|
|
assertions.StatusCodeAssertionsMixin, |
|
17
|
|
|
assertions.EmailAssertionsMixin, |
|
18
|
|
|
assertions.InstanceAssertionsMixin): |
|
19
|
|
|
view_class = djoser.views.UserCreateView |
|
20
|
|
|
|
|
21
|
|
|
def test_post_create_user_without_login(self): |
|
|
|
|
|
|
22
|
|
|
data = { |
|
23
|
|
|
'username': 'john', |
|
24
|
|
|
'password': 'secret', |
|
25
|
|
|
'csrftoken': 'asdf', |
|
26
|
|
|
} |
|
27
|
|
|
request = self.factory.post(data=data) |
|
28
|
|
|
|
|
29
|
|
|
response = self.view(request) |
|
30
|
|
|
|
|
31
|
|
|
self.assert_status_equal(response, status.HTTP_201_CREATED) |
|
32
|
|
|
self.assertTrue('password' not in response.data) |
|
33
|
|
|
self.assert_instance_exists(User, username=data['username']) |
|
34
|
|
|
user = User.objects.get(username=data['username']) |
|
35
|
|
|
self.assertTrue(user.check_password(data['password'])) |
|
36
|
|
|
|
|
37
|
|
|
@override_settings( |
|
38
|
|
|
DJOSER=dict(settings.DJOSER, **{'SEND_ACTIVATION_EMAIL': True}) |
|
39
|
|
|
) |
|
|
|
|
|
|
40
|
|
|
def test_post_create_user_with_login_and_send_activation_email(self): |
|
41
|
|
|
data = { |
|
42
|
|
|
'username': 'john', |
|
43
|
|
|
'email': '[email protected]', |
|
44
|
|
|
'password': 'secret', |
|
45
|
|
|
} |
|
46
|
|
|
request = self.factory.post(data=data) |
|
47
|
|
|
response = self.view(request) |
|
48
|
|
|
|
|
49
|
|
|
self.assert_status_equal(response, status.HTTP_201_CREATED) |
|
50
|
|
|
self.assert_instance_exists(User, username=data['username']) |
|
51
|
|
|
self.assert_emails_in_mailbox(1) |
|
52
|
|
|
self.assert_email_exists(to=[data['email']]) |
|
53
|
|
|
|
|
54
|
|
|
user = User.objects.get(username='john') |
|
55
|
|
|
self.assertFalse(user.is_active) |
|
56
|
|
|
|
|
57
|
|
|
@override_settings( |
|
58
|
|
|
DJOSER=dict(settings.DJOSER, **{ |
|
59
|
|
|
'SEND_ACTIVATION_EMAIL': False, 'SEND_CONFIRMATION_EMAIL': True |
|
60
|
|
|
}) |
|
|
|
|
|
|
61
|
|
|
) |
|
62
|
|
|
def test_post_create_user_with_login_and_send_confirmation_email(self): |
|
63
|
|
|
data = { |
|
64
|
|
|
'username': 'john', |
|
65
|
|
|
'email': '[email protected]', |
|
66
|
|
|
'password': 'secret', |
|
67
|
|
|
} |
|
68
|
|
|
request = self.factory.post(data=data) |
|
69
|
|
|
response = self.view(request) |
|
70
|
|
|
|
|
71
|
|
|
self.assert_status_equal(response, status.HTTP_201_CREATED) |
|
72
|
|
|
self.assert_instance_exists(User, username=data['username']) |
|
73
|
|
|
self.assert_emails_in_mailbox(1) |
|
74
|
|
|
self.assert_email_exists(to=[data['email']]) |
|
75
|
|
|
|
|
76
|
|
|
user = User.objects.get(username='john') |
|
77
|
|
|
self.assertTrue(user.is_active) |
|
78
|
|
|
|
|
79
|
|
|
def test_post_not_create_new_user_if_username_exists(self): |
|
|
|
|
|
|
80
|
|
|
create_user(username='john') |
|
81
|
|
|
data = { |
|
82
|
|
|
'username': 'john', |
|
83
|
|
|
'password': 'secret', |
|
84
|
|
|
'csrftoken': 'asdf', |
|
85
|
|
|
} |
|
86
|
|
|
request = self.factory.post(data=data) |
|
87
|
|
|
|
|
88
|
|
|
response = self.view(request) |
|
89
|
|
|
|
|
90
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
|
91
|
|
|
|
|
92
|
|
|
def test_post_not_register_if_fails_password_validation(self): |
|
|
|
|
|
|
93
|
|
|
data = { |
|
94
|
|
|
'username': 'john', |
|
95
|
|
|
'password': '666', |
|
96
|
|
|
'csrftoken': 'asdf', |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
request = self.factory.post(data=data) |
|
100
|
|
|
response = self.view(request) |
|
101
|
|
|
|
|
102
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
|
103
|
|
|
self.assertEqual( |
|
104
|
|
|
response.data, |
|
105
|
|
|
{'password': ['Password 666 is not allowed.']} |
|
106
|
|
|
) |
|
107
|
|
|
|
|
108
|
|
|
@mock.patch('djoser.serializers.UserRegistrationSerializer.perform_create', |
|
109
|
|
|
side_effect=perform_create_mock) |
|
110
|
|
|
def test_post_return_400_for_integrity_error(self, perform_create): |
|
|
|
|
|
|
111
|
|
|
data = { |
|
112
|
|
|
'username': 'john', |
|
113
|
|
|
'email': '[email protected]', |
|
114
|
|
|
'password': 'secret', |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
request = self.factory.post(data=data) |
|
118
|
|
|
response = self.view(request) |
|
119
|
|
|
|
|
120
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
|
121
|
|
|
self.assertEqual( |
|
122
|
|
|
response.data, [djoser.constants.CANNOT_CREATE_USER_ERROR] |
|
123
|
|
|
) |
|
124
|
|
|
|
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.