From 0b8414534c9769a679083bdc1a707fdf8c213e71 Mon Sep 17 00:00:00 2001 From: George Burton Date: Wed, 4 Sep 2024 12:11:32 +0100 Subject: [PATCH] remove all references to CORE_API_HOST --- django_app/redbox_app/settings.py | 3 - django_app/tests/management/test_commands.py | 82 +------------------ django_app/tests/views/test_document_views.py | 41 +--------- 3 files changed, 4 insertions(+), 122 deletions(-) diff --git a/django_app/redbox_app/settings.py b/django_app/redbox_app/settings.py index 3f23ffefe..9db73b76c 100644 --- a/django_app/redbox_app/settings.py +++ b/django_app/redbox_app/settings.py @@ -289,9 +289,6 @@ }, } -# link to core_api app -CORE_API_HOST = env.str("CORE_API_HOST") -CORE_API_PORT = env.int("CORE_API_PORT") # Email EMAIL_BACKEND_TYPE = env.str("EMAIL_BACKEND_TYPE") diff --git a/django_app/tests/management/test_commands.py b/django_app/tests/management/test_commands.py index c3e170448..9bc2c1d0e 100644 --- a/django_app/tests/management/test_commands.py +++ b/django_app/tests/management/test_commands.py @@ -1,16 +1,11 @@ -import re -import uuid from datetime import UTC, datetime, timedelta -from http import HTTPStatus from io import StringIO from unittest.mock import MagicMock, patch import elasticsearch import pytest -import requests from botocore.exceptions import UnknownClientMethodError from django.conf import settings -from django.core.files.uploadedfile import SimpleUploadedFile from django.core.management import CommandError, call_command from django.utils import timezone from freezegun import freeze_time @@ -22,68 +17,6 @@ # === check_file_status command tests === -@patch("redbox_app.redbox_core.models.File.delete_from_s3") -@patch("redbox_app.redbox_core.models.File.original_file.field.storage.save") -@pytest.mark.django_db() -def test_check_file_status(deletion_mock: MagicMock, put_mock: MagicMock, alice: User, requests_mock: Mocker): - # Based on: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/delete_object.html - deletion_mock.side_effect = { - "DeleteMarker": True, - } - put_mock.side_effect = yield - - # Given - def create_files(): - files = [] - - for i in range(3): - filename = f"original_file_{i}.txt" - files.append( - File.objects.create( - user=alice, - original_file=SimpleUploadedFile(filename, b"Lorem Ipsum."), - original_file_name=filename, - core_file_uuid=uuid.uuid4(), - ) - ) - return files - - file_in_core_api, file_with_surprising_status, file_not_in_core_api, file_core_api_error = create_files() - - matcher = re.compile(f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/file/[0-9a-f]|\\-/status") - - requests_mock.get( - matcher, - status_code=HTTPStatus.CREATED, - json={ - "processing_status": StatusEnum.processing, - }, - ) - requests_mock.get( - f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/file/{file_with_surprising_status.core_file_uuid}/status", - status_code=HTTPStatus.CREATED, - json={ - "processing_status": "this_is_a_surprising_string", - }, - ) - requests_mock.get( - f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/file/{file_not_in_core_api.core_file_uuid}/status", - status_code=HTTPStatus.NOT_FOUND, - ) - requests_mock.get( - f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/file/{file_core_api_error.core_file_uuid}/status", - exc=requests.exceptions.Timeout, - ) - # When - call_command("check_file_status") - - # Then - assert File.objects.get(id=file_in_core_api.id).status == StatusEnum.processing - assert File.objects.get(id=file_with_surprising_status.id).status == StatusEnum.processing - assert File.objects.get(id=file_not_in_core_api.id).status == StatusEnum.deleted - assert File.objects.get(id=file_core_api_error.id).status == StatusEnum.errored - - # === show_magiclink_url command tests === @@ -149,25 +82,12 @@ def test_command_output_with_valid_links(alice: User): ], ) @pytest.mark.django_db() -def test_delete_expired_files( - uploaded_file: File, requests_mock: Mocker, last_referenced: datetime, should_delete: bool -): +def test_delete_expired_files(uploaded_file: File, last_referenced: datetime, should_delete: bool): # Given mock_file = uploaded_file mock_file.last_referenced = last_referenced mock_file.save() - matcher = re.compile(f"http://{settings.CORE_API_HOST}:{settings.CORE_API_PORT}/file/[0-9a-f]|\\-") - requests_mock.delete( - matcher, - status_code=HTTPStatus.CREATED, - json={ - "key": mock_file.original_file_name, - "bucket": settings.BUCKET_NAME, - "uuid": str(uuid.uuid4()), - }, - ) - # When call_command("delete_expired_data") diff --git a/django_app/tests/views/test_document_views.py b/django_app/tests/views/test_document_views.py index 53e48b1e1..9230a155e 100644 --- a/django_app/tests/views/test_document_views.py +++ b/django_app/tests/views/test_document_views.py @@ -15,7 +15,7 @@ @pytest.mark.django_db() -def test_upload_view(alice, client, file_pdf_path: Path, s3_client, requests_mock): +def test_upload_view(alice, client, file_pdf_path: Path, s3_client): """ Given that the object store does not have a file with our test file in it When we POST our test file to /upload/ @@ -30,18 +30,6 @@ def test_upload_view(alice, client, file_pdf_path: Path, s3_client, requests_moc client.force_login(alice) - # we mock the response from the core-api - 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 file_pdf_path.open("rb") as f: response = client.post("/upload/", {"uploadDocs": f}) @@ -51,7 +39,7 @@ def test_upload_view(alice, client, file_pdf_path: Path, s3_client, requests_moc @pytest.mark.django_db() -def test_document_upload_status(client, alice, file_pdf_path: Path, s3_client, requests_mock): +def test_document_upload_status(client, alice, file_pdf_path: Path, s3_client): file_name = file_pdf_path.name # we begin by removing any file in minio that has this key @@ -61,17 +49,6 @@ def test_document_upload_status(client, alice, file_pdf_path: Path, s3_client, r client.force_login(alice) 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 file_pdf_path.open("rb") as f: response = client.post("/upload/", {"uploadDocs": f}) @@ -83,19 +60,7 @@ def test_document_upload_status(client, alice, file_pdf_path: Path, s3_client, r @pytest.mark.django_db() -def test_upload_view_duplicate_files(alice, bob, client, file_pdf_path: Path, s3_client, requests_mock): - # we mock the response from the core-api - mocked_response = { - "key": "file_key", - "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, - ) - +def test_upload_view_duplicate_files(alice, bob, client, file_pdf_path: Path, s3_client): previous_count = count_s3_objects(s3_client) client.force_login(alice)