test_moderation.py 8.09 KB
Newer Older
1
2
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
3
from django.test import override_settings
4

5
6
from backend_app.models.for_testing.moderation import ForTestingModeration
from backend_app.models.pendingModeration import PendingModeration
Florent Chehab's avatar
Florent Chehab committed
7
from backend_app.models.university import University
8
from backend_app.tests.utils import WithUserTestCase
9
10
11


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

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

19
20
    #####

21
22
23
    def _submit_post_test(self, client, data, end_point=None):
        if end_point is None:
            end_point = self.api_moderation
24
        response = client.post(end_point, data, format="json")
25
26
27
28
        self.assertEqual(response.status_code, 201)
        return response

    def _submit_put_test(self, client, data, pk):
29
        response = client.put(self.api_moderation + str(pk) + "/", data, format="json")
30
31
32
33
        self.assertEqual(response.status_code, 200)
        return response

    def _test_retreive_instance(self, data):
34
        matching = ForTestingModeration.objects.filter(aaa=data["aaa"])
35
36
37
38
39
40
41
        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(
42
43
            content_type=ct, object_id=instance.pk
        )
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        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)

66
67
68
    def _test_has_pending_moderation_val(self, instance, has: bool):
        self.assertEqual(instance.has_pending_moderation, has)

69
    def _test_new_obj_val(self, instance, data):
70
        self.assertEqual(instance.new_object["aaa"], data["aaa"])
71
72
73

    ####

74
75
76
    @override_settings(MODERATION_ACTIVATED=True)
    def test_moderation_activated(self):
        """
77
78
79
        Test to check that when moderation IS activated,
        the app behaves as expected regarding creation
        and moderation of models.
80
81
82
83
        """

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

88
89
        self._test_updated_val(instance, null=True)
        self._test_moderated_val(instance, null=True)
90
        self._test_has_pending_moderation_val(instance, True)
91

92
        instance_in_moderation = self._test_retreive_instance_in_moderation(instance)
93
94
        self._test_updated_val(instance_in_moderation, null=False)
        self._test_new_obj_val(instance_in_moderation, data_1)
95
96
97

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

102
103
        self._test_updated_val(instance, null=True)
        self._test_moderated_val(instance, null=True)
104
        self._test_has_pending_moderation_val(instance, True)
105

106
        instance_in_moderation = self._test_retreive_instance_in_moderation(instance)
107
        self._test_updated_val(instance_in_moderation, null=False)
108
        # here we espect data_2
109
        self._test_new_obj_val(instance_in_moderation, data_2)
110
111
112

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

117
118
        self._test_updated_val(instance, null=False)
        self._test_moderated_val(instance, null=False)
119
120
121
122
        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)

123
        instance_in_moderation = self._test_retreive_instance_in_moderation(
124
125
            instance, True
        )
126
        self._test_has_pending_moderation_val(instance, False)
127
128
129

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

134
135
        self._test_updated_val(instance, null=False)  # Not True
        self._test_moderated_val(instance, null=False)  # Not True
136
        self._test_has_pending_moderation_val(instance, True)
137

138
        instance_in_moderation = self._test_retreive_instance_in_moderation(instance)
139
        self._test_updated_val(instance_in_moderation, null=False)
140
        # here we espect data_4
141
        self._test_new_obj_val(instance_in_moderation, data_4)
142
143
144

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

148
149
150
        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
151
152
153
        self.assertEqual(instance.updated_by, self.authenticated_user)
        self.assertEqual(instance.moderated_by, self.moderator_user)

154
        instance_in_moderation = self._test_retreive_instance_in_moderation(
155
156
            instance, True
        )
157
        self._test_has_pending_moderation_val(instance, False)
158
159
160
161
162
163
164
165
166

    ####

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

167
        data_1 = {"aaa": "Test sdfdf"}
168
169
170
171
172
173
174
175
        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)

176
        self._test_retreive_instance_in_moderation(instance, expected_fail=True)
177
178
179
180
181
182
183
184
185
186
187
188
189
190

    ####

    @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
191
        data_1 = {"aaa": "Test"}
192
193
194
195
196
197
198
199
        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)

200
        self._test_retreive_instance_in_moderation(instance, expected_fail=True)
201
202
203
204
205
206
207
208

    ####
    @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
        """
209
        c = University(name="Univ de test", utc_id=100)
210
211
        c.save()

212
        data = {"comment": "", "useful_links": [], "universities": [c.pk]}
213
        api_end_point = "/api/universityDri/?universities={}/".format(c.pk)
214
        self._submit_post_test(self.dri_client, data, api_end_point)