test_moderation.py 7.15 KB
Newer Older
1
2
from django.test import override_settings
from .withUserTestCase import WithUserTestCase
Florent Chehab's avatar
Florent Chehab committed
3
4
from backend.models.my_model import ForTestingModeration
from backend.models.my_model import PendingModeration
5
6
7
8
9
10
from django.contrib.contenttypes.models import ContentType
from django.conf import settings


class ModerationTestCase(WithUserTestCase):

Florent Chehab's avatar
Florent Chehab committed
11
12
13
    @classmethod
    def setUpMoreTestData(cls):
        cls.api_moderation = '/api/test/moderation/'
14

Florent Chehab's avatar
Florent Chehab committed
15
16
    def test_setting_ok(self):
        self.assertTrue(settings.TESTING)
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
    #####

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

    def _submit_put_test(self, 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(self, data):
        matching = ForTestingModeration.objects.filter(
            aaa=data['aaa']
        )
        self.assertTrue(matching.exists())

        return matching[0]

    def _test_retreive_instance_in_moderation(self, 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(self, val1, null):
        if null:
            self.assertIsNone(val1)
        else:
            self.assertIsNotNone(val1)

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

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

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

    ####

80
81
82
    @override_settings(MODERATION_ACTIVATED=True)
    def test_moderation_activated(self):
        """
83
84
85
        Test to check that when moderation IS activated,
        the app behaves as expected regarding creation
        and moderation of models.
86
87
88
89
90
        """

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

94
95
        self._test_updated_val(instance, null=True)
        self._test_moderated_val(instance, null=True)
96

97
        instance_in_moderation = self._test_retreive_instance_in_moderation(
98
            instance)
99
100
        self._test_updated_val(instance_in_moderation, null=False)
        self._test_new_obj_val(instance_in_moderation, data_1)
101
102
103
104

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

108
109
        self._test_updated_val(instance, null=True)
        self._test_moderated_val(instance, null=True)
110

111
        instance_in_moderation = self._test_retreive_instance_in_moderation(
112
            instance)
113
        self._test_updated_val(instance_in_moderation, null=False)
114
        # here we espect data_2
115
        self._test_new_obj_val(instance_in_moderation, data_2)
116
117
118
119

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

123
124
        self._test_updated_val(instance, null=False)
        self._test_moderated_val(instance, null=False)
125
126
127
128
        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)

129
        instance_in_moderation = self._test_retreive_instance_in_moderation(
130
131
132
133
134
            instance, True)

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

138
139
        self._test_updated_val(instance, null=False)  # Not True
        self._test_moderated_val(instance, null=False)  # Not True
140

141
        instance_in_moderation = self._test_retreive_instance_in_moderation(
142
            instance)
143
        self._test_updated_val(instance_in_moderation, null=False)
144
        # here we espect data_4
145
        self. _test_new_obj_val(instance_in_moderation, data_4)
146
147
148

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

152
153
154
        self._test_updated_val(instance, null=False)
        self._test_moderated_val(instance, null=False)
        # (below) because the field aaa was NOT updated by the moderator
155
156
157
        self.assertEqual(instance.updated_by, self.authenticated_user)
        self.assertEqual(instance.moderated_by, self.moderator_user)

158
        instance_in_moderation = self._test_retreive_instance_in_moderation(
159
            instance, True)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

    ####

    @override_settings(MODERATION_ACTIVATED=True)
    def test_staff_no_moderation(self):
        """
        Test to check that staff member are not subject to moderation.
        """

        data_1 = {'aaa': "Test sdfdf"}
        self._submit_post_test(self.staff_client, data_1)
        instance = self._test_retreive_instance(data_1)

        self._test_updated_val(instance, null=False)
        self._test_moderated_val(instance, null=False)
        self.assertEqual(instance.updated_by, self.staff_user)
        self.assertEqual(instance.moderated_by, self.staff_user)

        self._test_retreive_instance_in_moderation(
            instance, expected_fail=True
        )

    ####

    @override_settings(MODERATION_ACTIVATED=False)
    def test_moderation_not_activated(self):
        """
        Test to check that when moderation IS NOT activated,
        the app behaves as expected regarding creation
        and moderation of models.
        """

        # phase 1
        # Test that an authentificated user can post data
        # And that there is nothing pending in moderation
        data_1 = {'aaa': "Test"}
        self._submit_post_test(self.authenticated_client, data_1)
        instance = self._test_retreive_instance(data_1)

        self._test_updated_val(instance, null=False)
        self._test_moderated_val(instance, null=False)
        self.assertEqual(instance.updated_by, self.authenticated_user)
        self.assertEqual(instance.moderated_by, self.authenticated_user)

        self._test_retreive_instance_in_moderation(
            instance, expected_fail=True
        )