test_moderation.py 7.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
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 moreSetUp(self):
        self.api_moderation = '/api/test/moderation/'

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

    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']
        )

    ####

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

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

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

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

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

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

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

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

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

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

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

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

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

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

151
152
153
        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
154
155
156
        self.assertEqual(instance.updated_by, self.authenticated_user)
        self.assertEqual(instance.moderated_by, self.moderator_user)

157
        instance_in_moderation = self._test_retreive_instance_in_moderation(
158
            instance, True)
159
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

    ####

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