test_moderation.py 5.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
101
102
103
104
105
106
107
108
109
110
111
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from django.test import override_settings
from .withUserTestCase import WithUserTestCase
from backend.models.moderation import ForTestingModeration
from backend.models.moderation import PendingModeration
from django.contrib.contenttypes.models import ContentType
from django.conf import settings


class ModerationTestCase(WithUserTestCase):

    def test_setting_ok(self):
        self.assertTrue(settings.TESTING)

    def moreSetUp(self):
        self.api_moderation = '/api/test/moderation/'

    def test_read(self):
        """
        TODO à déplacer ailleur
        Basic tests to check read access rights
        """
        response = self.staff_client.get(self.api_moderation)
        self.assertEqual(response.status_code, 200)

        response = self.authenticated_client.get(self.api_moderation)
        self.assertEqual(response.status_code, 200)

        response = self.unauthenticated_client.get(self.api_moderation)
        self.assertEqual(response.status_code, 403)

    @override_settings(MODERATION_ACTIVATED=True)
    def test_moderation_activated(self):
        """
        Simple test to check if we can create a superuser. We had issues in the past.
        """

        def _submit_post_test(client, data):
            response = client.post(
                self.api_moderation,
                data,
                format='json'
            )
            self.assertEqual(response.status_code, 201)
            return response

        def _submit_put_test(client, data, pk):
            response = client.put(
                self.api_moderation + str(pk) + '/',
                data,
                format='json'
            )
            self.assertEqual(response.status_code, 200)
            return response

        def _test_retreive_instance(data):
            matching = ForTestingModeration.objects.filter(
                aaa=data['aaa']
            )
            self.assertTrue(matching.exists())

            return matching[0]

        def _test_retreive_instance_in_moderation(instance, expected_fail=False):
            ct = ContentType.objects.get_for_model(ForTestingModeration)
            pending = PendingModeration.objects.filter(
                content_type=ct, object_id=instance.pk)
            if expected_fail:
                self.assertFalse(pending.exists())
                return None
            else:
                self.assertTrue(pending.exists())
                self.assertEqual(len(pending), 1)
                return pending[0]

        def _test_val_null(val1, null):
            if null:
                self.assertIsNone(val1)
            else:
                self.assertIsNotNone(val1)

        def _test_moderated_val(instance, null=True):
            _test_val_null(instance.moderated_by, null)
            _test_val_null(instance.moderated_on, null)

        def _test_updated_val(instance, null=True):
            _test_val_null(instance.updated_on, null)
            _test_val_null(instance.updated_by, null)

        def _test_new_obj_val(instance, data):
            self.assertEqual(
                instance.new_object['aaa'],
                data['aaa']
            )

        # phase 1
        # Need moderation
        data_1 = {'aaa': "Test"}
        _submit_post_test(self.authenticated_client, data_1)
        instance = _test_retreive_instance(data_1)

        _test_updated_val(instance, null=True)
        _test_moderated_val(instance, null=True)

        instance_in_moderation = _test_retreive_instance_in_moderation(
            instance)
        _test_updated_val(instance_in_moderation, null=False)
        _test_new_obj_val(instance_in_moderation, data_1)

        # Phase 2
        # More moderation
        data_2 = {'aaa': "Test 2"}
        _submit_put_test(self.authenticated_client, data_2, instance.pk)
        instance = _test_retreive_instance(data_1)  # not data_2 !

        _test_updated_val(instance, null=True)
        _test_moderated_val(instance, null=True)

        instance_in_moderation = _test_retreive_instance_in_moderation(
            instance)
        _test_updated_val(instance_in_moderation, null=False)
        # here we espect data_2
        _test_new_obj_val(instance_in_moderation, data_2)

        # Phase 3
        # Moderation with modification
        data_3 = {'aaa': "Test 3"}
        _submit_put_test(self.moderator_client, data_3, instance.pk)
        instance = _test_retreive_instance(data_3)  # not data_2 !

        _test_updated_val(instance, null=False)
        _test_moderated_val(instance, null=False)
        self.assertEqual(instance.updated_by, self.moderator_user)
        # (below) because the field aaa was updated by the moderator
        self.assertEqual(instance.moderated_by, self.moderator_user)

        instance_in_moderation = _test_retreive_instance_in_moderation(
            instance, True)

        # Phase 4
        # Put that require moderation
        data_4 = {'aaa': "Test 4"}
        _submit_put_test(self.authenticated_client, data_4, instance.pk)
        instance = _test_retreive_instance(data_3)  # not data_4 !

        _test_updated_val(instance, null=False)  # Not True
        _test_moderated_val(instance, null=False)  # Not True

        instance_in_moderation = _test_retreive_instance_in_moderation(
            instance)
        _test_updated_val(instance_in_moderation, null=False)
        # here we espect data_4
        _test_new_obj_val(instance_in_moderation, data_4)

        # Phase 5
        # Moderation with no modification
        _submit_put_test(self.moderator_client, data_4, instance.pk)
        instance = _test_retreive_instance(data_4)  # not data_2 !

        _test_updated_val(instance, null=False)
        _test_moderated_val(instance, null=False)
        # (below) because the field aaa was updated by the moderator
        self.assertEqual(instance.updated_by, self.authenticated_user)
        self.assertEqual(instance.moderated_by, self.moderator_user)

        instance_in_moderation = _test_retreive_instance_in_moderation(
            instance, True)