test_serializers.py 3.78 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import django_filters

from funkwhale_api.common import serializers
from funkwhale_api.users import models


class TestActionFilterSet(django_filters.FilterSet):
    class Meta:
        model = models.User
        fields = ['is_active']


class TestSerializer(serializers.ActionSerializer):
    actions = ['test']
    filterset_class = TestActionFilterSet

    def handle_test(self, objects):
        return {'hello': 'world'}


21
22
23
24
25
26
27
28
29
30
31
class TestDangerousSerializer(serializers.ActionSerializer):
    actions = ['test', 'test_dangerous']
    dangerous_actions = ['test_dangerous']

    def handle_test(self, objects):
        pass

    def handle_test_dangerous(self, objects):
        pass


Eliot Berriot's avatar
Eliot Berriot committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
def test_action_serializer_validates_action():
    data = {'objects': 'all', 'action': 'nope'}
    serializer = TestSerializer(data, queryset=models.User.objects.none())

    assert serializer.is_valid() is False
    assert 'action' in serializer.errors


def test_action_serializer_validates_objects():
    data = {'objects': 'nope', 'action': 'test'}
    serializer = TestSerializer(data, queryset=models.User.objects.none())

    assert serializer.is_valid() is False
    assert 'objects' in serializer.errors


def test_action_serializers_objects_clean_ids(factories):
    user1 = factories['users.User']()
    user2 = factories['users.User']()

    data = {'objects': [user1.pk], 'action': 'test'}
    serializer = TestSerializer(data, queryset=models.User.objects.all())

    assert serializer.is_valid() is True
    assert list(serializer.validated_data['objects']) == [user1]


def test_action_serializers_objects_clean_all(factories):
    user1 = factories['users.User']()
    user2 = factories['users.User']()

    data = {'objects': 'all', 'action': 'test'}
    serializer = TestSerializer(data, queryset=models.User.objects.all())

    assert serializer.is_valid() is True
    assert list(serializer.validated_data['objects']) == [user1, user2]


def test_action_serializers_save(factories, mocker):
    handler = mocker.spy(TestSerializer, 'handle_test')
    user1 = factories['users.User']()
    user2 = factories['users.User']()

    data = {'objects': 'all', 'action': 'test'}
    serializer = TestSerializer(data, queryset=models.User.objects.all())

    assert serializer.is_valid() is True
    result = serializer.save()
    assert result == {
        'updated': 2,
        'action': 'test',
        'result': {'hello': 'world'},
    }
    handler.assert_called_once()


def test_action_serializers_filterset(factories):
    user1 = factories['users.User'](is_active=False)
    user2 = factories['users.User'](is_active=True)

    data = {
        'objects': 'all',
        'action': 'test',
        'filters': {'is_active': True},
    }
    serializer = TestSerializer(data, queryset=models.User.objects.all())

    assert serializer.is_valid() is True
    assert list(serializer.validated_data['objects']) == [user2]
101
102
103
104
105
106
107
108
109
110
111


def test_action_serializers_validates_at_least_one_object():
    data = {
        'objects': 'all',
        'action': 'test',
    }
    serializer = TestSerializer(data, queryset=models.User.objects.none())

    assert serializer.is_valid() is False
    assert 'non_field_errors' in serializer.errors
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


def test_dangerous_actions_refuses_all(factories):
    factories['users.User']()
    data = {
        'objects': 'all',
        'action': 'test_dangerous',
    }
    serializer = TestDangerousSerializer(
        data, queryset=models.User.objects.all())

    assert serializer.is_valid() is False
    assert 'non_field_errors' in serializer.errors


def test_dangerous_actions_refuses_not_listed(factories):
    factories['users.User']()
    data = {
        'objects': 'all',
        'action': 'test',
    }
    serializer = TestDangerousSerializer(
        data, queryset=models.User.objects.all())

    assert serializer.is_valid() is True