Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling additional cases in various transcription-related tests #2592

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
72 changes: 71 additions & 1 deletion concordia/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from datetime import date, timedelta
from unittest.mock import patch

Expand Down Expand Up @@ -771,6 +772,7 @@ def test_asset_reservation_tombstone_expiration(self):
def test_transcription_save(self):
asset = create_asset()

# Test when Turnstile validation failes
with patch("concordia.turnstile.fields.TurnstileField.validate") as mock:
mock.side_effect = forms.ValidationError(
"Testing error", code="invalid_turnstile"
Expand Down Expand Up @@ -798,14 +800,42 @@ def test_transcription_save(self):
data = self.assertValidJSON(resp, expected_status=409)
self.assertIn("error", data)

# This should work with the chain specified:
# If a transcription contains a URL, it should return an error
resp = self.client.post(
reverse("save-transcription", args=(asset.pk,)),
data={
"text": "http://example.com",
"supersedes": asset.transcription_set.get().pk,
},
)
data = self.assertValidJSON(resp, expected_status=400)
self.assertIn("error", data)

# Test that it correctly works when supersedes is set
resp = self.client.post(
reverse("save-transcription", args=(asset.pk,)),
data={"text": "test", "supersedes": asset.transcription_set.get().pk},
)
data = self.assertValidJSON(resp, expected_status=201)
self.assertIn("submissionUrl", data)

# Test that it correctly works when supersedes is set and confirm
# ocr_originaed is properly set
transcription = asset.transcription_set.order_by("pk").last()
transcription.ocr_originated = True
transcription.save()
resp = self.client.post(
reverse("save-transcription", args=(asset.pk,)),
data={
"text": "test",
"supersedes": asset.transcription_set.order_by("pk").last().pk,
},
)
data = self.assertValidJSON(resp, expected_status=201)
self.assertIn("submissionUrl", data)
new_transcription = asset.transcription_set.order_by("pk").last()
self.assertTrue(new_transcription.ocr_originated)

# We should see an error if you attempt to supersede a transcription
# which has already been superseded:
resp = self.client.post(
Expand All @@ -818,6 +848,30 @@ def test_transcription_save(self):
data = self.assertValidJSON(resp, expected_status=409)
self.assertIn("error", data)

# We should get an error if you attempt to supersede a transcription
# that doesn't exist
resp = self.client.post(
reverse("save-transcription", args=(asset.pk,)),
data={
"text": "test",
"supersedes": sys.maxsize,
},
)
data = self.assertValidJSON(resp, expected_status=400)
self.assertIn("error", data)

# We should get an error if you attempt to supersede with
# with a pk that is invalid (i.e., a string instead of int)
resp = self.client.post(
reverse("save-transcription", args=(asset.pk,)),
data={
"text": "test",
"supersedes": "bad-pk",
},
)
data = self.assertValidJSON(resp, expected_status=400)
self.assertIn("error", data)

# A logged in user can take over from an anonymous user:
self.login_user()
resp = self.client.post(
Expand Down Expand Up @@ -1097,6 +1151,22 @@ def test_tag_submission(self):
self.assertEqual(sorted(test_tags), data["user_tags"])
self.assertEqual(sorted(test_tags), data["all_tags"])

def test_invalid_tag_submission(self):
asset = create_asset()

self.login_user()

test_tags = ["foo", "bar"]

with patch("concordia.models.Tag.full_clean") as mock:
mock.side_effect = forms.ValidationError("Testing error")
resp = self.client.post(
reverse("submit-tags", kwargs={"asset_pk": asset.pk}),
data={"tags": test_tags},
)
data = self.assertValidJSON(resp, expected_status=400)
self.assertIn("error", data)

def test_tag_submission_with_diacritics(self):
asset = create_asset()

Expand Down
17 changes: 10 additions & 7 deletions concordia/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,14 +1567,17 @@ def get_transcription_superseded(asset, supersedes_pk):
else:
superseded = None
else:
if asset.transcription_set.filter(supersedes=supersedes_pk).exists():
return JsonResponse(
{"error": "This transcription has been superseded"}, status=409
)

try:
superseded = asset.transcription_set.get(pk=supersedes_pk)
except Transcription.DoesNotExist:
if asset.transcription_set.filter(supersedes=supersedes_pk).exists():
return JsonResponse(
{"error": "This transcription has been superseded"}, status=409
)

try:
superseded = asset.transcription_set.get(pk=supersedes_pk)
except Transcription.DoesNotExist:
return JsonResponse({"error": "Invalid supersedes value"}, status=400)
except ValueError:
return JsonResponse({"error": "Invalid supersedes value"}, status=400)
return superseded

Expand Down