Skip to content

Commit

Permalink
call core api delete from django delete
Browse files Browse the repository at this point in the history
  • Loading branch information
rachaelcodes committed May 20, 2024
1 parent b37f2e1 commit d61ce14
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
7 changes: 7 additions & 0 deletions django_app/redbox_app/redbox_core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ def get_file_status(self, file_id: UUID, user: User) -> SimpleNamespace:
response.raise_for_status()
response_data = response.json(object_hook=lambda d: SimpleNamespace(**d))
return response_data

def delete_file(self, file_id: UUID, user: User) -> SimpleNamespace:
url = self.url / "file" / str(file_id)
response = requests.delete(url, headers={"Authorization": user.get_bearer_token()}, timeout=60)
response.raise_for_status()
response_data = response.json(object_hook=lambda d: SimpleNamespace(**d))
return response_data
19 changes: 16 additions & 3 deletions django_app/redbox_app/redbox_core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,27 @@ def ingest_file(uploaded_file: UploadedFile, user: User) -> list[str]:
@login_required
def remove_doc_view(request, doc_id: uuid):
file = File.objects.get(pk=doc_id)
errors: list[str] = []

if request.method == "POST":
logger.info("Removing document: %s", request.POST["doc_id"])
file.delete()
api = CoreApiClient(host=settings.CORE_API_HOST, port=settings.CORE_API_PORT)

try:
api.delete_file(file.core_file_uuid, request.user)
except HTTPError as e:
logger.error("Error deleting file object %s.", file, exc_info=e)
file.delete()
errors.append("failed to connect to core-api")

else:
logger.info("Removing document: %s", request.POST["doc_id"])
file.delete()
return redirect("documents")

return render(
request,
template_name="remove-doc.html",
context={"request": request, "doc_id": doc_id, "doc_name": file.name},
context={"request": request, "doc_id": doc_id, "doc_name": file.name, "errors": errors},
)


Expand Down
43 changes: 43 additions & 0 deletions django_app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,49 @@ def test_upload_view_no_file(alice, client):
assert "No document selected" in str(response.content)


@pytest.mark.django_db
def test_remove_doc_view(client: Client, alice: User, file_pdf_path: str, s3_client: Client, requests_mock: Mocker):
file_name = file_pdf_path.split("/")[-1]

client.force_login(alice)
# we begin by removing any file in minio that has this key
s3_client.delete_object(Bucket=settings.BUCKET_NAME, Key=file_name.replace(" ", "_"))

previous_count = count_s3_objects(s3_client)

mocked_response = {
"key": file_name,
"bucket": settings.BUCKET_NAME,
"uuid": str(uuid.uuid4()),
}
requests_mock.post(
f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/file",
status_code=201,
json=mocked_response,
)

with open(file_pdf_path, "rb") as f:
# create file before testing deletion
client.post("/upload/", {"uploadDoc": f})
assert file_exists(s3_client, file_name)
assert count_s3_objects(s3_client) == previous_count + 1

new_file = File.objects.filter(user=alice).order_by("-created_at")[0]
requests_mock.delete(
f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/file/{new_file.core_file_uuid}",
status_code=201,
json=mocked_response,
)

client.post(f"/remove-doc/{new_file.id}", {"doc_id": new_file.id})
assert not file_exists(s3_client, file_name)
assert count_s3_objects(s3_client) == previous_count
assert requests_mock.request_history[-1].method == "DELETE"

with pytest.raises(File.DoesNotExist):
File.objects.get(id=new_file.id)


@pytest.mark.django_db
def test_post_message_to_new_session(alice: User, client: Client, requests_mock: Mocker):
# Given
Expand Down

0 comments on commit d61ce14

Please sign in to comment.