Skip to content

Commit

Permalink
500 error if reach upload or cya page with no session
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Rees authored and Morgan Rees committed Jan 7, 2025
1 parent 3eec0a8 commit 44f96e9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
27 changes: 14 additions & 13 deletions django_app/utils/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ def generate_presigned_url(s3_storage: Any, s3_file_object: str) -> str:
def get_all_session_files(s3_storage: S3Boto3Storage, session: SessionBase) -> dict[str, Any]:
"""Gets all files that a user has uploaded in a session."""
s3_client = get_s3_client_from_storage(s3_storage=s3_storage)
response = s3_client.list_objects_v2(Bucket=s3_storage.bucket.name, Prefix=session.session_key)
session_files = {}

user_uploaded_files = get_user_uploaded_files(session)
for content in response.get("Contents", []):
key = content["Key"]
file_name = key.rpartition("/")[2]

# checking that a file with this name was uploaded in the session
if file_name in user_uploaded_files:
session_files[key] = {
"file_name": file_name,
"url": reverse("report_a_suspected_breach:download_document", kwargs={"file_name": file_name}),
}
if not session.is_empty():
response = s3_client.list_objects_v2(Bucket=s3_storage.bucket.name, Prefix=session.session_key)

user_uploaded_files = get_user_uploaded_files(session)
for content in response.get("Contents", []):
key = content["Key"]
file_name = key.rpartition("/")[2]

# checking that a file with this name was uploaded in the session
if file_name in user_uploaded_files:
session_files[key] = {
"file_name": file_name,
"url": reverse("report_a_suspected_breach:download_document", kwargs={"file_name": file_name}),
}
return session_files


Expand Down
57 changes: 39 additions & 18 deletions tests/test_unit/test_report_a_suspected_breach/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
# TODO: To be updated in DST-512
# from unittest.mock import MagicMock, Mock, patch
#
# from utils.s3 import get_all_session_files
#
#
# @patch("utils.s3.get_s3_client_from_storage")
# def test_get_all_session_files(mocked_get_s3_client):
# mocked_get_s3_client.return_value = MagicMock()
# mocked_get_s3_client.return_value.list_objects_v2 = MagicMock(return_value={"Contents": [{"Key": "test.png"}]})
#
# session_object = MagicMock()
# session_object.get = MagicMock(return_value=["test.png"])
# session_object.session_key = "test_session_key"
#
# session_files = get_all_session_files(Mock(), session_object)
# assert len(session_files) == 1
# assert "test.png" in session_files
from unittest.mock import MagicMock, Mock, patch

from django.core.cache import cache
from utils.s3 import get_all_session_files


@patch("utils.s3.get_s3_client_from_storage")
def test_get_all_session_files(mocked_get_s3_client):
mocked_get_s3_client.return_value = MagicMock()
mocked_get_s3_client.return_value.list_objects_v2 = MagicMock(return_value={"Contents": [{"Key": "test.png"}]})

session_object = MagicMock()
session_object.is_empty = MagicMock(return_value=False)
session_object.get = MagicMock(return_value=["test.png"])

session_object.session_key = "test_session_key"
redis_cache_key = f"{session_object.session_key}"
cache.set(redis_cache_key, "test.png")

session_files = get_all_session_files(Mock(), session_object)
assert len(session_files) == 1
assert "test.png" in session_files


@patch("utils.s3.get_s3_client_from_storage")
def test_no_session_returns_empty_session_files(mocked_get_s3_client):
mocked_get_s3_client.return_value = MagicMock()
mocked_get_s3_client.return_value.list_objects_v2 = MagicMock(return_value={"Contents": [{"Key": "test.png"}]})

session_object = MagicMock()
session_object.is_empty = MagicMock(return_value=True)
session_object.get = MagicMock(return_value=["test.png"])

session_object.session_key = "test_session_key"
redis_cache_key = f"{session_object.session_key}"
cache.set(redis_cache_key, "test.png")

session_files = get_all_session_files(Mock(), session_object)
assert len(session_files) == 0

0 comments on commit 44f96e9

Please sign in to comment.