1
|
|
|
import pytest |
|
|
|
|
2
|
|
|
|
3
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
4
|
|
|
|
5
|
|
|
from djoser import constants, exceptions, pipelines, signals |
6
|
|
|
from tests.common import catch_signal, mock |
7
|
|
|
|
8
|
|
|
User = get_user_model() |
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
View Code Duplication |
def test_valid_perform(test_user, settings): |
|
|
|
|
12
|
|
|
from djoser.conf import settings as djoser_settings |
13
|
|
|
|
14
|
|
|
settings.DJOSER = dict( |
15
|
|
|
settings.DJOSER, |
16
|
|
|
**{'TOKEN_MODEL': 'rest_framework.authtoken.models.Token'} |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
request = mock.MagicMock() |
20
|
|
|
request.user = test_user |
21
|
|
|
context = {'request': request} |
22
|
|
|
|
23
|
|
|
djoser_settings.TOKEN_MODEL.objects.get_or_create(user=test_user) |
24
|
|
|
assert djoser_settings.TOKEN_MODEL.objects.count() == 1 |
25
|
|
|
|
26
|
|
|
result = pipelines.token_destroy.perform(**context) |
27
|
|
|
assert result['user'] == test_user |
28
|
|
|
assert djoser_settings.TOKEN_MODEL.objects.count() == 0 |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@pytest.mark.django_db(transaction=False) |
32
|
|
|
def test_invalid_perform_none_token_model(test_user): |
|
|
|
|
33
|
|
|
request = mock.MagicMock() |
34
|
|
|
request.user = test_user |
35
|
|
|
context = {'request': request} |
36
|
|
|
|
37
|
|
|
with pytest.raises(exceptions.ValidationError) as e: |
|
|
|
|
38
|
|
|
pipelines.token_destroy.perform(**context) |
39
|
|
|
|
40
|
|
|
assert e.value.errors == constants.TOKEN_MODEL_NONE_ERROR |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def test_valid_signal(test_user): |
|
|
|
|
44
|
|
|
request = mock.MagicMock() |
45
|
|
|
context = {'request': request, 'user': test_user} |
46
|
|
|
|
47
|
|
|
with catch_signal(signals.token_destroyed) as handler: |
48
|
|
|
pipelines.token_destroy.signal(**context) |
49
|
|
|
|
50
|
|
|
handler.assert_called_once_with( |
51
|
|
|
sender=mock.ANY, |
52
|
|
|
signal=signals.token_destroyed, |
53
|
|
|
user=test_user, |
54
|
|
|
request=request |
55
|
|
|
) |
56
|
|
|
|
57
|
|
|
|
58
|
|
View Code Duplication |
@pytest.mark.django_db(transaction=False) |
|
|
|
|
59
|
|
|
def test_valid_pipeline(test_user, settings): |
|
|
|
|
60
|
|
|
from djoser.conf import settings as djoser_settings |
61
|
|
|
|
62
|
|
|
settings.DJOSER = dict( |
63
|
|
|
settings.DJOSER, |
64
|
|
|
**{'TOKEN_MODEL': 'rest_framework.authtoken.models.Token'} |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
request = mock.MagicMock() |
68
|
|
|
request.user = test_user |
69
|
|
|
|
70
|
|
|
djoser_settings.TOKEN_MODEL.objects.get_or_create(user=test_user) |
71
|
|
|
assert djoser_settings.TOKEN_MODEL.objects.count() == 1 |
72
|
|
|
|
73
|
|
|
pipeline = pipelines.token_destroy.Pipeline(request) |
74
|
|
|
with catch_signal(signals.token_destroyed) as handler: |
75
|
|
|
result = pipeline.run() |
76
|
|
|
|
77
|
|
|
handler.assert_called_once_with( |
78
|
|
|
sender=mock.ANY, |
79
|
|
|
signal=signals.token_destroyed, |
80
|
|
|
user=result['user'], |
81
|
|
|
request=request |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
assert result['user'] == test_user |
85
|
|
|
assert djoser_settings.TOKEN_MODEL.objects.count() == 0 |
86
|
|
|
|
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.