Passed
Pull Request — master (#259)
by Piotr
01:25
created

test_invalid_serialize_request_invalid_token()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
1
import pytest
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Unable to import 'pytest'
Loading history...
2
3
from django.conf import settings
4
from django.contrib.auth import get_user_model
5
from django.contrib.auth.tokens import default_token_generator
6
from django.test.utils import override_settings
7
8
from djoser import constants, exceptions, pipelines, signals, utils
9
from djoser.conf import settings as djoser_settings
10
from tests.common import catch_signal, mock
11
12
User = get_user_model()
0 ignored issues
show
Coding Style Naming introduced by
The name User does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
13
14
15
@pytest.mark.django_db(transaction=False)
16
def test_valid_serialize_request(test_user):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
17
    request = mock.MagicMock()
18
    request.data = {
19
        'uid': utils.encode_uid(test_user.pk),
20
        'token': default_token_generator.make_token(test_user),
21
        'new_password': 'cool-new-password123',
22
    }
23
    context = {'request': request}
24
    result = pipelines.password_reset_confirm.serialize_request(**context)
25
    validated_data = result['serializer'].validated_data
26
27
    assert 'serializer' in result
28
    assert 'user' in validated_data
29
    assert 'new_password' in validated_data
30
    assert validated_data['user'] == test_user
31
    assert validated_data['new_password'] == request.data['new_password']
32
33
34
@pytest.mark.django_db(transaction=False)
35
def test_invalid_serialize_request_wrong_uid():
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_wrong_uid does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
36
    request = mock.MagicMock()
37
    request.data = {
38
        'uid': utils.encode_uid(1),
39
        'token': 'whatever',
40
        'new_password': 'whatever-again',
41
    }
42
    context = {'request': request}
43
    with pytest.raises(exceptions.ValidationError) as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
44
        pipelines.password_reset_confirm.serialize_request(**context)
45
46
    assert e.value.errors == {
47
        'non_field_errors': [constants.INVALID_UID_ERROR]
48
    }
49
50 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
@pytest.mark.django_db(transaction=False)
52
def test_invalid_serialize_request_not_existent_user(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_not_existent_user does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
53
    request = mock.MagicMock()
54
    request.data = {
55
        'uid': utils.encode_uid(test_user.pk + 1),
56
        'token': default_token_generator.make_token(test_user),
57
        'new_password': 'whatever-123',
58
    }
59
    context = {'request': request}
60
    with pytest.raises(exceptions.ValidationError) as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
61
        pipelines.password_reset_confirm.serialize_request(**context)
62
63
    assert e.value.errors == {
64
        'non_field_errors': [constants.INVALID_UID_ERROR]
65
    }
66
67
68
@pytest.mark.django_db(transaction=False)
69
def test_invalid_serialize_request_invalid_token(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_invalid_token does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
70
    request = mock.MagicMock()
71
    request.data = {
72
        'uid': utils.encode_uid(test_user.pk),
73
        'token': 'invalid-token',
74
        'new_password': 'whatever-123',
75
    }
76
    context = {'request': request}
77
    with pytest.raises(exceptions.ValidationError) as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
78
        pipelines.password_reset_confirm.serialize_request(**context)
79
80
    assert e.value.errors == {
81
        'non_field_errors': [constants.INVALID_TOKEN_ERROR]
82
    }
83
84
85 View Code Duplication
@pytest.mark.django_db(transaction=False)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
86
@override_settings(DJOSER=dict(settings.DJOSER, **{
87
    'SERIALIZERS': {
88
        'password_reset_confirm':
89
            'djoser.serializers.PasswordResetConfirmRetypeSerializer'
90
    }
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_retype_mismatch does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
91
}))
92
def test_invalid_serialize_request_retype_mismatch(test_user):
93
    request = mock.MagicMock()
94
    request.data = {
95
        'uid': utils.encode_uid(test_user.pk),
96
        'token': default_token_generator.make_token(test_user),
97
        'new_password': 'whatever-123',
98
        're_new_password': 'meh',
99
    }
100
    context = {'request': request}
101
    with pytest.raises(exceptions.ValidationError) as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
102
        pipelines.password_reset_confirm.serialize_request(**context)
103
    assert e.value.errors == {
104
        'non_field_errors': [constants.PASSWORD_MISMATCH_ERROR]
105
    }
106
107
108 View Code Duplication
@pytest.mark.django_db(transaction=False)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
@override_settings(DJOSER=dict(settings.DJOSER, **{
110
    'SERIALIZERS': {
111
        'password_reset_confirm':
112
            'djoser.serializers.PasswordResetConfirmSerializer'
113
    }
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_r...assword_validation_fail does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
114
}))
115
def test_invalid_serialize_request_password_validation_fail(test_user):
116
    request = mock.MagicMock()
117
    request.data = {
118
        'uid': utils.encode_uid(test_user.pk),
119
        'token': default_token_generator.make_token(test_user),
120
        'new_password': '666',
121
    }
122
    context = {'request': request}
123
    with pytest.raises(exceptions.ValidationError) as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
124
        pipelines.password_reset_confirm.serialize_request(**context)
125
126
    assert e.value.errors == {
127
        'new_password': ['Password 666 is not allowed.']
128
    }
129
130
131
@pytest.mark.django_db(transaction=False)
132
def test_valid_perform(test_user):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
133
    serializer = mock.MagicMock()
134
    serializer.validated_data = {
135
        'user': test_user,
136
        'new_password': 'cool-new-password123',
137
    }
138
    context = {'serializer': serializer}
139
    result = pipelines.password_reset_confirm.perform(**context)
140
141
    assert result['user'] == test_user
142
    assert test_user.check_password(serializer.validated_data['new_password'])
143
144
145
def test_valid_signal(test_user):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
146
    request = mock.MagicMock()
147
    context = {'request': request, 'user': test_user}
148
149
    with catch_signal(signals.password_reset_completed) as handler:
150
        pipelines.password_reset_confirm.signal(**context)
151
152
    handler.assert_called_once_with(
153
        sender=mock.ANY,
154
        signal=signals.password_reset_completed,
155
        user=test_user,
156
        request=request
157
    )
158
159
160
@pytest.mark.django_db(transaction=False)
161
def test_valid_pipeline(test_user):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
162
    request = mock.MagicMock()
163
    request.data = {
164
        'uid': utils.encode_uid(test_user.pk),
165
        'token': default_token_generator.make_token(test_user),
166
        'new_password': 'cool-new-password123',
167
    }
168
169
    steps = djoser_settings.PIPELINES.password_reset_confirm
170
    pipeline = pipelines.base.Pipeline(request, steps)
171
    with catch_signal(signals.password_reset_completed) as handler:
172
        result = pipeline.run()
173
174
    handler.assert_called_once_with(
175
        sender=mock.ANY,
176
        signal=signals.password_reset_completed,
177
        user=result['user'],
178
        request=request
179
    )
180
181
    test_user.refresh_from_db()
182
    assert result['user'] == test_user
183
    assert test_user.check_password(request.data['new_password'])
184