|
| 1 | +from django.urls import reverse |
| 2 | +from rest_framework.authtoken.models import Token |
| 3 | +from rest_framework.test import APIClient |
| 4 | + |
| 5 | +from dojo.models import Dojo_User, Product_Type, User |
| 6 | +from unittests.dojo_test_case import DojoAPITestCase, versioned_fixtures |
| 7 | + |
| 8 | + |
| 9 | +@versioned_fixtures |
| 10 | +class ProductTypeAuthorizedUsersApiPermissionTest(DojoAPITestCase): |
| 11 | + |
| 12 | + """ |
| 13 | + Authorization coverage for the ``authorized_users`` field on the Product |
| 14 | + Type API: changing it requires ``Product_Type_Manage_Members``; all other |
| 15 | + fields require ``Product_Type_Edit``; no-op submissions are unaffected. |
| 16 | + Mirrors unittests.test_product_authorized_users_api_authz. |
| 17 | + """ |
| 18 | + |
| 19 | + fixtures = ["dojo_testdata.json"] |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def setUpTestData(cls): |
| 23 | + cls.product_type = Product_Type.objects.create(name="PTAU-Perm PT") |
| 24 | + |
| 25 | + # Alice: holds Product_Type_Edit on the product type (via authorized_users membership). |
| 26 | + cls.alice = User.objects.create_user( |
| 27 | + username="ptau_alice", |
| 28 | + password="not-a-real-secret", # noqa: S106 - test fixture user |
| 29 | + is_staff=False, |
| 30 | + ) |
| 31 | + cls.product_type.authorized_users.add(Dojo_User.objects.get(pk=cls.alice.pk)) |
| 32 | + |
| 33 | + # Bob: another user, not initially a member of the product type. |
| 34 | + cls.bob = User.objects.create_user( |
| 35 | + username="ptau_bob", |
| 36 | + password="not-a-real-secret", # noqa: S106 - test fixture user |
| 37 | + is_staff=False, |
| 38 | + ) |
| 39 | + |
| 40 | + # Staff user, who holds Product_Type_Manage_Members. |
| 41 | + cls.admin = User.objects.create_user( |
| 42 | + username="ptau_admin", |
| 43 | + password="not-a-real-secret", # noqa: S106 - test fixture user |
| 44 | + is_staff=True, |
| 45 | + ) |
| 46 | + |
| 47 | + cls.detail_url = reverse("product_type-detail", args=[cls.product_type.id]) |
| 48 | + |
| 49 | + def _client_for(self, user): |
| 50 | + token, _ = Token.objects.get_or_create(user=user) |
| 51 | + client = APIClient() |
| 52 | + client.credentials(HTTP_AUTHORIZATION="Token " + token.key) |
| 53 | + return client |
| 54 | + |
| 55 | + def test_product_type_edit_cannot_add_authorized_users(self): |
| 56 | + client = self._client_for(self.alice) |
| 57 | + |
| 58 | + # Product_Type_Edit alone permits ordinary field updates. |
| 59 | + response = client.patch( |
| 60 | + self.detail_url, {"description": "edited via product type edit"}, format="json", secure=True, |
| 61 | + ) |
| 62 | + self.assertEqual(200, response.status_code, response.content[:500]) |
| 63 | + |
| 64 | + # Adding a user to authorized_users requires Product_Type_Manage_Members. |
| 65 | + self.assertFalse(self.product_type.authorized_users.filter(pk=self.bob.pk).exists()) |
| 66 | + response = client.patch( |
| 67 | + self.detail_url, |
| 68 | + {"authorized_users": [self.alice.pk, self.bob.pk]}, |
| 69 | + format="json", secure=True, |
| 70 | + ) |
| 71 | + self.assertEqual(403, response.status_code, response.content[:500]) |
| 72 | + |
| 73 | + self.product_type.refresh_from_db() |
| 74 | + self.assertFalse(self.product_type.authorized_users.filter(pk=self.bob.pk).exists()) |
| 75 | + self.assertTrue(self.product_type.authorized_users.filter(pk=self.alice.pk).exists()) |
| 76 | + |
| 77 | + def test_product_type_edit_cannot_replace_authorized_users(self): |
| 78 | + # PATCH replaces the M2M via .set(); replacing the list is also a |
| 79 | + # member-management change and requires Product_Type_Manage_Members. |
| 80 | + client = self._client_for(self.alice) |
| 81 | + response = client.patch( |
| 82 | + self.detail_url, {"authorized_users": [self.bob.pk]}, format="json", secure=True, |
| 83 | + ) |
| 84 | + self.assertEqual(403, response.status_code, response.content[:500]) |
| 85 | + |
| 86 | + self.product_type.refresh_from_db() |
| 87 | + self.assertTrue(self.product_type.authorized_users.filter(pk=self.alice.pk).exists()) |
| 88 | + self.assertFalse(self.product_type.authorized_users.filter(pk=self.bob.pk).exists()) |
| 89 | + |
| 90 | + def test_product_type_edit_unchanged_authorized_users_is_allowed(self): |
| 91 | + # Replay-safe: re-submitting the current membership set unchanged is not |
| 92 | + # a member-management change and is accepted. |
| 93 | + client = self._client_for(self.alice) |
| 94 | + response = client.patch( |
| 95 | + self.detail_url, {"authorized_users": [self.alice.pk]}, format="json", secure=True, |
| 96 | + ) |
| 97 | + self.assertEqual(200, response.status_code, response.content[:500]) |
| 98 | + |
| 99 | + def test_non_member_cannot_change_authorized_users(self): |
| 100 | + # A user who is not a member of the product type cannot retrieve it (the |
| 101 | + # viewset queryset is scoped to authorized product types), so the PATCH |
| 102 | + # is rejected before the member-management check is reached. |
| 103 | + client = self._client_for(self.bob) |
| 104 | + |
| 105 | + response = client.get(self.detail_url, secure=True) |
| 106 | + self.assertIn(response.status_code, {403, 404}, response.content[:500]) |
| 107 | + |
| 108 | + response = client.patch( |
| 109 | + self.detail_url, {"authorized_users": [self.bob.pk]}, format="json", secure=True, |
| 110 | + ) |
| 111 | + self.assertIn(response.status_code, {403, 404}, response.content[:500]) |
| 112 | + |
| 113 | + self.product_type.refresh_from_db() |
| 114 | + self.assertFalse(self.product_type.authorized_users.filter(pk=self.bob.pk).exists()) |
| 115 | + |
| 116 | + def test_manage_members_permission_can_change_authorized_users(self): |
| 117 | + # Product_Type_Manage_Members (staff) can update the membership list. |
| 118 | + client = self._client_for(self.admin) |
| 119 | + response = client.patch( |
| 120 | + self.detail_url, |
| 121 | + {"authorized_users": [self.alice.pk, self.bob.pk]}, |
| 122 | + format="json", secure=True, |
| 123 | + ) |
| 124 | + self.assertEqual(200, response.status_code, response.content[:500]) |
| 125 | + |
| 126 | + self.product_type.refresh_from_db() |
| 127 | + self.assertTrue(self.product_type.authorized_users.filter(pk=self.bob.pk).exists()) |
0 commit comments