Skip to content

Commit

Permalink
Merge pull request #1803 from uktrade/minio
Browse files Browse the repository at this point in the history
Minio
  • Loading branch information
kevincarrogan authored Feb 5, 2024
2 parents bfa87fe + 0b0d8b2 commit 0ce06e9
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 118 deletions.
153 changes: 77 additions & 76 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions api/cases/tests/test_case_documents.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import uuid
from unittest import mock

from django.http import StreamingHttpResponse
from moto import mock_aws

from django.conf import settings
from django.http import FileResponse
from django.urls import reverse
from rest_framework import status

from lite_content.lite_api.strings import Documents
from test_helpers.clients import DataTestClient

from api.documents.libraries.s3_operations import init_s3_client


class CaseDocumentsTests(DataTestClient):
def setUp(self):
Expand All @@ -27,6 +31,7 @@ def test_can_view_all_documents_on_a_case(self):
self.assertEqual(len(response_data["documents"]), 2)


@mock_aws
class CaseDocumentDownloadTests(DataTestClient):
def setUp(self):
super().setUp()
Expand All @@ -35,16 +40,26 @@ def setUp(self):
self.file = self.create_case_document(self.case, self.gov_user, "Test")
self.path = "cases:document_download"

@mock.patch("api.documents.libraries.s3_operations.get_object")
def test_download_case_document_success(self, get_object_function):
get_object_function.return_value = None
s3 = init_s3_client()
s3.create_bucket(
Bucket=settings.AWS_STORAGE_BUCKET_NAME,
CreateBucketConfiguration={
"LocationConstraint": settings.AWS_REGION,
},
)
s3.put_object(
Bucket=settings.AWS_STORAGE_BUCKET_NAME,
Key=self.file.s3_key,
Body=b"test",
)

def test_download_case_document_success(self):
url = reverse(self.path, kwargs={"case_pk": self.case.id, "document_pk": self.file.id})

response = self.client.get(url, **self.exporter_headers)

get_object_function.assert_called_once()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(isinstance(response, StreamingHttpResponse))
self.assertTrue(isinstance(response, FileResponse))
self.assertEqual(response.headers["content-disposition"], 'attachment; filename="Test"')

def test_download_case_document_invalid_organisation_failure(self):
Expand Down
2 changes: 2 additions & 0 deletions api/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,13 @@
raise Exception("S3 Bucket not bound to environment")

aws_credentials = VCAP_SERVICES["aws-s3-bucket"][0]["credentials"]
AWS_ENDPOINT_URL = None
AWS_ACCESS_KEY_ID = aws_credentials["aws_access_key_id"]
AWS_SECRET_ACCESS_KEY = aws_credentials["aws_secret_access_key"]
AWS_REGION = aws_credentials["aws_region"]
AWS_STORAGE_BUCKET_NAME = aws_credentials["bucket_name"]
else:
AWS_ENDPOINT_URL = env("AWS_ENDPOINT_URL", default=None)
AWS_ACCESS_KEY_ID = env("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env("AWS_SECRET_ACCESS_KEY")
AWS_REGION = env("AWS_REGION")
Expand Down
2 changes: 2 additions & 0 deletions api/conf/settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
LOGGING = {"version": 1, "disable_existing_loggers": True}

SUPPRESS_TEST_OUTPUT = True

AWS_ENDPOINT_URL = None
Loading

0 comments on commit 0ce06e9

Please sign in to comment.