Completed
Pull Request — master (#259)
by Piotr
02:26
created

test_valid_signal()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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