test_versionning.py 2.33 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
12

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

    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):
        response = client.put(
26
            self.api_versioning + str(pk) + '/',
Florent Chehab's avatar
Florent Chehab committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
            data,
            format='json'
        )
        self.assertEqual(response.status_code, 200)
        return response

    def _test_retreive_instance(self, data):
        matching = self.testing_model.objects.filter(
            bbb=data['bbb']
        )
        self.assertTrue(matching.exists())

        return matching[0]

    ####
Florent Chehab's avatar
Florent Chehab committed
42
    @override_settings(MODERATION_ACTIVATED=False)
43
    def test_versioning(self):
Florent Chehab's avatar
Florent Chehab committed
44
        """
45
        Test to check that versioning is working
Florent Chehab's avatar
Florent Chehab committed
46
47
48
49
50
51
52
53
54
55
        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)

        data_1 = {'bbb': "Test 1"}
        response = self.authenticated_client.post(
56
            self.api_versioning,
Florent Chehab's avatar
Florent Chehab committed
57
58
59
60
61
62
63
64
65
66
67
68
69
            data_1,
            format='json'
        )
        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()

        data_2 = {'bbb': "Test 2"}
        response = self.authenticated_client_2.put(
70
            self.api_versioning + str(instance.pk) + '/',
Florent Chehab's avatar
Florent Chehab committed
71
72
73
74
75
76
77
78
            data_2,
            format='json'
        )
        self.assertEqual(response.status_code, 200)

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