Skip to content

Commit

Permalink
Mock _get_presigned_url
Browse files Browse the repository at this point in the history
  • Loading branch information
boxydog committed Oct 20, 2024
1 parent 5a998b7 commit 32e7fd1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
21 changes: 13 additions & 8 deletions python/src/functions/generate_presigned_url_and_send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ class PresignedException(Exception):
"""Exception generating presigned URL"""


def email_data(payload: SendTreasuryEmailLambdaPayload) -> EmailData:
"""Generate data for email.
1) Check to see if the s3 object exists:
treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/report.zip
2) If it does not, raise an exception and quit
3) Generate a pre-signed URL with an expiration date of 1 hour
"""
def _get_presigned_url(payload: SendTreasuryEmailLambdaPayload) -> str:
s3_client = boto3.client("s3")

# user = payload.user
Expand All @@ -58,7 +51,19 @@ def email_data(payload: SendTreasuryEmailLambdaPayload) -> EmailData:
)
if presigned_url is None:
raise PresignedException("Failed to generate signed-URL or file not found")
return presigned_url


def email_data(payload: SendTreasuryEmailLambdaPayload) -> EmailData:
"""Generate data for email.
1) Check to see if the s3 object exists:
treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/report.zip
2) If it does not, raise an exception and quit
3) Generate a pre-signed URL with an expiration date of 1 hour
"""

presigned_url = _get_presigned_url(payload)
return EmailData(
EMAIL_TITLE,
EMAIL_SUBJECT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
from unittest.mock import patch

from src.functions.generate_presigned_url_and_send_email import (
handle,
EMAIL_TITLE,
EMAIL_SUBJECT,
EMAIL_HTML,
EMAIL_TEXT,
email_data,
)
from src.lib.treasury_email_common import generate_email, EmailData
from src.lib.treasury_email_common import generate_email
from src.lib.logging import get_logger
from tests.test_utils import (
long_string_compare,
Expand All @@ -16,18 +16,17 @@


def test_generate_email():
logger = get_logger()
presigned_url = "https://example.com"

email_html, email_text, subject = generate_email(
EmailData(
EMAIL_TITLE,
EMAIL_SUBJECT,
EMAIL_HTML.format(url=presigned_url),
EMAIL_TEXT.format(url=presigned_url),
),
logger,
)
with patch(
"src.functions.generate_presigned_url_and_send_email._get_presigned_url"
) as get_presigned:
get_presigned.side_effect = lambda payload: presigned_url

email_html, email_text, subject = generate_email(
email_data(None),
get_logger(),
)

assert subject == EMAIL_SUBJECT

Expand Down
3 changes: 1 addition & 2 deletions python/tests/src/functions/test_send_failure_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@


def test_generate_email():
logger = get_logger()
email_html, email_text, subject = generate_email(email_data(None), logger)
email_html, email_text, subject = generate_email(email_data(None), get_logger())

assert subject == send_failure_email.EMAIL_SUBJECT

Expand Down

0 comments on commit 32e7fd1

Please sign in to comment.