test_versionning.py 2.21 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from .withUserTestCase import WithUserTestCase
Florent Chehab's avatar
Florent Chehab committed
2
from backend_app.models.abstract.my_model import ForTestingVersioning
Florent Chehab's avatar
Florent Chehab committed
3
4
from django.conf import settings
from reversion.models import Version
Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.signals.__squash_revision_by_user import new_revision_saved
Florent Chehab's avatar
Florent Chehab committed
6
from django.test import override_settings
Florent Chehab's avatar
Florent Chehab committed
7
8


9
class VersioningTestCase(WithUserTestCase):
Florent Chehab's avatar
Florent Chehab committed
10
11
    @classmethod
    def setUpMoreTestData(cls):
12
        cls.testing_model = ForTestingVersioning
13
        cls.api_versioning = "/api/test/versioning/"
Florent Chehab's avatar
Florent Chehab committed
14
15
16
17
18
19
20
21
22
23

    def reset_signal_called(self):
        self.signal_was_called = False

    def test_setting_ok(self):
        self.assertTrue(settings.TESTING)

    #####

    def _submit_put_test(self, client, data, pk):
24
        response = client.put(self.api_versioning + str(pk) + "/", data, format="json")
Florent Chehab's avatar
Florent Chehab committed
25
26
27
28
        self.assertEqual(response.status_code, 200)
        return response

    def _test_retreive_instance(self, data):
29
        matching = self.testing_model.objects.filter(bbb=data["bbb"])
Florent Chehab's avatar
Florent Chehab committed
30
31
32
33
34
        self.assertTrue(matching.exists())

        return matching[0]

    ####
Florent Chehab's avatar
Florent Chehab committed
35
    @override_settings(MODERATION_ACTIVATED=False)
36
    def test_versioning(self):
Florent Chehab's avatar
Florent Chehab committed
37
        """
38
        Test to check that versioning is working
Florent Chehab's avatar
Florent Chehab committed
39
40
41
42
43
44
45
46
        We also check that new_revision_saved is called
        """

        def _test_signal_sent(sender, obj, **kwargs):
            self.signal_was_called = True

        new_revision_saved.connect(_test_signal_sent)

47
        data_1 = {"bbb": "Test 1"}
Florent Chehab's avatar
Florent Chehab committed
48
        response = self.authenticated_client.post(
49
            self.api_versioning, data_1, format="json"
Florent Chehab's avatar
Florent Chehab committed
50
51
52
53
54
55
56
57
58
        )
        self.assertEqual(response.status_code, 201)
        instance = self._test_retreive_instance(data_1)

        versions = Version.objects.get_for_object(instance)
        self.assertEqual(len(versions), 1)
        self.assertTrue(self.signal_was_called)
        self.reset_signal_called()

59
        data_2 = {"bbb": "Test 2"}
Florent Chehab's avatar
Florent Chehab committed
60
        response = self.authenticated_client_2.put(
61
            self.api_versioning + str(instance.pk) + "/", data_2, format="json"
Florent Chehab's avatar
Florent Chehab committed
62
63
64
65
66
67
        )
        self.assertEqual(response.status_code, 200)

        versions = Version.objects.get_for_object(instance)
        self.assertEqual(len(versions), 2)
        self.assertTrue(self.signal_was_called)