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

test_valid_perform()   B

Complexity

Conditions 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 15
Bugs 0 Features 0
Metric Value
cc 6
c 15
b 0
f 0
dl 0
loc 16
rs 8
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 pipelines, signals
6
from djoser.conf import settings
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(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.user = test_user
16
    request.data = {'email': 'new@localhost'}
17
    context = {'request': request}
18
    result = pipelines.user_update.serialize_request(**context)
19
20
    assert 'serializer' in result
21
    assert result['serializer'].validated_data == request.data
22
23
24
@pytest.mark.django_db(transaction=False)
25
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...
26
    request = mock.MagicMock()
27
    request.user = test_user
28
    old_email = test_user.email
29
30
    serializer = mock.MagicMock()
31
    serializer.validated_data = {'email': 'new@localhost'}
32
    context = {'request': request, 'serializer': serializer}
33
    result = pipelines.user_update.perform(**context)
34
35
    assert 'user' in result
36
    assert result['user'].username == test_user.username
37
    assert result['user'].email == serializer.validated_data['email']
38
    assert result['user'].email != old_email
39
    assert test_user.email == serializer.validated_data['email']
40
41
42
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...
43
    request = mock.MagicMock()
44
    context = {'request': request, 'user': test_user}
45
46
    with catch_signal(signals.user_updated) as handler:
47
        pipelines.user_update.signal(**context)
48
49
    handler.assert_called_once_with(
50
        sender=mock.ANY,
51
        signal=signals.user_updated,
52
        user=test_user,
53
        request=request
54
    )
55
56
57
@pytest.mark.django_db(transaction=False)
58
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...
59
    context = {'user': test_user}
60
    result = pipelines.user_update.serialize_instance(**context)
61
    username = getattr(test_user, User.USERNAME_FIELD)
62
63
    assert 'response_data' in result
64
    assert result['response_data'] == {
65
        'id': 1,
66
        'email': test_user.email,
67
        User.USERNAME_FIELD: username,
68
    }
69
70
71
@pytest.mark.django_db(transaction=False)
72
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...
73
    request = mock.MagicMock()
74
    request.user = test_user
75
    request.data = {'email': 'new@localhost'}
76
    username = getattr(test_user, User.USERNAME_FIELD)
77
78
    steps = settings.PIPELINES['user_update']
79
    pipeline = pipelines.base.Pipeline(request, steps)
80
    with catch_signal(signals.user_updated) as handler:
81
        result = pipeline.run()
82
83
    handler.assert_called_once_with(
84
        sender=mock.ANY,
85
        signal=signals.user_updated,
86
        user=result['user'],
87
        request=request
88
    )
89
90
    assert test_user.email == request.data['email']
91
    assert 'response_data' in result
92
    assert result['response_data'] == {
93
        'id': 1,
94
        'email': test_user.email,
95
        User.USERNAME_FIELD: username,
96
    }
97
98
99
# 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...
100