Skip to content

Commit

Permalink
remove all references to CORE_API_HOST
Browse files Browse the repository at this point in the history
  • Loading branch information
George Burton committed Sep 4, 2024
1 parent a4fb89d commit 0b84145
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 122 deletions.
3 changes: 0 additions & 3 deletions django_app/redbox_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
82 changes: 1 addition & 81 deletions django_app/tests/management/test_commands.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 ===


Expand Down Expand Up @@ -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")

Expand Down
41 changes: 3 additions & 38 deletions django_app/tests/views/test_document_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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})

Expand All @@ -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
Expand All @@ -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})

Expand All @@ -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)

Expand Down

0 comments on commit 0b84145

Please sign in to comment.