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

test_invalid_serialize_request_invalid_username()   B

Complexity

Conditions 5

Size

Total Lines 15

Duplication

Lines 14
Ratio 93.33 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 14
loc 15
rs 8.5454
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
5
from djoser import exceptions, pipelines, signals
6
from djoser.conf import settings as djoser_settings
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 View Code Duplication
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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
    request = mock.MagicMock()
15
    request.user = test_user
16
    request.data = {
17
        User.USERNAME_FIELD: 'new_username',
18
        'current_password': 'testing123',
19
    }
20
    context = {'request': request}
21
    result = pipelines.username_update.serialize_request(**context)
22
23
    assert 'serializer' in result
24
    assert result['serializer'].validated_data == {
25
        User.USERNAME_FIELD: 'new_username',
26
        'current_password': 'testing123',
27
    }
28
29
30
@pytest.mark.django_db(transaction=False)
31 View Code Duplication
def test_invalid_serialize_request_same_username(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_same_username 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...
32
    request = mock.MagicMock()
33
    request.user = test_user
34
    request.data = {
35
        User.USERNAME_FIELD: getattr(test_user, User.USERNAME_FIELD),
36
        'current_password': 'testing123',
37
    }
38
    context = {'request': request}
39
    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...
40
        pipelines.username_update.serialize_request(**context)
41
42
    assert e.value.errors == {
43
        'username': ['A user with that username already exists.']
44
    }
45
46
47
@pytest.mark.django_db(transaction=False)
48 View Code Duplication
def test_invalid_serialize_request_invalid_username(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_invalid_username 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...
49
    request = mock.MagicMock()
50
    request.user = test_user
51
    request.data = {
52
        User.USERNAME_FIELD: '$ lolwut #',
53
        'current_password': 'testing123',
54
    }
55
    context = {'request': request}
56
    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...
57
        pipelines.username_update.serialize_request(**context)
58
59
    assert 'username' in e.value.errors
60
    assert len(e.value.errors['username']) == 1
61
    assert 'Enter a valid username.' in e.value.errors['username'][0]
62
63
64 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...
65
def test_invalid_serialize_request_retype_mismatch(settings, test_user):
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...
66
    settings.DJOSER = dict(settings.DJOSER, **{
67
        'SERIALIZERS': {
68
            'username_update':
69
                'djoser.serializers.UsernameUpdateRetypeSerializer'
70
        }
71
    })
72
73
    request = mock.MagicMock()
74
    request.user = test_user
75
    request.data = {
76
        User.USERNAME_FIELD: 'new_username',
77
        're_' + User.USERNAME_FIELD: 'spanish_inquisition',
78
        'current_password': 'testing123',
79
    }
80
    context = {'request': request}
81
    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...
82
        pipelines.username_update.serialize_request(**context)
83
84
    assert e.value.errors == {
85
        'non_field_errors': ["The two username fields didn't match."]
86
    }
87
88
89
@pytest.mark.django_db(transaction=False)
90
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...
91
    request = mock.MagicMock()
92
    request.user = test_user
93
    serializer = mock.MagicMock()
94
    serializer.validated_data = {
95
        User.USERNAME_FIELD: 'new_username',
96
        'current_password': 'testing123',
97
    }
98
    context = {'request': request, 'serializer': serializer}
99
    result = pipelines.username_update.perform(**context)
100
101
    assert 'user' in result
102
    test_user.refresh_from_db()
103
    assert test_user == result['user']
104
    assert getattr(test_user, User.USERNAME_FIELD) == 'new_username'
105
106
107
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...
108
    request = mock.MagicMock()
109
    context = {'request': request, 'user': test_user}
110
111
    with catch_signal(signals.username_updated) as handler:
112
        pipelines.username_update.signal(**context)
113
114
    handler.assert_called_once_with(
115
        sender=mock.ANY,
116
        signal=signals.username_updated,
117
        user=test_user,
118
        request=request
119
    )
120
121
122
@pytest.mark.django_db(transaction=False)
123
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...
124
    request = mock.MagicMock()
125
    request.user = test_user
126
    request.data = {
127
        User.USERNAME_FIELD: 'new_username',
128
        'current_password': 'testing123',
129
    }
130
131
    steps = djoser_settings.PIPELINES['username_update']
132
    pipeline = pipelines.base.Pipeline(request, steps)
133
    with catch_signal(signals.username_updated) as handler:
134
        result = pipeline.run()
135
136
    handler.assert_called_once_with(
137
        sender=mock.ANY,
138
        signal=signals.username_updated,
139
        user=result['user'],
140
        request=request
141
    )
142