1
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
2
|
|
|
from djet import assertions, restframework |
|
|
|
|
3
|
|
|
from rest_framework import status |
|
|
|
|
4
|
|
|
|
5
|
|
|
import djoser.constants |
6
|
|
|
import djoser.utils |
7
|
|
|
import djoser.views |
8
|
|
|
|
9
|
|
|
from .common import create_user |
10
|
|
|
|
11
|
|
|
User = get_user_model() |
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class UserDeleteViewTest(restframework.APIViewTestCase, |
|
|
|
|
15
|
|
|
assertions.StatusCodeAssertionsMixin, |
16
|
|
|
assertions.EmailAssertionsMixin, |
17
|
|
|
assertions.InstanceAssertionsMixin): |
18
|
|
|
view_class = djoser.views.UserDeleteView |
19
|
|
|
|
20
|
|
|
def test_delete_user_if_logged_in(self): |
|
|
|
|
21
|
|
|
user = create_user() |
22
|
|
|
self.assert_instance_exists(User, username='john') |
23
|
|
|
data = { |
24
|
|
|
'current_password': 'secret', |
25
|
|
|
} |
26
|
|
|
request = self.factory.delete(user=user, data=data) |
27
|
|
|
|
28
|
|
|
response = self.view(request) |
29
|
|
|
|
30
|
|
|
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT) |
31
|
|
|
self.assert_instance_does_not_exist(User, username='john') |
32
|
|
|
|
33
|
|
|
def test_not_delete_if_fails_password_validation(self): |
|
|
|
|
34
|
|
|
user = create_user() |
35
|
|
|
self.assert_instance_exists(User, username='john') |
36
|
|
|
data = { |
37
|
|
|
'current_password': 'incorrect', |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
request = self.factory.delete(user=user, data=data) |
41
|
|
|
response = self.view(request) |
42
|
|
|
|
43
|
|
|
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST) |
44
|
|
|
self.assertEqual( |
45
|
|
|
response.data, |
46
|
|
|
{'current_password': ['Invalid password.']} |
47
|
|
|
) |
48
|
|
|
|
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.