test_versionning.py 2.73 KB
Newer Older
1
2
from backend_app.tests.utils import WithUserTestCase
from backend_app.models.for_testing.versioning import ForTestingVersioning
Florent Chehab's avatar
Florent Chehab committed
3
4
from django.conf import settings
from reversion.models import Version
5
from backend_app.signals.squash_revisions import new_revision_saved
Florent Chehab's avatar
Florent Chehab committed
6
from django.test import override_settings
7
from django.contrib.contenttypes.models import ContentType
Florent Chehab's avatar
Florent Chehab committed
8
9


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

    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):
25
        response = client.put(self.api_versioning + str(pk) + "/", data, format="json")
Florent Chehab's avatar
Florent Chehab committed
26
27
28
        self.assertEqual(response.status_code, 200)
        return response

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

        return matching[0]

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

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

        new_revision_saved.connect(_test_signal_sent)

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

        versions = Version.objects.get_for_object(instance)
        self.assertEqual(len(versions), 1)
58
        self.assertEqual(len(versions), instance.nb_versions)
Florent Chehab's avatar
Florent Chehab committed
59
60
61
        self.assertTrue(self.signal_was_called)
        self.reset_signal_called()

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

68
        instance = self._test_retrieve_instance(data_2)
Florent Chehab's avatar
Florent Chehab committed
69
70
        versions = Version.objects.get_for_object(instance)
        self.assertEqual(len(versions), 2)
71
        self.assertEqual(len(versions), instance.nb_versions)
Florent Chehab's avatar
Florent Chehab committed
72
        self.assertTrue(self.signal_was_called)
73
74
75
76

        # Final test: we query the version viewset itself
        ct = ContentType.objects.get_for_model(instance).id
        self.authenticated_client.get("/api/versions/{}/{}/".format(ct, instance.pk))