Passed
Pull Request — master (#259)
by Piotr
03:22
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
0 ignored issues
show
introduced by
Unable to import 'django.conf'
Loading history...
4
from django.contrib.auth import get_user_model
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth'
Loading history...
5
from django.contrib.auth.tokens import default_token_generator
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth.tokens'
Loading history...
6
from django.test.utils import override_settings
0 ignored issues
show
introduced by
Unable to import 'django.test.utils'
Loading history...
7
8
from djoser import constants, exceptions, pipelines, signals, utils
9
from tests.common import catch_signal, mock
10
11
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...
12
13
14
@pytest.mark.django_db(transaction=False)
15
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...
16
    request = mock.MagicMock()
17
    request.data = {
18
        'uid': utils.encode_uid(test_user.pk),
19
        'token': default_token_generator.make_token(test_user),
20
        'new_password': 'cool-new-password123',
21
    }
22
    context = {'request': request}
23
    result = pipelines.password_reset_confirm.serialize_request(**context)
24
    validated_data = result['serializer'].validated_data
25
26
    assert 'serializer' in result
27
    assert 'user' in validated_data
28
    assert 'new_password' in validated_data
29
    assert validated_data['user'] == test_user
30
    assert validated_data['new_password'] == request.data['new_password']
31
32
33
@pytest.mark.django_db(transaction=False)
34
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...
35
    request = mock.MagicMock()
36
    request.data = {
37
        'uid': utils.encode_uid(1),
38
        'token': 'whatever',
39
        'new_password': 'whatever-again',
40
    }
41
    context = {'request': request}
42
    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...
43
        pipelines.password_reset_confirm.serialize_request(**context)
44
45
    assert e.value.errors == {
46
        'non_field_errors': [constants.INVALID_UID_ERROR]
47
    }
48
49
50 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...
51
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...
52
    request = mock.MagicMock()
53
    request.data = {
54
        'uid': utils.encode_uid(test_user.pk + 1),
55
        'token': default_token_generator.make_token(test_user),
56
        'new_password': 'whatever-123',
57
    }
58
    context = {'request': request}
59
    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...
60
        pipelines.password_reset_confirm.serialize_request(**context)
61
62
    assert e.value.errors == {
63
        'non_field_errors': [constants.INVALID_UID_ERROR]
64
    }
65
66
67
@pytest.mark.django_db(transaction=False)
68
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...
69
    request = mock.MagicMock()
70
    request.data = {
71
        'uid': utils.encode_uid(test_user.pk),
72
        'token': 'invalid-token',
73
        'new_password': 'whatever-123',
74
    }
75
    context = {'request': request}
76
    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...
77
        pipelines.password_reset_confirm.serialize_request(**context)
78
79
    assert e.value.errors == {
80
        'non_field_errors': [constants.INVALID_TOKEN_ERROR]
81
    }
82
83
84 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...
85
@override_settings(DJOSER=dict(
86
    settings.DJOSER, **{'PASSWORD_RESET_CONFIRM_REQUIRE_RETYPE': True}
87
))
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_r...assword_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...
88
def test_invalid_serialize_request_password_retype_mismatch(test_user):
89
    request = mock.MagicMock()
90
    request.data = {
91
        'uid': utils.encode_uid(test_user.pk),
92
        'token': default_token_generator.make_token(test_user),
93
        'new_password': 'whatever-123',
94
        're_new_password': 'meh',
95
    }
96
    context = {'request': request}
97
    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...
98
        pipelines.password_reset_confirm.serialize_request(**context)
99
100
    assert e.value.errors == {
101
        'non_field_errors': [constants.PASSWORD_MISMATCH_ERROR]
102
    }
103
104
105
@pytest.mark.django_db(transaction=False)
106
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...
107
    request = mock.MagicMock()
108
    request.data = {
109
        'uid': utils.encode_uid(test_user.pk),
110
        'token': default_token_generator.make_token(test_user),
111
        'new_password': '666',
112
    }
113
    context = {'request': request}
114
    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...
115
        pipelines.password_reset_confirm.serialize_request(**context)
116
117
    assert e.value.errors == {
118
        'new_password': ['Password 666 is not allowed.']
119
    }
120
121
122
@pytest.mark.django_db(transaction=False)
123
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...
124
    serializer = mock.MagicMock()
125
    serializer.validated_data = {
126
        'user': test_user,
127
        'new_password': 'cool-new-password123',
128
    }
129
    context = {'serializer': serializer}
130
    result = pipelines.password_reset_confirm.perform(**context)
131
132
    assert result['user'] == test_user
133
    assert test_user.check_password(serializer.validated_data['new_password'])
134
135
136
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...
137
    request = mock.MagicMock()
138
    context = {'request': request, 'user': test_user}
139
140
    with catch_signal(signals.password_reset_completed) as handler:
141
        pipelines.password_reset_confirm.signal(**context)
142
143
    handler.assert_called_once_with(
144
        sender=mock.ANY,
145
        signal=signals.password_reset_completed,
146
        user=test_user,
147
        request=request
148
    )
149
150
151
@pytest.mark.django_db(transaction=False)
152
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...
153
    request = mock.MagicMock()
154
    request.data = {
155
        'uid': utils.encode_uid(test_user.pk),
156
        'token': default_token_generator.make_token(test_user),
157
        'new_password': 'cool-new-password123',
158
    }
159
160
    pipeline = pipelines.password_reset_confirm.Pipeline(request)
161
    result = pipeline.run()
162
163
    test_user.refresh_from_db()
164
    assert result['user'] == test_user
165
    assert test_user.check_password(request.data['new_password'])
166