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