Completed
Pull Request — master (#259)
by Piotr
02:26
created

test_invalid_serialize_request_same_username()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

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