Skip to content

Commit 14870b3

Browse files
committed
fixup! ✨(backend) allow masking documents from the list view
1 parent fbb6037 commit 14870b3

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

src/backend/core/api/viewsets.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,10 +1108,7 @@ def favorite(self, request, *args, **kwargs):
11081108
document=document, user=user
11091109
).delete()
11101110
if deleted:
1111-
return drf.response.Response(
1112-
{"detail": "Document unmarked as favorite"},
1113-
status=drf.status.HTTP_204_NO_CONTENT,
1114-
)
1111+
return drf.response.Response(status=drf.status.HTTP_204_NO_CONTENT)
11151112
return drf.response.Response(
11161113
{"detail": "Document was already not marked as favorite"},
11171114
status=drf.status.HTTP_200_OK,
@@ -1124,37 +1121,36 @@ def mask(self, request, *args, **kwargs):
11241121
document = self.get_object()
11251122
user = request.user
11261123

1127-
if request.method == "POST":
1128-
link_trace, created = models.LinkTrace.objects.get_or_create(
1129-
document=document, user=user, defaults={"is_masked": True}
1124+
try:
1125+
link_trace = models.LinkTrace.objects.get(document=document, user=user)
1126+
except models.LinkTrace.DoesNotExist:
1127+
return drf.response.Response(
1128+
{"detail": "User never accessed this document before."},
1129+
status=status.HTTP_400_BAD_REQUEST,
11301130
)
1131-
if not created and link_trace.is_masked:
1131+
1132+
if request.method == "POST":
1133+
if link_trace.is_masked:
11321134
return drf.response.Response(
11331135
{"detail": "Document was already masked"},
11341136
status=drf.status.HTTP_200_OK,
11351137
)
11361138
link_trace.is_masked = True
1137-
link_trace.save()
1139+
link_trace.save(update_fields=["is_masked"])
11381140
return drf.response.Response(
11391141
{"detail": "Document was masked"},
11401142
status=drf.status.HTTP_201_CREATED,
11411143
)
11421144

11431145
# Handle DELETE method to unmask document
1144-
try:
1145-
link_trace = models.LinkTrace.objects.get(document=document, user=user)
1146-
except models.LinkTrace.DoesNotExist:
1146+
if not link_trace.is_masked:
11471147
return drf.response.Response(
11481148
{"detail": "Document was already not masked"},
11491149
status=drf.status.HTTP_200_OK,
11501150
)
1151-
11521151
link_trace.is_masked = False
1153-
link_trace.save()
1154-
return drf.response.Response(
1155-
{"detail": "Document was unmasked"},
1156-
status=drf.status.HTTP_204_NO_CONTENT,
1157-
)
1152+
link_trace.save(update_fields=["is_masked"])
1153+
return drf.response.Response(status=drf.status.HTTP_204_NO_CONTENT)
11581154

11591155
@drf.decorators.action(detail=True, methods=["post"], url_path="attachment-upload")
11601156
def attachment_upload(self, request, *args, **kwargs):

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def test_api_document_favorite_list_authenticated_with_favorite():
4141
client = APIClient()
4242
client.force_login(user)
4343

44-
# User don't have access to this document (e.g the user had access and this
45-
# access was removed. It should not be in the favorite list anymore.
44+
# If the user doesn't have access to this document (e.g the user had access
45+
# and this access was removed), it should not be in the favorite list anymore.
4646
factories.DocumentFactory(favorited_by=[user])
4747

4848
document = factories.UserDocumentAccessFactory(

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def test_api_document_mask_authenticated_post_allowed(reach, has_role):
5454
if has_role:
5555
models.DocumentAccess.objects.create(document=document, user=user)
5656

57+
# Try masking the document without a link trace
58+
response = client.post(f"/api/v1.0/documents/{document.id!s}/mask/")
59+
assert response.status_code == 400
60+
assert response.json() == {"detail": "User never accessed this document before."}
61+
assert not models.LinkTrace.objects.filter(document=document, user=user).exists()
62+
63+
models.LinkTrace.objects.create(document=document, user=user)
5764
# Mask document
5865
response = client.post(f"/api/v1.0/documents/{document.id!s}/mask/")
5966

@@ -211,6 +218,10 @@ def test_api_document_mask_authenticated_delete_allowed(reach, has_role):
211218
response = client.delete(f"/api/v1.0/documents/{document.id!s}/mask/")
212219

213220
assert response.status_code == 204
221+
assert response.content == b"" # No body
222+
assert response.text == "" # Empty decoded text
223+
assert "Content-Type" not in response.headers # No Content-Type for 204
224+
214225
assert models.LinkTrace.objects.filter(
215226
document=document, user=user, is_masked=False
216227
).exists()
@@ -259,14 +270,21 @@ def test_api_document_mask_authenticated_delete_not_masked_allowed(reach, has_ro
259270
if has_role:
260271
models.DocumentAccess.objects.create(document=document, user=user)
261272

262-
# Try to unmask when no entry exists
273+
# Try unmasking the document without a link trace
274+
response = client.delete(f"/api/v1.0/documents/{document.id!s}/mask/")
275+
assert response.status_code == 400
276+
assert response.json() == {"detail": "User never accessed this document before."}
277+
assert not models.LinkTrace.objects.filter(document=document, user=user).exists()
278+
279+
models.LinkTrace.objects.create(document=document, user=user, is_masked=False)
280+
# Unmask document
263281
response = client.delete(f"/api/v1.0/documents/{document.id!s}/mask/")
264282

265283
assert response.status_code == 200
266284
assert response.json() == {"detail": "Document was already not masked"}
267-
assert (
268-
models.LinkTrace.objects.filter(document=document, user=user).exists() is False
269-
)
285+
assert models.LinkTrace.objects.filter(
286+
document=document, user=user, is_masked=False
287+
).exists()
270288

271289

272290
def test_api_document_mask_authenticated_delete_not_masked_forbidden():
@@ -310,6 +328,7 @@ def test_api_document_mask_authenticated_post_unmark_then_mark_again_allowed(
310328
document = factories.DocumentFactory(link_reach=reach)
311329
if has_role:
312330
models.DocumentAccess.objects.create(document=document, user=user)
331+
models.LinkTrace.objects.create(document=document, user=user, is_masked=False)
313332

314333
url = f"/api/v1.0/documents/{document.id!s}/mask/"
315334

@@ -320,6 +339,9 @@ def test_api_document_mask_authenticated_post_unmark_then_mark_again_allowed(
320339
# Unmask document
321340
response = client.delete(url)
322341
assert response.status_code == 204
342+
assert response.content == b"" # No body
343+
assert response.text == "" # Empty decoded text
344+
assert "Content-Type" not in response.headers # No Content-Type for 204
323345

324346
# Mask document again
325347
response = client.post(url)

0 commit comments

Comments
 (0)