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

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