|
| 1 | +import json |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from email_api.email import send_email |
| 7 | +from email_api.email import UnauthorizedException |
| 8 | +from email_api.email import BadRequestException |
| 9 | +from settings import TEST_DATA_DIR |
| 10 | + |
| 11 | + |
| 12 | +def read_json(filename): |
| 13 | + with open(TEST_DATA_DIR.joinpath(filename)) as resp_file: |
| 14 | + return json.load(resp_file) |
| 15 | + |
| 16 | + |
| 17 | +@mock.patch("email_api.email.SendGridAPIClient.send") |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_send_email(mock_email_client): |
| 20 | + mock_email_client.return_value = {"status_code": 202} |
| 21 | + response = await send_email(data=read_json("email_data.json")) |
| 22 | + assert response["status_code"] == 202 |
| 23 | + |
| 24 | + |
| 25 | +@mock.patch("email_api.email.SendGridAPIClient.send") |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_send_email_raises_unauthorized_exception(mock_email_client): |
| 28 | + mock_email_client.side_effect = UnauthorizedException("Invalid api key") |
| 29 | + with pytest.raises(UnauthorizedException) as exc: |
| 30 | + await send_email(data=read_json("email_data.json")) |
| 31 | + assert isinstance(exc.value, UnauthorizedException) |
| 32 | + assert exc.value.args[0] == "Invalid api key" |
| 33 | + |
| 34 | + |
| 35 | +@mock.patch("email_api.email.SendGridAPIClient.send") |
| 36 | +@pytest.mark.asyncio |
| 37 | +async def test_send_email_raises_bad_request_exception(mock_email_client): |
| 38 | + mock_email_client.side_effect = BadRequestException("Bad request") |
| 39 | + with pytest.raises(BadRequestException) as exc: |
| 40 | + await send_email(data=read_json("email_data.json")) |
| 41 | + assert isinstance(exc.value, BadRequestException) |
| 42 | + assert exc.value.args[0] == "Bad request" |
0 commit comments