-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'setup/sendgrid' of github.com:HackAtUCI/irvinehacks-sit…
…e-2024 into setup/sendgrid
- Loading branch information
Showing
3 changed files
with
87 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
from enum import Enum | ||
from typing import Protocol | ||
|
||
from pydantic import EmailStr | ||
|
||
from services import sendgrid_handler | ||
from services.sendgrid_handler import PersonalizationData | ||
from services.sendgrid_handler import Template | ||
|
||
IH_SENDER = ("[email protected]", "IrvineHacks 2024 Applications") | ||
|
||
|
@@ -15,27 +14,27 @@ class ContactInfo(Protocol): | |
last_name: str | ||
|
||
|
||
class Template(str, Enum): | ||
# TODO: provide actual template IDs | ||
CONFIRMATION_EMAIL = "d-e053b7a4bedd449bafda46c6512d531c" | ||
GUEST_TOKEN = "d-d962252a87844188880d7bd19a5b2fbf" | ||
|
||
|
||
async def send_application_confirmation_email(user: ContactInfo) -> None: | ||
"""Send a confirmation email after a user submits an application. | ||
Will propagate exceptions from SendGrid.""" | ||
send_data: PersonalizationData = { | ||
"email": user.email, | ||
"first_name": user.first_name, | ||
"last_name": user.last_name, | ||
} | ||
await sendgrid_handler.send_email(Template.CONFIRMATION_EMAIL, IH_SENDER, send_data) | ||
await sendgrid_handler.send_email( | ||
Template.CONFIRMATION_EMAIL, | ||
IH_SENDER, | ||
{ | ||
"email": user.email, | ||
"first_name": user.first_name, | ||
"last_name": user.last_name, | ||
}, | ||
) | ||
|
||
|
||
async def send_guest_login_email(email: EmailStr, passphrase: str) -> None: | ||
"""Email login passphrase to guest.""" | ||
send_data: PersonalizationData = { | ||
"email": email, | ||
"passphrase": passphrase, | ||
} | ||
await sendgrid_handler.send_email(Template.GUEST_TOKEN, IH_SENDER, send_data) | ||
await sendgrid_handler.send_email( | ||
Template.GUEST_TOKEN, | ||
IH_SENDER, | ||
{ | ||
"email": email, | ||
"passphrase": passphrase, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ | |
from httpx import HTTPStatusError, Request, Response | ||
|
||
from services import sendgrid_handler | ||
from services.sendgrid_handler import PersonalizationData | ||
from services.sendgrid_handler import ConfirmationPersonalization, Template | ||
|
||
SAMPLE_SENDER = ("[email protected]", "No Reply IrvineHacks") | ||
SAMPLE_RECIPIENTS: list[PersonalizationData] = [ | ||
SAMPLE_RECIPIENTS: list[ConfirmationPersonalization] = [ | ||
{ | ||
"email": "[email protected]", | ||
"first_name": "Hacker", | ||
|
@@ -31,7 +31,9 @@ async def test_send_single_email(mock_AsyncClient: AsyncMock) -> None: | |
|
||
recipient_data = SAMPLE_RECIPIENTS[0] | ||
|
||
await sendgrid_handler.send_email("my-template-id", SAMPLE_SENDER, recipient_data) | ||
await sendgrid_handler.send_email( | ||
Template.CONFIRMATION_EMAIL, SAMPLE_SENDER, recipient_data | ||
) | ||
mock_client.send_mail_v3.assert_awaited_once_with( | ||
body={ | ||
"from": {"name": SAMPLE_SENDER[1], "email": SAMPLE_SENDER[0]}, | ||
|
@@ -41,7 +43,7 @@ async def test_send_single_email(mock_AsyncClient: AsyncMock) -> None: | |
"dynamic_template_data": recipient_data, | ||
} | ||
], | ||
"template_id": "my-template-id", | ||
"template_id": Template.CONFIRMATION_EMAIL, | ||
} | ||
) | ||
|
||
|
@@ -54,7 +56,7 @@ async def test_send_multiple_emails(mock_AsyncClient: AsyncMock) -> None: | |
mock_AsyncClient.return_value.__aenter__.return_value = mock_client | ||
|
||
await sendgrid_handler.send_email( | ||
"my-template-id", SAMPLE_SENDER, SAMPLE_RECIPIENTS, True | ||
Template.CONFIRMATION_EMAIL, SAMPLE_SENDER, SAMPLE_RECIPIENTS, True | ||
) | ||
mock_client.send_mail_v3.assert_awaited_once_with( | ||
body={ | ||
|
@@ -69,7 +71,7 @@ async def test_send_multiple_emails(mock_AsyncClient: AsyncMock) -> None: | |
"dynamic_template_data": SAMPLE_RECIPIENTS[0], | ||
}, | ||
], | ||
"template_id": "my-template-id", | ||
"template_id": Template.CONFIRMATION_EMAIL, | ||
} | ||
) | ||
|
||
|
@@ -88,5 +90,5 @@ async def test_sendgrid_error_causes_runtime_error(mock_AsyncClient: AsyncMock) | |
|
||
with pytest.raises(RuntimeError): | ||
await sendgrid_handler.send_email( | ||
"my-template-id", SAMPLE_SENDER, SAMPLE_RECIPIENTS, True | ||
Template.CONFIRMATION_EMAIL, SAMPLE_SENDER, SAMPLE_RECIPIENTS, True | ||
) |