test_moderation.py 8.43 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
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
7
8
from backend.models.university import University
from backend.utils import find_api_end_point_for_viewset
9
10
11
12


class ModerationTestCase(WithUserTestCase):

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

Florent Chehab's avatar
Florent Chehab committed
17
18
    def test_setting_ok(self):
        self.assertTrue(settings.TESTING)
19

20
21
    #####

22
23
24
    def _submit_post_test(self, client, data, end_point=None):
        if end_point is None:
            end_point = self.api_moderation
25
        response = client.post(
26
            end_point,
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
            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']
        )

    ####

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

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

98
99
        self._test_updated_val(instance, null=True)
        self._test_moderated_val(instance, null=True)
100

101
        instance_in_moderation = self._test_retreive_instance_in_moderation(
102
            instance)
103
104
        self._test_updated_val(instance_in_moderation, null=False)
        self._test_new_obj_val(instance_in_moderation, data_1)
105
106
107
108

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

112
113
        self._test_updated_val(instance, null=True)
        self._test_moderated_val(instance, null=True)
114

115
        instance_in_moderation = self._test_retreive_instance_in_moderation(
116
            instance)
117
        self._test_updated_val(instance_in_moderation, null=False)
118
        # here we espect data_2
119
        self._test_new_obj_val(instance_in_moderation, data_2)
120
121
122
123

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

127
128
        self._test_updated_val(instance, null=False)
        self._test_moderated_val(instance, null=False)
129
130
131
132
        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)

133
        instance_in_moderation = self._test_retreive_instance_in_moderation(
134
135
136
137
138
            instance, True)

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

142
143
        self._test_updated_val(instance, null=False)  # Not True
        self._test_moderated_val(instance, null=False)  # Not True
144

145
        instance_in_moderation = self._test_retreive_instance_in_moderation(
146
            instance)
147
        self._test_updated_val(instance_in_moderation, null=False)
148
        # here we espect data_4
149
        self. _test_new_obj_val(instance_in_moderation, data_4)
150
151
152

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

156
157
158
        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
159
160
161
        self.assertEqual(instance.updated_by, self.authenticated_user)
        self.assertEqual(instance.moderated_by, self.moderator_user)

162
        instance_in_moderation = self._test_retreive_instance_in_moderation(
163
            instance, True)
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
207
208
209
210

    ####

    @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
        )
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

    ####
    @override_settings(MODERATION_ACTIVATED=True)
    def test_moderation_version_instance_error(self):
        """
        Test to check that when moderation IS activated
        no error regarding saving occures with foreing key
        """
        c = University(name="Univ de test")
        c.save()

        data = {
            "comment": "",
            "usefull_links": [],
            "custom_content": {},
            "university": [c.pk]
        }
        api_end_point = "/api/" + \
            find_api_end_point_for_viewset("UniversityDriViewSet") + "/"
        self._submit_post_test(self.authenticated_client,
                               data, api_end_point)
        # 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
        # )