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

test_invalid_serialize_request_not_existent_user()   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(
24
        request, context
25
    )
26
    validated_data = result['serializer'].validated_data
27
28
    assert 'serializer' in result
29
    assert 'user' in validated_data
30
    assert 'new_password' in validated_data
31
    assert validated_data['user'] == test_user
32
    assert validated_data['new_password'] == request.data['new_password']
33
34
35
@pytest.mark.django_db(transaction=False)
36
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...
37
    request = mock.MagicMock()
38
    request.data = {
39
        'uid': utils.encode_uid(1),
40
        'token': 'whatever',
41
        'new_password': 'whatever-again',
42
    }
43
    context = {'request': request}
44
    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...
45
        pipelines.password_reset_confirm.serialize_request(request, context)
46
47
    assert e.value.errors == {
48
        'non_field_errors': [constants.INVALID_UID_ERROR]
49
    }
50
51
52
@pytest.mark.django_db(transaction=False)
53
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...
54
    request = mock.MagicMock()
55
    request.data = {
56
        'uid': utils.encode_uid(test_user.pk + 1),
57
        'token': default_token_generator.make_token(test_user),
58
        'new_password': 'whatever-123',
59
    }
60
    context = {'request': request}
61
    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...
62
        pipelines.password_reset_confirm.serialize_request(request, context)
63
64
    assert e.value.errors == {
65
        'non_field_errors': [constants.INVALID_UID_ERROR]
66
    }
67
68
69
@pytest.mark.django_db(transaction=False)
70
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...
71
    request = mock.MagicMock()
72
    request.data = {
73
        'uid': utils.encode_uid(test_user.pk),
74
        'token': 'invalid-token',
75
        'new_password': 'whatever-123',
76
    }
77
    context = {'request': request}
78
    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...
79
        pipelines.password_reset_confirm.serialize_request(request, context)
80
81
    assert e.value.errors == {
82
        'non_field_errors': [constants.INVALID_TOKEN_ERROR]
83
    }
84
85
86 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...
87
@override_settings(DJOSER=dict(
88
    settings.DJOSER, **{'PASSWORD_RESET_CONFIRM_REQUIRE_RETYPE': True}
89
))
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...
90
def test_invalid_serialize_request_password_retype_mismatch(test_user):
91
    request = mock.MagicMock()
92
    request.data = {
93
        'uid': utils.encode_uid(test_user.pk),
94
        'token': default_token_generator.make_token(test_user),
95
        'new_password': 'whatever-123',
96
        're_new_password': 'meh',
97
    }
98
    context = {'request': request}
99
    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...
100
        pipelines.password_reset_confirm.serialize_request(request, context)
101
102
    assert e.value.errors == {
103
        'non_field_errors': [constants.PASSWORD_MISMATCH_ERROR]
104
    }
105
106
107
@pytest.mark.django_db(transaction=False)
108
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...
109
    request = mock.MagicMock()
110
    request.data = {
111
        'uid': utils.encode_uid(test_user.pk),
112
        'token': default_token_generator.make_token(test_user),
113
        'new_password': '666',
114
    }
115
    context = {'request': request}
116
    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...
117
        pipelines.password_reset_confirm.serialize_request(request, context)
118
119
    assert e.value.errors == {
120
        'new_password': ['Password 666 is not allowed.']
121
    }
122
123
124
@pytest.mark.django_db(transaction=False)
125
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...
126
    serializer = mock.MagicMock()
127
    serializer.validated_data = {
128
        'user': test_user,
129
        'new_password': 'cool-new-password123',
130
    }
131
    context = {'serializer': serializer}
132
    result = pipelines.password_reset_confirm.perform(None, context)
133
134
    assert result['user'] == test_user
135
    assert test_user.check_password(serializer.validated_data['new_password'])
136
137
138
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...
139
    request = mock.MagicMock()
140
    context = {'user': test_user}
141
142
    with catch_signal(signals.password_reset_completed) as handler:
143
        pipelines.password_reset_confirm.signal(request, context)
144
145
    handler.assert_called_once_with(
146
        sender=mock.ANY,
147
        signal=signals.password_reset_completed,
148
        user=test_user,
149
        request=request
150
    )
151