|
| 1 | +""" |
| 2 | +Test that the caching mechanisms are in place. |
| 3 | +""" |
| 4 | +from rest_framework import status |
| 5 | +from rest_framework.test import APITestCase, APITransactionTestCase |
| 6 | +from vng_api_common.tests import CacheMixin, JWTAuthMixin, generate_jwt_auth, reverse |
| 7 | +from vng_api_common.tests.schema import get_spec |
| 8 | + |
| 9 | +from brc.datamodel.tests.factories import BesluitFactory, BesluitInformatieObjectFactory |
| 10 | + |
| 11 | +from .mixins import MockSyncMixin |
| 12 | + |
| 13 | + |
| 14 | +class BesluitCacheTests(CacheMixin, JWTAuthMixin, APITestCase): |
| 15 | + heeft_alle_autorisaties = True |
| 16 | + |
| 17 | + def test_besluit_get_cache_header(self): |
| 18 | + besluit = BesluitFactory.create() |
| 19 | + |
| 20 | + response = self.client.get(reverse(besluit)) |
| 21 | + |
| 22 | + self.assertHasETag(response) |
| 23 | + |
| 24 | + def test_besluit_head_cache_header(self): |
| 25 | + besluit = BesluitFactory.create() |
| 26 | + |
| 27 | + self.assertHeadHasETag(reverse(besluit)) |
| 28 | + |
| 29 | + def test_head_in_apischema(self): |
| 30 | + spec = get_spec() |
| 31 | + |
| 32 | + endpoint = spec["paths"]["/besluiten/{uuid}"] |
| 33 | + |
| 34 | + self.assertIn("head", endpoint) |
| 35 | + |
| 36 | + def test_conditional_get_304(self): |
| 37 | + besluit = BesluitFactory.create(with_etag=True) |
| 38 | + response = self.client.get( |
| 39 | + reverse(besluit), HTTP_IF_NONE_MATCH=f'"{besluit._etag}"' |
| 40 | + ) |
| 41 | + |
| 42 | + self.assertEqual(response.status_code, status.HTTP_304_NOT_MODIFIED) |
| 43 | + |
| 44 | + def test_conditional_get_stale(self): |
| 45 | + besluit = BesluitFactory.create(with_etag=True) |
| 46 | + |
| 47 | + response = self.client.get(reverse(besluit), HTTP_IF_NONE_MATCH=f'"not-an-md5"') |
| 48 | + |
| 49 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 50 | + |
| 51 | + |
| 52 | +class BesluitInformatieObjectCacheTests( |
| 53 | + MockSyncMixin, CacheMixin, JWTAuthMixin, APITestCase |
| 54 | +): |
| 55 | + heeft_alle_autorisaties = True |
| 56 | + |
| 57 | + def test_besluit_get_cache_header(self): |
| 58 | + bio = BesluitInformatieObjectFactory.create() |
| 59 | + |
| 60 | + response = self.client.get(reverse(bio)) |
| 61 | + |
| 62 | + self.assertHasETag(response) |
| 63 | + |
| 64 | + def test_besluit_head_cache_header(self): |
| 65 | + bio = BesluitInformatieObjectFactory.create() |
| 66 | + |
| 67 | + self.assertHeadHasETag(reverse(bio)) |
| 68 | + |
| 69 | + def test_head_in_apischema(self): |
| 70 | + spec = get_spec() |
| 71 | + |
| 72 | + endpoint = spec["paths"]["/besluitinformatieobjecten/{uuid}"] |
| 73 | + |
| 74 | + self.assertIn("head", endpoint) |
| 75 | + |
| 76 | + def test_conditional_get_304(self): |
| 77 | + bio = BesluitInformatieObjectFactory.create(with_etag=True) |
| 78 | + response = self.client.get(reverse(bio), HTTP_IF_NONE_MATCH=f'"{bio._etag}"') |
| 79 | + |
| 80 | + self.assertEqual(response.status_code, status.HTTP_304_NOT_MODIFIED) |
| 81 | + |
| 82 | + def test_conditional_get_stale(self): |
| 83 | + bio = BesluitInformatieObjectFactory.create(with_etag=True) |
| 84 | + |
| 85 | + response = self.client.get(reverse(bio), HTTP_IF_NONE_MATCH=f'"not-an-md5"') |
| 86 | + |
| 87 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 88 | + |
| 89 | + |
| 90 | +class BesluitCacheTransactionTests(JWTAuthMixin, APITransactionTestCase): |
| 91 | + heeft_alle_autorisaties = True |
| 92 | + |
| 93 | + def setUp(self): |
| 94 | + super().setUp() |
| 95 | + self._create_credentials( |
| 96 | + self.client_id, |
| 97 | + self.secret, |
| 98 | + self.heeft_alle_autorisaties, |
| 99 | + self.max_vertrouwelijkheidaanduiding, |
| 100 | + ) |
| 101 | + |
| 102 | + def test_invalidate_etag_after_change(self): |
| 103 | + """ |
| 104 | + Because changes are made to the besluit, a code 200 should be returned |
| 105 | + """ |
| 106 | + besluit = BesluitFactory.create(toelichting="", with_etag=True) |
| 107 | + etag = besluit._etag |
| 108 | + |
| 109 | + besluit.toelichting = "bla" |
| 110 | + besluit.save() |
| 111 | + |
| 112 | + response = self.client.get(reverse(besluit), HTTP_IF_NONE_MATCH=f"{etag}") |
| 113 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
0 commit comments