Files
saladeaula.digital/layercake/tests/test_email.py
2025-07-29 14:09:52 -03:00

49 lines
1.4 KiB
Python

import os
from pathlib import Path
import boto3
import pytest
from layercake.dateutils import now
from layercake.email_ import Message
from moto import mock_aws
@pytest.fixture(scope='function')
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
os.environ['AWS_SECURITY_TOKEN'] = 'testing'
os.environ['AWS_SESSION_TOKEN'] = 'testing'
os.environ['AWS_DEFAULT_REGION'] = 'sa-east-1'
def test_send_email(aws_credentials):
from_ = 'pytest@eduseg.com.br'
with mock_aws():
ses = boto3.client('ses', region_name='sa-east-1')
ses.verify_email_identity(EmailAddress=from_)
to = ('Sérgio', 'sergio@somosbeta.com.br')
msg = Message(
to=to,
from_=('Beta Educação', from_),
subject='áéíóúnõ',
reply_to=('', 'osergiosiqueira@gmail.com'),
content='<b>Lorem ipsum dolor sit amet, consectetur adipiscing elit</b>',
)
msg.add_header('X-Feedback', f'uid=123; oid=123; date={now().isoformat()}')
msg.add_alternative(
"""
<h2>áéíóúnõ</h2>
html
"""
)
msg.attach(Path('tests/samples/test.pdf'), 'relatório.pdf')
assert ses.send_raw_email(
Source=from_,
Destinations=[to[1]],
RawMessage={'Data': msg.as_bytes()},
)