|
1
|
|
|
from django.contrib.auth import user_logged_in, user_login_failed |
|
|
|
|
|
|
2
|
|
|
from djet import assertions, restframework |
|
|
|
|
|
|
3
|
|
|
from rest_framework import status |
|
|
|
|
|
|
4
|
|
|
import django |
|
|
|
|
|
|
5
|
|
|
import djoser.constants |
|
6
|
|
|
import djoser.utils |
|
7
|
|
|
import djoser.views |
|
8
|
|
|
|
|
9
|
|
|
from .common import create_user |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TokenCreateViewTest(restframework.APIViewTestCase, |
|
|
|
|
|
|
13
|
|
|
assertions.StatusCodeAssertionsMixin, |
|
14
|
|
|
assertions.InstanceAssertionsMixin): |
|
15
|
|
|
view_class = djoser.views.TokenCreateView |
|
16
|
|
|
|
|
17
|
|
|
def setUp(self): |
|
|
|
|
|
|
18
|
|
|
self.signal_sent = False |
|
19
|
|
|
|
|
20
|
|
|
def signal_receiver(self, *args, **kwargs): |
|
|
|
|
|
|
21
|
|
|
self.signal_sent = True |
|
22
|
|
|
|
|
23
|
|
|
def test_post_should_login_user(self): |
|
|
|
|
|
|
24
|
|
|
user = create_user() |
|
25
|
|
|
data = { |
|
26
|
|
|
'username': user.username, |
|
27
|
|
|
'password': user.raw_password, |
|
28
|
|
|
} |
|
29
|
|
|
user_logged_in.connect(self.signal_receiver) |
|
30
|
|
|
request = self.factory.post(data=data) |
|
31
|
|
|
|
|
32
|
|
|
response = self.view(request) |
|
33
|
|
|
|
|
34
|
|
|
self.assert_status_equal(response, status.HTTP_200_OK) |
|
35
|
|
|
self.assertEqual(response.data['auth_token'], user.auth_token.key) |
|
36
|
|
|
self.assertTrue(self.signal_sent) |
|
37
|
|
|
|
|
38
|
|
|
def test_post_should_not_login_if_user_is_not_active(self): |
|
|
|
|
|
|
39
|
|
|
""" |
|
40
|
|
|
In Django >= 1.10 authenticate() returns None if |
|
41
|
|
|
user is inactive, while in Django < 1.10 authenticate() |
|
42
|
|
|
succeeds if user is inactive. |
|
43
|
|
|
""" |
|
44
|
|
|
user = create_user() |
|
45
|
|
|
data = { |
|
46
|
|
|
'username': user.username, |
|
47
|
|
|
'password': user.raw_password, |
|
48
|
|
|
} |
|
49
|
|
|
user.is_active = False |
|
50
|
|
|
user.save() |
|
51
|
|
|
user_logged_in.connect(self.signal_receiver) |
|
52
|
|
|
request = self.factory.post(data=data) |
|
53
|
|
|
|
|
54
|
|
|
response = self.view(request) |
|
55
|
|
|
|
|
56
|
|
|
if django.VERSION >= (1, 10): |
|
57
|
|
|
expected_errors = [djoser.constants.INVALID_CREDENTIALS_ERROR] |
|
58
|
|
|
else: |
|
59
|
|
|
expected_errors = [djoser.constants.INACTIVE_ACCOUNT_ERROR] |
|
60
|
|
|
|
|
61
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
|
62
|
|
|
self.assertEqual(response.data['non_field_errors'], expected_errors) |
|
63
|
|
|
self.assertFalse(self.signal_sent) |
|
64
|
|
|
|
|
65
|
|
|
def test_post_should_not_login_if_invalid_credentials(self): |
|
|
|
|
|
|
66
|
|
|
user = create_user() |
|
67
|
|
|
data = { |
|
68
|
|
|
'username': user.username, |
|
69
|
|
|
'password': 'wrong', |
|
70
|
|
|
} |
|
71
|
|
|
user_login_failed.connect(self.signal_receiver) |
|
72
|
|
|
request = self.factory.post(data=data) |
|
73
|
|
|
|
|
74
|
|
|
response = self.view(request) |
|
75
|
|
|
|
|
76
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
|
77
|
|
|
self.assertEqual( |
|
78
|
|
|
response.data['non_field_errors'], |
|
79
|
|
|
[djoser.constants.INVALID_CREDENTIALS_ERROR] |
|
80
|
|
|
) |
|
81
|
|
|
self.assertTrue(self.signal_sent) |
|
82
|
|
|
|
|
83
|
|
|
def test_post_should_not_login_if_empty_request(self): |
|
|
|
|
|
|
84
|
|
|
data = {} |
|
85
|
|
|
request = self.factory.post(data=data) |
|
86
|
|
|
|
|
87
|
|
|
response = self.view(request) |
|
88
|
|
|
|
|
89
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
|
90
|
|
|
self.assertEqual( |
|
91
|
|
|
response.data['non_field_errors'], |
|
92
|
|
|
[djoser.constants.INVALID_CREDENTIALS_ERROR] |
|
93
|
|
|
) |
|
94
|
|
|
|
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.