示例#1
0
 def test_it_doesnt_break_when_no_results(self, mocker):
     raw_response = {"resultSizeEstimate": 0}
     mocker.patch(
         "gmail_wrapper.client.GmailClient._make_client",
         return_value=make_gmail_client(mocker, list_return=raw_response),
     )
     client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
     messages = client.get_messages()
     assert messages == []
示例#2
0
 def test_it_returns_a_raw_message(self, mocker, raw_complete_message):
     mocker.patch(
         "gmail_wrapper.client.GmailClient._make_client",
         return_value=make_gmail_client(mocker,
                                        get_return=raw_complete_message),
     )
     client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
     message = client.get_raw_message("123AAB")
     assert message == raw_complete_message
示例#3
0
 def test_it_returns_raw_messages(self, mocker, raw_incomplete_message):
     raw_response = [{
         "messages": [raw_incomplete_message, raw_incomplete_message]
     }]
     mocker.patch(
         "gmail_wrapper.client.GmailClient._make_client",
         return_value=make_gmail_client(mocker, list_return=raw_response),
     )
     client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
     assert client.get_raw_messages() == raw_response
示例#4
0
 def test_it_returns_a_raw_attachment_body(self, mocker,
                                           raw_attachment_body):
     mocker.patch(
         "gmail_wrapper.client.GmailClient._make_client",
         return_value=make_gmail_client(
             mocker, attachment_return=raw_attachment_body),
     )
     client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
     attachment_body = client.get_raw_attachment_body(id="CCX457",
                                                      message_id="123AAB")
     assert attachment_body == raw_attachment_body
    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="*****@*****.**", 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"
示例#6
0
    def test_it_encapsulates_gmail_exceptions(self, mocker, error_code,
                                              exception_expected):
        error_response = mocker.MagicMock(status=error_code)
        mocker.patch(
            "gmail_wrapper.client.GmailClient._make_client",
            return_value=make_gmail_client(mocker,
                                           get_effect=HttpError(
                                               error_response, b"Content")),
        )

        client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
        with pytest.raises(exception_expected):
            client.get_raw_message("123AAB")
    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="*****@*****.**", 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}"
示例#8
0
    def test_it_encapsulates_gmail_exceptions(self, mocker, error_code,
                                              exception_expected):
        error_response = mocker.MagicMock(status=error_code)
        mocker.patch(
            "gmail_wrapper.client.GmailClient._make_client",
            return_value=make_gmail_client(mocker,
                                           modify_effect=HttpError(
                                               error_response, b"Content")),
        )

        client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
        with pytest.raises(exception_expected):
            client.modify_raw_message(id="CCX457",
                                      add_labels=["processed"],
                                      remove_labels=["phishing"])
示例#9
0
 def test_it_modifies_and_return_a_raw_message(self, mocker,
                                               raw_complete_message):
     mocker.patch(
         "gmail_wrapper.client.GmailClient._make_client",
         return_value=make_gmail_client(mocker,
                                        modify_return=raw_complete_message),
     )
     client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
     modified_message = client.modify_raw_message(
         id="CCX457", add_labels=["processed"], remove_labels=["phishing"])
     assert modified_message == raw_complete_message
     client._messages_resource().modify.assert_called_once_with(
         userId="*****@*****.**",
         id="CCX457",
         body={
             "addLabelIds": ["processed"],
             "removeLabelIds": ["phishing"]
         },
     )
示例#10
0
 def test_it_send_and_return_a_raw_message(self, mocker,
                                           raw_complete_message):
     mocker.patch(
         "gmail_wrapper.client.GmailClient._make_client",
         return_value=make_gmail_client(mocker,
                                        send_return=raw_complete_message),
     )
     client = GmailClient(email="*****@*****.**", secrets_json_string="{}")
     sent_message = client.send_raw("Hi there!", "<html><p>Hey</p></html>",
                                    "*****@*****.**")
     assert sent_message == raw_complete_message
     client._messages_resource().send.assert_called_once_with(
         userId="*****@*****.**",
         body={
             "raw":
             base64.urlsafe_b64encode(
                 b'Content-Type: text/html; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nsubject: Hi there!\nfrom: [email protected]\nto: [email protected]\ncc: \nbcc: \nreferences: \nin-reply-to: \n\n<html><p>Hey</p></html>'
             ).decode("utf-8"),
             "threadId":
             None,
         },
     )
示例#11
0
def client(mocker):
    mocker.patch(
        "gmail_wrapper.client.GmailClient._make_client",
        return_value=make_gmail_client(mocker),
    )
    return GmailClient(email="*****@*****.**", secrets_json_string="{}")