Skip to content

Commit 905684a

Browse files
surajitsurajit
authored andcommitted
add tests for email
1 parent 61eb0fb commit 905684a

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

settings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2-
2+
from pathlib import Path
33
from decouple import config
44

5+
BASE_DIR = Path(__file__).resolve(strict=True).parent
6+
57
DATABASE_HOST = config("DATABASE_HOST")
68
DATABASE = config("DATABASE_NAME")
79
DATABASE_USER = config("DATABASE_USER")
@@ -17,3 +19,5 @@
1719
SENDGRID_API_KEY = config("SENDGRID_API_KEY")
1820
WELCOME_MESSAGE_TEMPLATE_ID = config("WELCOME_MESSAGE_TEMPLATE_ID")
1921
PAYMENT_CONFIRMATION_TEMPLATE_ID = config("WELCOME_MESSAGE_TEMPLATE_ID")
22+
23+
TEST_DATA_DIR = BASE_DIR.joinpath('tests/data')

tests/data/__init_-.py

Whitespace-only changes.

tests/data/email_data.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"recipients": [
3+
{
4+
"recipient_email": "[email protected]",
5+
"recipient_name": "DC"
6+
},
7+
{
8+
"recipient_email": "[email protected]",
9+
"recipient_name": "Flash"
10+
}
11+
],
12+
"template_content": {
13+
"content": "Hey there DC please continue with the Flash seasons"
14+
},
15+
"sender": "[email protected]",
16+
"subject": "Flash Fan",
17+
"email_type": "welcome_email"
18+
}

tests/routes/test_email.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import json
2+
from unittest import mock
3+
4+
from models.message import Message
5+
from settings import TEST_DATA_DIR
6+
7+
8+
def read_json(filename):
9+
with open(TEST_DATA_DIR.joinpath(filename)) as resp_file:
10+
return json.load(resp_file)
11+
12+
13+
class SendGridResponseMock:
14+
def __init__(self, headers, status_code):
15+
self.headers = headers
16+
self.status_code = status_code
17+
18+
19+
@mock.patch("routes.v1.email.email.send_email")
20+
def test_email_send(mock_send_email, client, db):
21+
sendgrid_response_mock = SendGridResponseMock(
22+
headers={"x-message-id": "xvis10203sn"}, status_code=202
23+
)
24+
mock_send_email.return_value = sendgrid_response_mock
25+
response = client.post("/api/v1/email/send/", json=read_json("email_data.json"))
26+
message = db.query(Message).filter_by(message_id="xvis10203sn").all()[0]
27+
assert response.status_code == 200
28+
assert message.message_id == "xvis10203sn"
29+
assert message.message_type.value == "EMAIL"
30+
assert message.status_code == "202"
31+
assert message.carrier.value == "SENDGRID"

0 commit comments

Comments
 (0)