Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions arches/extensions/querysets/rest_framework/view_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ def perform_create(self, serializer):
self.validate_tile_data_and_save(serializer)

def perform_update(self, serializer):
resource = serializer.validated_data.get("resourceinstance")
if resource is not None and not user_can_edit_resource(
Comment thread
apeters marked this conversation as resolved.
Resolved
user=self.request.user, resource=resource
):
raise PermissionDenied
if (
Comment thread
apeters marked this conversation as resolved.
Resolved
isinstance(serializer.instance, TileModel)
and "resourceinstance" in serializer.validated_data
and getattr(resource, "pk", None) != serializer.instance.resourceinstance_id
):
raise ValidationError(
Comment thread
apeters marked this conversation as resolved.
Resolved
{"resourceinstance": [_("A tile cannot be moved to another resource.")]}
)
self.validate_tile_data_and_save(serializer)

def perform_destroy(self, instance):
Expand Down
1 change: 1 addition & 0 deletions releases/8.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
- Under the default-deny permission framework, the querysets API and resource-instance widgets no longer show resources the user can't read [#13096](https://github.com/archesproject/arches/pull/13096)
- Replace CKEditor rich text editor with an Arches rich text editor using Tiptap [#13029](https://github.com/archesproject/arches/pull/13029)
- Fix the map card's default center not updating when zooming with the scroll wheel or a pinch, which zooms around the pointer and so also moves the center [#10364](https://github.com/archesproject/arches/issues/10364)
- Disallow moving an existing tile to a different resource instance [#13140](https://github.com/archesproject/arches/issues/13140)

### Deprecations

Expand Down
75 changes: 75 additions & 0 deletions tests/extensions/querysets/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,81 @@ def test_resource_put_leaves_tiles_parented_in_their_own_nodegroup(self):
self.assertEqual(response.status_code, HTTPStatus.OK, response.json())
self.assertTrue(TileModel.objects.filter(pk=misparented_tile.pk).exists())

def test_update_tile_rejects_moving_the_tile_to_another_resource(self):
tile = self.resource_42.aliased_data.datatypes_1
update_url = reverse(
"arches_querysets:api-tile",
kwargs={
"graph": "datatype_lookups",
"nodegroup_alias": "datatypes_1",
"pk": tile.pk,
},
)
request_body = {"resourceinstance": str(self.resource_none.pk)}

self.client.login(username="dev", password="dev")
response = self.client.patch(
update_url, request_body, content_type="application/json"
)

self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
self.assertIn("resourceinstance", response.json())
self.assertEqual(
TileModel.objects.get(pk=tile.pk).resourceinstance_id, self.resource_42.pk
)

def test_update_tile_rejects_a_null_resourceinstance(self):
tile = self.resource_42.aliased_data.datatypes_1
update_url = reverse(
"arches_querysets:api-tile",
kwargs={
"graph": "datatype_lookups",
"nodegroup_alias": "datatypes_1",
"pk": tile.pk,
},
)

self.client.login(username="dev", password="dev")
response = self.client.patch(
update_url, {"resourceinstance": None}, content_type="application/json"
)

self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
self.assertIn("resourceinstance", response.json())
self.assertEqual(
TileModel.objects.get(pk=tile.pk).resourceinstance_id, self.resource_42.pk
)

def test_update_tile_move_to_uneditable_resource_is_forbidden(self):
tile = self.resource_42.aliased_data.datatypes_1
update_url = reverse(
"arches_querysets:api-tile",
kwargs={
"graph": "datatype_lookups",
"nodegroup_alias": "datatypes_1",
"pk": tile.pk,
},
)
request_body = {"resourceinstance": str(self.resource_none.pk)}

def can_edit_only_resource_42(*, user, resource=None, resourceid=None):
resource_id = resource.pk if resource is not None else resourceid
return str(resource_id) == str(self.resource_42.pk)

self.client.login(username="dev", password="dev")
with patch(
"arches.extensions.querysets.rest_framework.view_mixins.user_can_edit_resource",
side_effect=can_edit_only_resource_42,
):
response = self.client.patch(
update_url, request_body, content_type="application/json"
)

self.assertEqual(response.status_code, HTTPStatus.FORBIDDEN)
self.assertEqual(
TileModel.objects.get(pk=tile.pk).resourceinstance_id, self.resource_42.pk
)

def test_update_tile_rejects_a_related_resource_without_a_resource_id(self):
update_url = reverse(
"arches_querysets:api-tile",
Expand Down
Loading