diff --git a/src/objects/token/admin.py b/src/objects/token/admin.py index 03959f9c..3d7d82b6 100644 --- a/src/objects/token/admin.py +++ b/src/objects/token/admin.py @@ -52,6 +52,10 @@ def get_data_field_choices(self): except requests.JSONDecodeError: continue + # TODO: remove check once API V1 is removed + if "results" in response_data: + response_data = response_data["results"] + # use only first level of properties data_fields[object_type.id] = { version["version"]: { diff --git a/src/objects/token/tests/cassettes/PermissionAdminTests.test_with_object_types_api_v1.yaml b/src/objects/token/tests/cassettes/PermissionAdminTests.test_with_object_types_api_v1.yaml new file mode 100644 index 00000000..d787d463 --- /dev/null +++ b/src/objects/token/tests/cassettes/PermissionAdminTests.test_with_object_types_api_v1.yaml @@ -0,0 +1,96 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://127.0.0.1:8008/api/v1/objecttypes + response: + body: + string: '[{"url":"http://127.0.0.1:8008/api/v1/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e","uuid":"71a2452a-66c3-4030-b5ec-a06035102e9e","name":"Garry","namePlural":"Garrys","description":"","dataClassification":"open","maintainerOrganization":"","maintainerDepartment":"","contactPerson":"","contactEmail":"","source":"","updateFrequency":"unknown","providerOrganization":"","documentationUrl":"","labels":{},"createdAt":"2024-09-24","modifiedAt":"2024-09-24","allowGeometry":true,"versions":["http://127.0.0.1:8008/api/v1/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e/versions/1"]}]' + headers: + Allow: + - GET, POST, HEAD, OPTIONS + Content-Length: + - '584' + Content-Type: + - application/json + Cross-Origin-Opener-Policy: + - same-origin + Date: + - Tue, 24 Sep 2024 14:49:45 GMT + Referrer-Policy: + - same-origin + Server: + - WSGIServer/0.2 CPython/3.11.10 + Server-Timing: + - TimerPanel_utime;dur=66.08599999999998;desc="User CPU time", TimerPanel_stime;dur=41.489;desc="System + CPU time", TimerPanel_total;dur=107.57499999999999;desc="Total CPU time", + TimerPanel_total_time;dur=109.82951400001184;desc="Elapsed time", SQLPanel_sql_time;dur=2.634805998241063;desc="SQL + 3 queries", CachePanel_total_time;dur=0;desc="Cache 0 Calls" + Vary: + - origin, Cookie + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + djdt-store-id: + - 011b401c548140e393c17931b9eed7e2 + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://127.0.0.1:8008/api/v1/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e/versions + response: + body: + string: '[{"url":"http://127.0.0.1:8008/api/v1/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e/versions/1","version":1,"objectType":"http://127.0.0.1:8008/api/v1/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e","status":"draft","jsonSchema":{"string":"value"},"createdAt":"2024-09-24","modifiedAt":"2024-09-24","publishedAt":null}]' + headers: + Allow: + - GET, POST, HEAD, OPTIONS + Content-Length: + - '324' + Content-Type: + - application/json + Cross-Origin-Opener-Policy: + - same-origin + Date: + - Tue, 24 Sep 2024 14:49:45 GMT + Referrer-Policy: + - same-origin + Server: + - WSGIServer/0.2 CPython/3.11.10 + Server-Timing: + - TimerPanel_utime;dur=26.51400000000004;desc="User CPU time", TimerPanel_stime;dur=8.871000000000018;desc="System + CPU time", TimerPanel_total;dur=35.385000000000055;desc="Total CPU time", + TimerPanel_total_time;dur=29.870047997974325;desc="Elapsed time", SQLPanel_sql_time;dur=3.140712000458734;desc="SQL + 3 queries", CachePanel_total_time;dur=0;desc="Cache 0 Calls" + Vary: + - origin, Cookie + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + djdt-store-id: + - 58b76bf29d284ba694ccdabb76919aa7 + status: + code: 200 + message: OK +version: 1 diff --git a/src/objects/token/tests/cassettes/PermissionAdminTests.test_with_object_types_api_v2.yaml b/src/objects/token/tests/cassettes/PermissionAdminTests.test_with_object_types_api_v2.yaml new file mode 100644 index 00000000..461b90cc --- /dev/null +++ b/src/objects/token/tests/cassettes/PermissionAdminTests.test_with_object_types_api_v2.yaml @@ -0,0 +1,96 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://127.0.0.1:8008/api/v2/objecttypes + response: + body: + string: '{"count":1,"next":null,"previous":null,"results":[{"url":"http://127.0.0.1:8008/api/v2/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e","uuid":"71a2452a-66c3-4030-b5ec-a06035102e9e","name":"Garry","namePlural":"Garrys","description":"","dataClassification":"open","maintainerOrganization":"","maintainerDepartment":"","contactPerson":"","contactEmail":"","source":"","updateFrequency":"unknown","providerOrganization":"","documentationUrl":"","labels":{},"createdAt":"2024-09-24","modifiedAt":"2024-09-24","allowGeometry":true,"versions":["http://127.0.0.1:8008/api/v2/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e/versions/1"]}]}' + headers: + Allow: + - GET, POST, HEAD, OPTIONS + Content-Length: + - '634' + Content-Type: + - application/json + Cross-Origin-Opener-Policy: + - same-origin + Date: + - Tue, 24 Sep 2024 14:49:45 GMT + Referrer-Policy: + - same-origin + Server: + - WSGIServer/0.2 CPython/3.11.10 + Server-Timing: + - TimerPanel_utime;dur=17.129999999999868;desc="User CPU time", TimerPanel_stime;dur=2.0120000000000138;desc="System + CPU time", TimerPanel_total;dur=19.141999999999882;desc="Total CPU time", + TimerPanel_total_time;dur=21.54170200083172;desc="Elapsed time", SQLPanel_sql_time;dur=2.798614998027915;desc="SQL + 4 queries", CachePanel_total_time;dur=0;desc="Cache 0 Calls" + Vary: + - origin, Cookie + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + djdt-store-id: + - a591b4b56a884263835fbf632b782baf + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://127.0.0.1:8008/api/v2/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e/versions + response: + body: + string: '{"count":1,"next":null,"previous":null,"results":[{"url":"http://127.0.0.1:8008/api/v2/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e/versions/1","version":1,"objectType":"http://127.0.0.1:8008/api/v2/objecttypes/71a2452a-66c3-4030-b5ec-a06035102e9e","status":"draft","jsonSchema":{"string":"value"},"createdAt":"2024-09-24","modifiedAt":"2024-09-24","publishedAt":null}]}' + headers: + Allow: + - GET, POST, HEAD, OPTIONS + Content-Length: + - '374' + Content-Type: + - application/json + Cross-Origin-Opener-Policy: + - same-origin + Date: + - Tue, 24 Sep 2024 14:49:45 GMT + Referrer-Policy: + - same-origin + Server: + - WSGIServer/0.2 CPython/3.11.10 + Server-Timing: + - TimerPanel_utime;dur=17.527999999999988;desc="User CPU time", TimerPanel_stime;dur=0.8290000000000242;desc="System + CPU time", TimerPanel_total;dur=18.357000000000014;desc="Total CPU time", + TimerPanel_total_time;dur=20.605010002327617;desc="Elapsed time", SQLPanel_sql_time;dur=2.700799006561283;desc="SQL + 4 queries", CachePanel_total_time;dur=0;desc="Cache 0 Calls" + Vary: + - origin, Cookie + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + djdt-store-id: + - fa6e9a070865404f8c02c2ad60df23b2 + status: + code: 200 + message: OK +version: 1 diff --git a/src/objects/token/tests/test_admin.py b/src/objects/token/tests/test_admin.py new file mode 100644 index 00000000..0db3ea0d --- /dev/null +++ b/src/objects/token/tests/test_admin.py @@ -0,0 +1,92 @@ +from django.test import TestCase, tag +from django.urls import reverse + +from maykin_2fa.test import disable_admin_mfa +from vcr.unittest import VCRMixin +from zgw_consumers.constants import AuthTypes +from zgw_consumers.test.factories import ServiceFactory + +from objects.accounts.tests.factories import UserFactory +from objects.core.tests.factories import ObjectTypeFactory + + +@disable_admin_mfa() +class PermissionAdminTests(VCRMixin, TestCase): + + object_types_api = "http://127.0.0.1:8008/api/{version}/" + + def setUp(self): + super().setUp() + + self.user = UserFactory(superuser=True) + self.client.force_login(self.user) + self.url = reverse("admin:token_permission_add") + + def _get_vcr(self, **kwargs): + vcr = super()._get_vcr(**kwargs) + vcr.filter_headers = ["authorization"] + return vcr + + @tag("#449") + def test_with_object_types_api_v1(self): + """ + Regression test for #449. + Test if Permission admin can still handle objecttypes API V1 + """ + + v1_service = ServiceFactory( + api_root=self.object_types_api.format(version="v1"), + auth_type=AuthTypes.api_key, + header_key="Authorization", + header_value="Token 5cebbb33ffa725b6ed5e9e98300061218ba98d71", + ) + object_type = ObjectTypeFactory( + service=v1_service, uuid="71a2452a-66c3-4030-b5ec-a06035102e9e" + ) + + response = self.client.get(self.url) + + self.assertEqual(response.status_code, 200) + + form = response.context["adminform"].form + choices = list(form.fields["object_type"].choices) + + self.assertEqual( + choices[1][0].value, + object_type.id, + ) + self.assertEqual( + choices[1][1], + f"{v1_service.label}: {object_type._name}", + ) + + @tag("#449") + def test_with_object_types_api_v2(self): + """ + Regression test for #449. + Test if Permission admin can handle objecttypes API V2 which added pagination + """ + v2_service = ServiceFactory( + api_root=self.object_types_api.format(version="v2"), + auth_type=AuthTypes.api_key, + header_key="Authorization", + header_value="Token 5cebbb33ffa725b6ed5e9e98300061218ba98d71", + ) + object_type = ObjectTypeFactory( + service=v2_service, uuid="71a2452a-66c3-4030-b5ec-a06035102e9e" + ) + + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + + form = response.context["adminform"].form + choices = list(form.fields["object_type"].choices) + + self.assertEqual( + choices[1][0].value, + object_type.id, + ) + self.assertEqual( + choices[1][1], + f"{v2_service.label}: {object_type._name}", + )