Passed
Pull Request — master (#259)
by Piotr
03:28
created

test_invalid_serialize_request_wrong_uid()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 12
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
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth'
Loading history...
4
from django.contrib.auth.tokens import default_token_generator
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth.tokens'
Loading history...
5
6
from djoser import constants, exceptions, pipelines, signals, utils
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
12
@pytest.mark.django_db(transaction=False)
13
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...
14
    request = mock.MagicMock()
15
    request.data = {
16
        'uid': utils.encode_uid(inactive_test_user.pk),
17
        'token': default_token_generator.make_token(inactive_test_user)
18
    }
19
    context = {'request': request}
20
    result = pipelines.user_activate.serialize_request(**context)
21
22
    assert 'serializer' in result
23
    assert 'user' in result['serializer'].validated_data
24
    assert result['serializer'].validated_data['user'] == inactive_test_user
25
26
27
@pytest.mark.django_db(transaction=False)
28
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...
29
    request = mock.MagicMock()
30
    request.data = {
31
        'uid': utils.encode_uid(1),
32
        'token': 'whatever',
33
    }
34
    context = {'request': request}
35
    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...
36
        pipelines.user_activate.serialize_request(**context)
37
38
    assert e.value.errors == {
39
        'non_field_errors': [constants.INVALID_UID_ERROR]
40
    }
41
42
43
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...
44
    request = mock.MagicMock()
45
    request.data = {
46
        'uid': utils.encode_uid(test_user.pk),
47
        'token': default_token_generator.make_token(test_user),
48
    }
49
    context = {'request': request}
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.user_activate.serialize_request(**context)
52
53
    assert e.value.errors == {
54
        'non_field_errors': ['Stale token for given user.']
55
    }
56
57
58
@pytest.mark.django_db(transaction=False)
59
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...
60
    serializer = mock.MagicMock()
61
    serializer.validated_data = {'user': inactive_test_user}
62
    context = {'serializer': serializer}
63
64
    assert inactive_test_user.is_active is False
65
    result = pipelines.user_activate.perform(**context)
66
    assert inactive_test_user.is_active is True
67
    assert result['user'] == inactive_test_user
68
69
70
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...
71
    request = mock.MagicMock()
72
    context = {'request': request, 'user': test_user}
73
74
    with catch_signal(signals.user_activated) as handler:
75
        pipelines.user_activate.signal(**context)
76
77
    handler.assert_called_once_with(
78
        sender=mock.ANY,
79
        signal=signals.user_activated,
80
        user=test_user,
81
        request=request
82 View Code Duplication
    )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
83
84
85
@pytest.mark.django_db(transaction=False)
86
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...
87
    request = mock.MagicMock()
88
    request.data = {
89
        'uid': utils.encode_uid(inactive_test_user.pk),
90
        'token': default_token_generator.make_token(inactive_test_user)
91
    }
92
93
    pipeline = pipelines.user_activate.Pipeline(request)
94
    with catch_signal(signals.user_activated) as handler:
95
        result = pipeline.run()
96
97
    handler.assert_called_once_with(
98
        sender=mock.ANY,
99
        signal=signals.user_activated,
100
        user=result['user'],
101
        request=request
102
    )
103
104
    assert inactive_test_user.is_active is False
105
    inactive_test_user.refresh_from_db()
106
    assert inactive_test_user.is_active is True
107