Skip to content

Commit

Permalink
Add exceptions tests (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
leogregianin committed Dec 13, 2021
1 parent db2edb7 commit bb73a7d
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest
from googleapiclient.errors import HttpError

from gmail_wrapper import GmailClient
from gmail_wrapper.exceptions import (
MessageNotFoundError,
AttachmentNotFoundError,
GmailError,
)
from tests.utils import make_gmail_client


class TestGmailError:

def test_exceptions_gmail_error(self, mocker):
server_error_response = mocker.MagicMock(status=500)
mocker.patch(
"gmail_wrapper.client.GmailClient._make_client",
return_value=make_gmail_client(
mocker, list_effect=HttpError(server_error_response, b"Content")
),
)

client = GmailClient(email="foo@bar.com", secrets_json_string="{}")
with pytest.raises(GmailError) as error:
raise client.get_raw_messages()
assert str(error.value) == "GmailError: Gmail returned an internal server error"


class TestMessageNotFoundError:
def test_exceptions_message_not_found(self, mocker):
error_response = mocker.MagicMock(status=404)
mocker.patch(
"gmail_wrapper.client.GmailClient._make_client",
return_value=make_gmail_client(
mocker, get_effect=HttpError(error_response, b"Content")
),
)

client = GmailClient(email="foo@bar.com", secrets_json_string="{}")
message_id = "123AAB"
with pytest.raises(MessageNotFoundError) as error:
raise client.get_raw_message(message_id)
assert str(error.value) == f"MessageNotFoundError: Gmail returned 404 when attempting to get message {message_id}"


class TestAttachmentNotFoundError:
def test_exceptions_attachment_not_found(self, mocker):
error_response = mocker.MagicMock(status=404)
mocker.patch(
"gmail_wrapper.client.GmailClient._make_client",
return_value=make_gmail_client(
mocker, attachment_effect=HttpError(error_response, b"Content")
),
)

client = GmailClient(email="foo@bar.com", secrets_json_string="{}")
message_id = "123AAB"
attachment_id = "CCX457"
with pytest.raises(AttachmentNotFoundError) as error:
raise client.get_attachment_body(id=attachment_id, message_id=message_id)
assert str(error.value) == f"AttachmentNotFoundError: Gmail returned 404 when attempting to get attachment {attachment_id} of message {message_id}"

0 comments on commit bb73a7d

Please sign in to comment.