Skip to content

Commit ceb1269

Browse files
committed
🥅(backend) link role could be updated when restricted document
When a document was restricted, the link role could be updated from "link-configuration" and gives a 200 response, but the change did not have any effect because of a restriction in LinkReachChoices. We added a validation step to ensure that the link role can only be updated if the document is not restricted.
1 parent 0cf8b9d commit ceb1269

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/backend/core/api/serializers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,23 @@ class Meta:
493493
"link_reach",
494494
]
495495

496+
def validate(self, attrs):
497+
"""Validate that link_role is compatible with link_reach."""
498+
link_reach = attrs.get("link_reach", self.instance.link_reach)
499+
link_role = attrs.get("link_role")
500+
501+
# If link_reach is restricted, link_role should not be set to anything meaningful
502+
if link_reach == models.LinkReachChoices.RESTRICTED and link_role in [
503+
models.LinkRoleChoices.READER,
504+
models.LinkRoleChoices.EDITOR,
505+
]:
506+
msg = _(
507+
"Cannot set link_role when link_reach is 'restricted'. Change link_reach first."
508+
)
509+
raise serializers.ValidationError({"link_role": msg})
510+
511+
return attrs
512+
496513

497514
class DocumentDuplicationSerializer(serializers.Serializer):
498515
"""

src/backend/core/tests/documents/test_api_documents_link_configuration.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def test_api_documents_link_configuration_update_authenticated_related_success(
133133
client = APIClient()
134134
client.force_login(user)
135135

136-
document = factories.DocumentFactory()
136+
document = factories.DocumentFactory(
137+
link_reach=models.LinkReachChoices.AUTHENTICATED,
138+
link_role=models.LinkRoleChoices.READER,
139+
)
137140
if via == USER:
138141
factories.UserDocumentAccessFactory(document=document, user=user, role=role)
139142
elif via == TEAM:
@@ -143,7 +146,10 @@ def test_api_documents_link_configuration_update_authenticated_related_success(
143146
)
144147

145148
new_document_values = serializers.LinkDocumentSerializer(
146-
instance=factories.DocumentFactory()
149+
instance=factories.DocumentFactory(
150+
link_reach=models.LinkReachChoices.PUBLIC,
151+
link_role=models.LinkRoleChoices.EDITOR,
152+
)
147153
).data
148154

149155
with mock_reset_connections(document.id):
@@ -158,3 +164,37 @@ def test_api_documents_link_configuration_update_authenticated_related_success(
158164
document_values = serializers.LinkDocumentSerializer(instance=document).data
159165
for key, value in document_values.items():
160166
assert value == new_document_values[key]
167+
168+
169+
def test_api_documents_link_configuration_update_role_restricted_forbidden():
170+
"""
171+
Test that trying to set link_role on a document with restricted link_reach
172+
returns a validation error.
173+
"""
174+
user = factories.UserFactory()
175+
client = APIClient()
176+
client.force_login(user)
177+
178+
document = factories.DocumentFactory(
179+
link_reach=models.LinkReachChoices.RESTRICTED,
180+
link_role=models.LinkRoleChoices.READER,
181+
)
182+
183+
factories.UserDocumentAccessFactory(
184+
document=document, user=user, role=models.RoleChoices.OWNER
185+
)
186+
187+
new_data = {"link_role": models.LinkRoleChoices.EDITOR}
188+
189+
response = client.put(
190+
f"/api/v1.0/documents/{document.id!s}/link-configuration/",
191+
new_data,
192+
format="json",
193+
)
194+
195+
assert response.status_code == 400
196+
assert "link_role" in response.json()
197+
assert (
198+
"Cannot set link_role when link_reach is 'restricted'"
199+
in response.json()["link_role"][0]
200+
)

0 commit comments

Comments
 (0)