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

test_invalid_serialize_request_user_not_active()   A

Complexity

Conditions 3

Size

Total Lines 13

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