Passed
Pull Request — master (#259)
by Piotr
01:19
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.contrib.auth import get_user_model
4
from django.contrib.auth.tokens import default_token_generator
5
6
from djoser import constants, exceptions, pipelines, signals, utils
7
from djoser.conf import settings
8
from tests.common import catch_signal, mock
9
10
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...
11
12
13
@pytest.mark.django_db(transaction=False)
14
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...
15
    request = mock.MagicMock()
16
    request.data = {
17
        'uid': utils.encode_uid(test_user.pk),
18
        'token': default_token_generator.make_token(test_user),
19
        'new_password': 'cool-new-password123',
20
    }
21
    context = {'request': request}
22
    result = pipelines.password_reset_confirm.serialize_request(**context)
23
    validated_data = result['serializer'].validated_data
24
25
    assert 'serializer' in result
26
    assert 'user' in validated_data
27
    assert 'new_password' in validated_data
28
    assert validated_data['user'] == test_user
29
    assert validated_data['new_password'] == request.data['new_password']
30
31
32
@pytest.mark.django_db(transaction=False)
33
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...
34
    request = mock.MagicMock()
35
    request.data = {
36
        'uid': utils.encode_uid(1),
37
        'token': 'whatever',
38
        'new_password': 'whatever-again',
39
    }
40
    context = {'request': request}
41
    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...
42
        pipelines.password_reset_confirm.serialize_request(**context)
43
44
    assert e.value.errors == {
45
        'non_field_errors': [constants.INVALID_UID_ERROR]
46
    }
47
48
49
@pytest.mark.django_db(transaction=False)
50 View Code Duplication
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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
    request = mock.MagicMock()
52
    request.data = {
53
        'uid': utils.encode_uid(test_user.pk + 1),
54
        'token': default_token_generator.make_token(test_user),
55
        'new_password': 'whatever-123',
56
    }
57
    context = {'request': request}
58
    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...
59
        pipelines.password_reset_confirm.serialize_request(**context)
60
61
    assert e.value.errors == {
62
        'non_field_errors': [constants.INVALID_UID_ERROR]
63
    }
64
65
66
@pytest.mark.django_db(transaction=False)
67
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...
68
    request = mock.MagicMock()
69
    request.data = {
70
        'uid': utils.encode_uid(test_user.pk),
71
        'token': 'invalid-token',
72
        'new_password': 'whatever-123',
73
    }
74
    context = {'request': request}
75
    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...
76
        pipelines.password_reset_confirm.serialize_request(**context)
77
78
    assert e.value.errors == {
79
        'non_field_errors': [constants.INVALID_TOKEN_ERROR]
80
    }
81
82 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
83
@pytest.mark.django_db(transaction=False)
84
def test_invalid_serialize_request_retype_mismatch(test_user, settings):
0 ignored issues
show
Comprehensibility Bug introduced by
settings is re-defining a name which is already available in the outer-scope (previously defined on line 7).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
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...
85
    settings.DJOSER = dict(
86
        settings.DJOSER, **{'PASSWORD_RESET_CONFIRM_REQUIRE_RETYPE': True}
87
    )
88
    request = mock.MagicMock()
89
    request.data = {
90
        'uid': utils.encode_uid(test_user.pk),
91
        'token': default_token_generator.make_token(test_user),
92
        'new_password': 'whatever-123',
93
        're_new_password': 'meh',
94
    }
95
    context = {'request': request}
96
    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...
97
        pipelines.password_reset_confirm.serialize_request(**context)
98
99
    assert e.value.errors == {
100
        'non_field_errors': [constants.PASSWORD_MISMATCH_ERROR]
101
    }
102
103
104
@pytest.mark.django_db(transaction=False)
105
def test_invalid_serialize_request_password_validation_fail(test_user):
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...
106
    request = mock.MagicMock()
107
    request.data = {
108
        'uid': utils.encode_uid(test_user.pk),
109
        'token': default_token_generator.make_token(test_user),
110
        'new_password': '666',
111
    }
112
    context = {'request': request}
113
    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...
114
        pipelines.password_reset_confirm.serialize_request(**context)
115
116
    assert e.value.errors == {
117
        'new_password': ['Password 666 is not allowed.']
118
    }
119
120
121
@pytest.mark.django_db(transaction=False)
122
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...
123
    serializer = mock.MagicMock()
124
    serializer.validated_data = {
125
        'user': test_user,
126
        'new_password': 'cool-new-password123',
127
    }
128
    context = {'serializer': serializer}
129
    result = pipelines.password_reset_confirm.perform(**context)
130
131
    assert result['user'] == test_user
132
    assert test_user.check_password(serializer.validated_data['new_password'])
133
134
135
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...
136
    request = mock.MagicMock()
137
    context = {'request': request, 'user': test_user}
138
139
    with catch_signal(signals.password_reset_completed) as handler:
140
        pipelines.password_reset_confirm.signal(**context)
141
142
    handler.assert_called_once_with(
143
        sender=mock.ANY,
144
        signal=signals.password_reset_completed,
145
        user=test_user,
146
        request=request
147
    )
148
149
150 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...
151
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...
152
    request = mock.MagicMock()
153
    request.data = {
154
        'uid': utils.encode_uid(test_user.pk),
155
        'token': default_token_generator.make_token(test_user),
156
        'new_password': 'cool-new-password123',
157
    }
158
159
    steps = settings.PIPELINES.password_reset_confirm
160
    pipeline = pipelines.base.Pipeline(request, steps)
161
    with catch_signal(signals.password_reset_completed) as handler:
162
        result = pipeline.run()
163
164
    handler.assert_called_once_with(
165
        sender=mock.ANY,
166
        signal=signals.password_reset_completed,
167
        user=result['user'],
168
        request=request
169
    )
170
171
    test_user.refresh_from_db()
172
    assert result['user'] == test_user
173
    assert test_user.check_password(request.data['new_password'])
174