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

test_valid_pipeline()   B

Complexity

Conditions 5

Size

Total Lines 21

Duplication

Lines 20
Ratio 95.24 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
cc 5
c 13
b 0
f 0
dl 20
loc 21
rs 8.439
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
5
from djoser import 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.user = test_user
15
    request.data = {'email': 'new@localhost'}
16
    context = {'request': request}
17
    result = pipelines.user_update.serialize_request(**context)
18
19
    assert 'serializer' in result
20
    assert result['serializer'].validated_data == request.data
21
22
23
@pytest.mark.django_db(transaction=False)
24
def test_valid_perform(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...
25
    request = mock.MagicMock()
26
    request.user = test_user
27
    old_email = test_user.email
28
29
    serializer = mock.MagicMock()
30
    serializer.validated_data = {'email': 'new@localhost'}
31
    context = {'request': request, 'serializer': serializer}
32
    result = pipelines.user_update.perform(**context)
33
34
    assert 'user' in result
35
    assert result['user'].username == test_user.username
36
    assert result['user'].email == serializer.validated_data['email']
37
    assert result['user'].email != old_email
38
    assert test_user.email == serializer.validated_data['email']
39
40
41
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...
42
    request = mock.MagicMock()
43
    context = {'request': request, 'user': test_user}
44
45
    with catch_signal(signals.user_updated) as handler:
46
        pipelines.user_update.signal(**context)
47
48
    handler.assert_called_once_with(
49
        sender=mock.ANY,
50
        signal=signals.user_updated,
51
        user=test_user,
52
        request=request
53
    )
54
55
56
@pytest.mark.django_db(transaction=False)
57
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...
58
    context = {'user': test_user}
59
    result = pipelines.user_update.serialize_instance(**context)
60
    username = getattr(test_user, User.USERNAME_FIELD)
61
62
    assert 'response_data' in result
63
    assert result['response_data'] == {
64
        'id': 1,
65
        'email': test_user.email,
66
        User.USERNAME_FIELD: username,
67
    }
68
69 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
70
@pytest.mark.django_db(transaction=False)
71
def test_valid_pipeline(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
    request.user = test_user
74
    request.data = {'email': 'new@localhost'}
75
    username = getattr(test_user, User.USERNAME_FIELD)
76
77
    pipeline = pipelines.user_update.Pipeline(request)
78
    with catch_signal(signals.user_updated) as handler:
79
        result = pipeline.run()
80
81
    handler.assert_called_once_with(
82
        sender=mock.ANY,
83
        signal=signals.user_updated,
84
        user=result['user'],
85
        request=request
86
    )
87
88
    assert test_user.email == request.data['email']
89
    assert 'response_data' in result
90
    assert result['response_data'] == {
91
        'id': 1,
92
        'email': test_user.email,
93
        User.USERNAME_FIELD: username,
94
    }
95
96
97
# TODO: test errors, test FK, test m2m
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
98