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

test_invalid_serialize_request_not_existent_user()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 11
Ratio 84.62 %

Importance

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