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

test_invalid_serialize_request_empty_request()   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.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 constants, 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.data = {
17
        User.USERNAME_FIELD: getattr(test_user, User.USERNAME_FIELD),
18
        'password': 'testing123',
19
    }
20
    context = {'request': request}
21
    result = pipelines.token_create.serialize_request(**context)
22
23
    assert 'serializer' in result
24
    assert 'user' in result['serializer'].validated_data
25
26
27
@pytest.mark.django_db(transaction=False)
28
def test_invalid_serialize_request_invalid_credentials(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_invalid_credentials 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 = {
31
        User.USERNAME_FIELD: getattr(test_user, User.USERNAME_FIELD),
32
        'password': 'wrong-credentials',
33
    }
34
    context = {'request': request}
35
36
    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...
37
        pipelines.token_create.serialize_request(**context)
38
39
    assert e.value.errors == {
40
        'non_field_errors': [constants.INVALID_CREDENTIALS_ERROR]
41
    }
42
43
44
@pytest.mark.django_db(transaction=False)
45
def test_invalid_serialize_request_empty_request(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_empty_request 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...
46
    request = mock.MagicMock()
47
    request.data = {}
48
    context = {'request': request}
49
50
    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...
51
        pipelines.token_create.serialize_request(**context)
52
53
    assert e.value.errors == {
54
        'non_field_errors': [constants.INVALID_CREDENTIALS_ERROR]
55
    }
56
57
58
@pytest.mark.django_db(transaction=False)
59
def test_invalid_serialize_request_user_not_active(inactive_test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_user_not_active 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...
60
    request = mock.MagicMock()
61
    request.data = {
62
        User.USERNAME_FIELD: getattr(inactive_test_user, User.USERNAME_FIELD),
63
        'password': 'testing123',
64
    }
65
    context = {'request': request}
66
67
    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...
68
        pipelines.token_create.serialize_request(**context)
69
70
    assert e.value.errors == {
71
        'non_field_errors': [constants.INVALID_CREDENTIALS_ERROR]
72
    }
73
74
75 View Code Duplication
@override_settings(DJOSER=dict(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
76
    settings.DJOSER,
77
    **{'TOKEN_MODEL': 'rest_framework.authtoken.models.Token'}
78
))
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...
79
def test_valid_perform(test_user):
80
    from djoser.conf import settings as djoser_settings
81
82
    serializer = mock.MagicMock()
83
    serializer.validated_data = {'user': test_user}
84
    context = {'serializer': serializer}
85
86
    result = pipelines.token_create.perform(**context)
87
    assert result['user'] == test_user
88
    assert djoser_settings.TOKEN_MODEL.objects.count() == 1
89
    assert djoser_settings.TOKEN_MODEL.objects.first().user == test_user
90
91
92
@pytest.mark.django_db(transaction=False)
93
def test_invalid_perform_none_token_model(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_perform_none_token_model 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...
94
    serializer = mock.MagicMock()
95
    serializer.validated_data = {'user': test_user}
96
    context = {'serializer': serializer}
97
98
    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...
99
        pipelines.token_create.perform(**context)
100
101
    assert str(e.value) == constants.TOKEN_MODEL_NONE_ERROR
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 = {'request': request, 'user': test_user}
107
108
    with catch_signal(signals.token_created) as handler:
109
        pipelines.token_create.signal(**context)
110
111
    handler.assert_called_once_with(
112
        sender=mock.ANY,
113
        signal=signals.token_created,
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
@override_settings(DJOSER=dict(
121
    settings.DJOSER,
122
    **{'TOKEN_MODEL': 'rest_framework.authtoken.models.Token'}
123
))
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...
124
def test_valid_pipeline(test_user):
125
    from djoser.conf import settings as djoser_settings
126
127
    request = mock.MagicMock()
128
    request.data = {
129
        User.USERNAME_FIELD: getattr(test_user, User.USERNAME_FIELD),
130
        'password': 'testing123',
131
    }
132
133
    pipeline = pipelines.token_create.Pipeline(request)
134
    with catch_signal(signals.token_created) as handler:
135
        result = pipeline.run()
136
137
    handler.assert_called_once_with(
138
        sender=mock.ANY,
139
        signal=signals.token_created,
140
        user=result['user'],
141
        request=request
142
    )
143
144
    assert result['user'] == test_user
145
    assert djoser_settings.TOKEN_MODEL.objects.count() == 1
146
    assert djoser_settings.TOKEN_MODEL.objects.first().user == test_user
147