test_moderation.py 8.48 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
            data,
            format='json'
        )
30
        print(response)
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
        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']
        )

    ####

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

163
        instance_in_moderation = self._test_retreive_instance_in_moderation(
164
            instance, True)
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
211

    ####

    @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
        )
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
242
243

    ####
    @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") + "/"
        print(api_end_point)
        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
        # )