Passed
Pull Request — master (#259)
by Piotr
01:19
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
from djoser.conf import settings
5
6
from djoser import exceptions, pipelines, signals
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 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
64
@pytest.mark.django_db(transaction=False)
65
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 4).

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...
66
    settings.DJOSER = dict(
67
        settings.DJOSER, **{'USERNAME_UPDATE_REQUIRE_RETYPE': True}
68
    )
69
    request = mock.MagicMock()
70
    request.user = test_user
71
    request.data = {
72
        User.USERNAME_FIELD: 'new_username',
73
        're_' + User.USERNAME_FIELD: 'spanish_inquisition',
74
        'current_password': 'testing123',
75
    }
76
    context = {'request': request}
77
    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...
78
        pipelines.username_update.serialize_request(**context)
79
80
    assert e.value.errors == {
81
        'non_field_errors': ["The two username fields didn't match."]
82
    }
83
84
85 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...
86
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...
87
    request = mock.MagicMock()
88
    request.user = test_user
89
    serializer = mock.MagicMock()
90
    serializer.validated_data = {
91
        User.USERNAME_FIELD: 'new_username',
92
        'current_password': 'testing123',
93
    }
94
    context = {'request': request, 'serializer': serializer}
95
    result = pipelines.username_update.perform(**context)
96
97
    assert 'user' in result
98
    test_user.refresh_from_db()
99
    assert test_user == result['user']
100
    assert getattr(test_user, User.USERNAME_FIELD) == 'new_username'
101
102
103
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...
104
    request = mock.MagicMock()
105
    context = {'request': request, 'user': test_user}
106
107
    with catch_signal(signals.username_updated) as handler:
108
        pipelines.username_update.signal(**context)
109
110
    handler.assert_called_once_with(
111
        sender=mock.ANY,
112
        signal=signals.username_updated,
113
        user=test_user,
114
        request=request
115
    )
116
117
118
@pytest.mark.django_db(transaction=False)
119
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...
120
    request = mock.MagicMock()
121
    request.user = test_user
122
    request.data = {
123
        User.USERNAME_FIELD: 'new_username',
124
        'current_password': 'testing123',
125
    }
126
127
    steps = settings.PIPELINES.username_update
128
    pipeline = pipelines.base.Pipeline(request, steps)
129
    with catch_signal(signals.username_updated) as handler:
130
        result = pipeline.run()
131
132
    handler.assert_called_once_with(
133
        sender=mock.ANY,
134
        signal=signals.username_updated,
135
        user=result['user'],
136
        request=request
137
    )
138