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

test_failed_serialize_request_duplicate_username()   A

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 10
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.contrib.auth import authenticate, get_user_model
4
5
from djoser import constants, exceptions, pipelines, signals
6
from djoser.conf import 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 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
@pytest.mark.django_db(transaction=False)
13
def test_valid_serialize_request():
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...
14
    request = mock.MagicMock()
15
    request.data = {User.USERNAME_FIELD: 'test', 'password': 'testing123'}
16
    context = {'request': request}
17
    result = pipelines.user_create.serialize_request(**context)
18
19
    expected_data = {
20
        User.USERNAME_FIELD: request.data[User.USERNAME_FIELD],
21
        'password': request.data['password']
22
    }
23
    assert User.objects.count() == 0
24
    assert 'serializer' in result
25
    assert result['serializer'].validated_data == expected_data
26
27
28
@pytest.mark.django_db(transaction=False)
29
def test_failed_serialize_request_password_validation():
0 ignored issues
show
Coding Style Naming introduced by
The name test_failed_serialize_request_password_validation 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...
30
    request = mock.MagicMock()
31
    request.data = {User.USERNAME_FIELD: 'test', 'password': '666'}
32
    context = {'request': request}
33
34
    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...
35
        pipelines.user_create.serialize_request(**context)
36
37
    assert e.value.errors == {'password': ['Password 666 is not allowed.']}
38
39
40
@pytest.mark.django_db(transaction=False)
41
def test_failed_serialize_request_duplicate_username(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_failed_serialize_request_duplicate_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...
Unused Code introduced by
The argument test_user seems to be unused.
Loading history...
42
    request = mock.MagicMock()
43
    request.data = {User.USERNAME_FIELD: 'test', 'password': 'testing123'}
44
    context = {'request': request}
45
46
    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...
47
        pipelines.user_create.serialize_request(**context)
48
49
    assert e.value.errors == {
50
        'username': ['A user with that username already exists.']
51
    }
52
53
54
@pytest.mark.django_db(transaction=False)
55
def test_valid_perform():
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...
56
    serializer = mock.MagicMock()
57
    serializer.validated_data = {
58
        User.USERNAME_FIELD: 'test',
59
        'password': 'testing123'
60
    }
61
    context = {'serializer': serializer}
62
    result = pipelines.user_create.perform(**context)
63
64
    assert User.objects.count() == 1
65
    assert 'user' in result
66
67
    username = getattr(result['user'], User.USERNAME_FIELD)
68
    assert username == serializer.validated_data[User.USERNAME_FIELD]
69
    assert result['user'].is_active is True
70
    assert authenticate(**{
71
        User.USERNAME_FIELD: serializer.validated_data[User.USERNAME_FIELD],
72
        'password': serializer.validated_data['password'],
73
    }) == result['user']
74
75
76
def test_failed_perform_user_already_exists(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_failed_perform_user_already_exists 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...
Unused Code introduced by
The argument test_user seems to be unused.
Loading history...
77
    serializer = mock.MagicMock()
78
    serializer.validated_data = {
79
        User.USERNAME_FIELD: 'test',
80
        'password': 'testing123'
81
    }
82
    context = {'serializer': serializer}
83
    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...
84
        pipelines.user_create.perform(**context)
85
86
    assert e.value.errors == constants.CANNOT_CREATE_USER_ERROR
87
88
89
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...
90
    request = mock.MagicMock()
91
    context = {'request': request, 'user': test_user}
92
93
    with catch_signal(signals.user_created) as handler:
94
        pipelines.user_create.signal(**context)
95
96
    handler.assert_called_once_with(
97
        sender=mock.ANY,
98
        signal=signals.user_created,
99
        user=test_user,
100
        request=request
101
    )
102
103
104
def test_valid_serialize_instance(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
    context = {'user': test_user}
106
    result = pipelines.user_create.serialize_instance(**context)
107
    username = getattr(test_user, User.USERNAME_FIELD)
108
109
    assert 'response_data' in result
110
    assert result['response_data'] == {User.USERNAME_FIELD: username, 'id': 1}
111
112
113 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...
114
def test_valid_pipeline():
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...
115
    request = mock.MagicMock()
116
    request.data = {User.USERNAME_FIELD: 'test', 'password': 'testing123'}
117
118
    steps = settings.PIPELINES.user_create
119
    pipeline = pipelines.base.Pipeline(request, steps)
120
    with catch_signal(signals.user_created) as handler:
121
        result = pipeline.run()
122
123
    handler.assert_called_once_with(
124
        sender=mock.ANY,
125
        signal=signals.user_created,
126
        user=result['user'],
127
        request=request
128
    )
129
130
    assert 'response_data' in result
131
    assert result['response_data'] == {
132
        User.USERNAME_FIELD: request.data[User.USERNAME_FIELD], 'id': 1
133
    }
134