예제 #1
0
    def test_template_error_debugging_for_single_message(self, mocked_create, app):
        # Given
        data = {
            "FromEmail": "*****@*****.**",
            "To": "*****@*****.**",
            "Mj-TemplateID": 0,
            "Mj-TemplateLanguage": True,
            "Vars": {
                "department": "93",
            },
        }

        # When
        send_raw_email(data)

        # Then
        transformed_data = {
            "FromEmail": "*****@*****.**",
            "To": "*****@*****.**",
            "Mj-TemplateID": 0,
            "Mj-TemplateLanguage": True,
            "Vars": {
                "department": "93",
            },
            "TemplateErrorReporting": {
                "Email": "*****@*****.**",
                "Name": "Mailjet Template Errors",
            },
        }

        mocked_create.assert_called_once_with(data=transformed_data)
예제 #2
0
def test_save_and_send_creates_an_entry_in_email_with_status_error_when_send_mail_unsuccessful(app):
    # given
    email_content = {
        "FromEmail": "*****@*****.**",
        "FromName": "Test From",
        "Subject": "Test subject",
        "Text-Part": "Hello world",
        "Html-part": "<html><body>Hello World</body></html>",
    }
    mocked_response = MagicMock()
    mocked_response.status_code = 500
    app.mailjet_client.send.create.return_value = mocked_response

    # when
    successfully_sent_email = send_raw_email(email_content)

    # then
    assert not successfully_sent_email
    assert app.mailjet_client.send.create.called_once_with(email_content)
    emails = Email.query.all()
    assert len(emails) == 1
    email = emails[0]
    assert email.content == email_content
    assert email.status == EmailStatus.ERROR
    assert email.datetime == datetime(2019, 1, 1, 12, 0, 0)
예제 #3
0
    def test_should_catch_errors_when_mailjet_api_is_not_reachable(self, app):
        # Given
        data = {"data": {}}
        app.mailjet_client.send.create = MagicMock()
        app.mailjet_client.send.create.side_effect = Timeout

        # When
        result = send_raw_email(data)

        # Then
        app.mailjet_client.send.create.assert_called_once_with(data=data)
        assert result is False
예제 #4
0
    def test_should_return_false_when_mailjet_status_code_is_not_200(self, app):
        # Given
        data = {"data": {}}
        app.mailjet_client.send.create = MagicMock()
        app.mailjet_client.send.create.return_value = MagicMock(status_code=400)

        # When
        result = send_raw_email(data)

        # Then
        app.mailjet_client.send.create.assert_called_once_with(data=data)
        assert result is False
예제 #5
0
    def test_should_call_mailjet_api_to_send_emails(self, app):
        # Given
        data = {"data": {}}
        app.mailjet_client.send.create = MagicMock()
        app.mailjet_client.send.create.return_value = MagicMock(status_code=200)

        # When
        result = send_raw_email(data)

        # Then
        app.mailjet_client.send.create.assert_called_once_with(data=data)
        assert result is True
예제 #6
0
def send_user_emails_for_email_change(user: User, new_email: str) -> None:
    user_with_new_email = User.query.filter_by(email=new_email).first()
    if user_with_new_email:
        return

    information_data = build_beneficiary_information_email_change_data(
        user.email, user.firstName)
    information_sucessfully_sent = mailing_utils.send_raw_email(
        information_data)
    if not information_sucessfully_sent:
        raise MailServiceException()

    link_for_email_change = _build_link_for_email_change(user.email, new_email)
    confirmation_data = build_beneficiary_confirmation_email_change_data(
        user.firstName, link_for_email_change, new_email)
    confirmation_sucessfully_sent = mailing_utils.send_raw_email(
        confirmation_data)
    if not confirmation_sucessfully_sent:
        raise MailServiceException()

    return