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

test_invalid_serialize_request_invalid_username()   B

Complexity

Conditions 5

Size

Total Lines 15

Duplication

Lines 13
Ratio 86.67 %

Importance

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