Skip to content

Commit

Permalink
Fix DIP upload
Browse files Browse the repository at this point in the history
* Fix get levels of description API endpoint
* Fix ingest upload view
  • Loading branch information
replaceafill authored Sep 19, 2023
1 parent 8265c07 commit 5276afc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/dashboard/src/components/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ def get_levels_of_description(request):
each level of description.
"""
levels = models.LevelOfDescription.objects.all().order_by("sortorder")
response = [{level.id: level.name} for level in levels]
response = [{str(level.id): level.name} for level in levels]
return helpers.json_response(response)


Expand Down
6 changes: 5 additions & 1 deletion src/dashboard/src/components/ingest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
name="ingest_metadata_add_files",
),
path("upload/url/check/", views.ingest_upload_destination_url_check),
re_path(r"^(?P<uuid>" + settings.UUID_REGEX + ")/upload/$", views.ingest_upload),
re_path(
r"^(?P<uuid>" + settings.UUID_REGEX + ")/upload/$",
views.ingest_upload,
name="ingest_upload",
),
path("status/", views.ingest_status),
re_path(r"^status/(?P<uuid>" + settings.UUID_REGEX + ")/$", views.ingest_status),
re_path(
Expand Down
6 changes: 4 additions & 2 deletions src/dashboard/src/components/ingest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,16 @@ def ingest_upload(request, uuid):
access = models.Access.objects.get(sipuuid=uuid)
except:
access = models.Access(sipuuid=uuid)
access.target = pickle.dumps({"target": request.POST["target"]}, protocol=0)
access.target = pickle.dumps(
{"target": request.POST["target"]}, protocol=0
).decode()
access.save()
response = {"ready": True}
return helpers.json_response(response)
elif request.method == "GET":
try:
access = models.Access.objects.get(sipuuid=uuid)
data = pickle.loads(str(access.target))
data = pickle.loads(access.target.encode())
except:
raise Http404
return helpers.json_response(data)
Expand Down
18 changes: 18 additions & 0 deletions src/dashboard/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.utils.timezone import make_aware
from lxml import etree
from main.models import Job
from main.models import LevelOfDescription
from main.models import SIP
from main.models import Task
from main.models import Transfer
Expand Down Expand Up @@ -467,6 +468,23 @@ def test_task(self):
assert payload["time_ended"] == "2019-06-18T00:00:05"
assert payload["duration"] == 5

@e2e
def test_get_levels_of_description(self):
LevelOfDescription.objects.create(name="Item", sortorder=2)
LevelOfDescription.objects.create(name="Collection", sortorder=0)
LevelOfDescription.objects.create(name="Fonds", sortorder=1)
expected = ["Collection", "Fonds", "Item"]

resp = self.client.get(reverse("api:get_levels_of_description"))
payload = json.loads(resp.content.decode("utf8"))

result = []
for level_of_description in payload:
name = next(iter(level_of_description.values()))
result.append(name)

assert result == expected


class TestProcessingConfigurationAPI(TestCase):
fixtures = ["test_user"]
Expand Down
37 changes: 37 additions & 0 deletions src/dashboard/tests/test_ingest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import os
import pickle
from unittest import mock

import pytest
Expand All @@ -11,6 +13,7 @@
from django.test import TestCase
from django.test.client import Client
from django.urls import reverse
from main.models import Access
from main.models import DashboardSetting


Expand Down Expand Up @@ -77,6 +80,40 @@ def test_add_metadata_files_view(self):
)
assert title in response.content.decode("utf8")

def test_ingest_upload_get(self):
sip_uuid = "4060ee97-9c3f-4822-afaf-ebdf838284c3"
access_target = {"target": "description-slug"}
Access.objects.create(
sipuuid=sip_uuid,
target=pickle.dumps(access_target, protocol=0).decode(),
)

response = self.client.get(
reverse("ingest:ingest_upload", args=[sip_uuid]),
)

assert response.status_code == 200
assert json.loads(response.content) == access_target

def test_ingest_upload_post(self):
sip_uuid = "4060ee97-9c3f-4822-afaf-ebdf838284c3"
access_target = {"target": "description-slug"}

# Check there is no Access object associated with the SIP yet.
assert Access.objects.filter(sipuuid=sip_uuid).count() == 0

response = self.client.post(
reverse("ingest:ingest_upload", args=[sip_uuid]),
data=access_target,
)
assert response.status_code == 200
assert json.loads(response.content) == {"ready": True}

# An Access object was created for the SIP with the right target.
assert Access.objects.filter(sipuuid=sip_uuid).count() == 1
access = Access.objects.get(sipuuid=sip_uuid)
assert pickle.loads(access.target.encode()) == access_target


def _assert_file_node_properties_match_record(file_node, record):
assert file_node["type"] == "file"
Expand Down

0 comments on commit 5276afc

Please sign in to comment.