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

test_invalid_serialize_request_stale_token()   A

Complexity

Conditions 3

Size

Total Lines 11

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 11
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
from django.contrib.auth.tokens import default_token_generator
5
6
from djoser import constants, exceptions, pipelines, signals, utils
7
from djoser.conf import settings
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(inactive_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
        'uid': utils.encode_uid(inactive_test_user.pk),
18
        'token': default_token_generator.make_token(inactive_test_user)
19
    }
20
    context = {'request': request}
21
    result = pipelines.user_activate.serialize_request(**context)
22
23
    assert 'serializer' in result
24
    assert 'user' in result['serializer'].validated_data
25
    assert result['serializer'].validated_data['user'] == inactive_test_user
26
27
28
@pytest.mark.django_db(transaction=False)
29
def test_invalid_serialize_request_wrong_uid():
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_wrong_uid 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 = {
32
        'uid': utils.encode_uid(1),
33
        'token': 'whatever',
34
    }
35
    context = {'request': request}
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.user_activate.serialize_request(**context)
38
39
    assert e.value.errors == {
40
        'non_field_errors': [constants.INVALID_UID_ERROR]
41
    }
42
43
44
def test_invalid_serialize_request_stale_token(test_user):
0 ignored issues
show
Coding Style Naming introduced by
The name test_invalid_serialize_request_stale_token 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...
45
    request = mock.MagicMock()
46
    request.data = {
47
        'uid': utils.encode_uid(test_user.pk),
48
        'token': default_token_generator.make_token(test_user),
49
    }
50
    context = {'request': request}
51
    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...
52
        pipelines.user_activate.serialize_request(**context)
53
54
    assert e.value.errors == {
55
        'non_field_errors': ['Stale token for given user.']
56
    }
57
58
59
@pytest.mark.django_db(transaction=False)
60
def test_valid_perform(inactive_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...
61
    serializer = mock.MagicMock()
62
    serializer.validated_data = {'user': inactive_test_user}
63
    context = {'serializer': serializer}
64
65
    assert inactive_test_user.is_active is False
66
    result = pipelines.user_activate.perform(**context)
67
    assert inactive_test_user.is_active is True
68
    assert result['user'] == inactive_test_user
69
70
71
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...
72
    request = mock.MagicMock()
73
    context = {'request': request, 'user': test_user}
74
75
    with catch_signal(signals.user_activated) as handler:
76
        pipelines.user_activate.signal(**context)
77
78
    handler.assert_called_once_with(
79
        sender=mock.ANY,
80
        signal=signals.user_activated,
81
        user=test_user,
82
        request=request
83
    )
84
85
86 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...
87
def test_valid_pipeline(inactive_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.data = {
90
        'uid': utils.encode_uid(inactive_test_user.pk),
91
        'token': default_token_generator.make_token(inactive_test_user)
92
    }
93
94
    steps = settings.PIPELINES.user_activate
95
    pipeline = pipelines.base.Pipeline(request, steps)
96
    with catch_signal(signals.user_activated) as handler:
97
        result = pipeline.run()
98
99
    handler.assert_called_once_with(
100
        sender=mock.ANY,
101
        signal=signals.user_activated,
102
        user=result['user'],
103
        request=request
104
    )
105
106
    assert inactive_test_user.is_active is False
107
    inactive_test_user.refresh_from_db()
108
    assert inactive_test_user.is_active is True
109