def test_that_as_dict_works(email_with_attachments: bytes):
    message = ParsedEmail.from_bytes(email_with_attachments)
    attachments = list(message.get_attachments())

    expected = {
        'from': '*****@*****.**',
        'to': ['*****@*****.**'],
        'subject': 'An email with attachments',
        'body': 'This is the message body\n',
        'attachments': [
            {
                'filename': 'a-text-file.txt',
                'content-type': 'text/plain',
                'body': b64encode(attachments[0].body).decode(),
                'base64': True,
            },
            {
                'filename': 'a-pdf-file.pdf',
                'content-type': 'application/pdf',
                'body': b64encode(attachments[1].body).decode(),
                'base64': True,
            },
            {
                'filename': 'an-excel-file.xlsx',
                'content-type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'body': b64encode(attachments[2].body).decode(),
                'base64': True,
            }
        ]
    }

    assert message.as_dict() == expected
def test_that_email_message_can_read_an_email_without_attachments(base_email: bytes):
    message = ParsedEmail.from_bytes(base_email)

    assert message.from_addr == '*****@*****.**'
    assert message.to_addr == ('*****@*****.**',)
    assert message.subject == 'Example Email'
    assert message.body == 'This is an example email\n*This is an example email*\nThis is an example email\n'
예제 #3
0
def test_that_lambda_handler_wires_everything_correctly(
        base_email: bytes,
        mock_sns_client: botocore.client.BaseClient,
        set_environment_variable: Callable[[str, str], None]):
    sns_arn = 'I am an ARM'
    bucket = 'bucket-bucket-bucket'
    prefix = 'tank-recipient'
    message_id = 'id1234'

    set_environment_variable('TRANSFORMER_ARN', sns_arn)
    set_environment_variable('EMAIL_BUCKET_NAME', bucket)
    create_s3_object(bucket, f'{prefix}/{message_id}', base_email)

    event = {
        'Records': [
            {
                'ses': {
                    'mail': {'messageId': message_id},
                    'receipt': {
                        'recipients': [f'{prefix}@foo.com'],
                    },
                },
            },
        ],
    }

    lambda_handler(event, None)
    mock_sns_client.publish.assert_called_once_with(
        TargetArn=sns_arn,
        Message=json.dumps({'default': ParsedEmail.from_bytes(base_email).as_json()}),
        MessageStructure='json')
def test_that_attachments_are_extracted(email_with_attachments: bytes):
    message = ParsedEmail.from_bytes(email_with_attachments)
    attachments = list(message.get_attachments())

    assert message.has_attachments
    assert len(attachments) == 3
    assert attachments[0].filename == 'a-text-file.txt'
    assert attachments[1].filename == 'a-pdf-file.pdf'
    assert attachments[2].filename == 'an-excel-file.xlsx'
def lambda_handler(event: dict, _context):
    _configure_logging()

    transformer_arn = os.environ['TRANSFORMER_ARN']
    bucket_name = os.environ['EMAIL_BUCKET_NAME']

    message_id = event['Records'][0]['ses']['mail']['messageId']
    # key prefix is the first part of the email address (i.e. 'jdoe' from '*****@*****.**')
    key_prefix = event['Records'][0]['ses']['receipt']['recipients'][0].split('@')[0]

    raw_email = get_raw_email(message_id, bucket_name, key_prefix)
    message = ParsedEmail.from_bytes(raw_email)
    publish_to_sns(message.as_json(), transformer_arn)
def test_that_attachments_get_decoded(email_with_attachments: bytes):
    message = ParsedEmail.from_bytes(email_with_attachments)

    text_file = list(message.get_attachments())[0]
    pdf_file = list(message.get_attachments())[1]
    excel_file = list(message.get_attachments())[2]

    assert text_file.filename == 'a-text-file.txt'
    assert text_file.body == b'This is a text file\n'

    assert pdf_file.filename == 'a-pdf-file.pdf'
    assert pdf_file.body.startswith(b'%PDF-1.4\n%\xc7\xec\x8f\xa2\n5')

    assert excel_file.filename == 'an-excel-file.xlsx'
    assert excel_file.body.startswith(b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xc5\xa5RN')
def test_to_addr_contains_all_email_recipients(two_recipient_email: bytes):
    message = ParsedEmail.from_bytes(two_recipient_email)

    assert message.to_addr == ('*****@*****.**', '*****@*****.**')
def test_that_html_emails_are_converted_to_plain_text(html_email: bytes):
    message = ParsedEmail.from_bytes(html_email)

    assert message.body == 'This is an example email\n\n **This is an example email**\n\nThis is an example email'