# pylint: disable=redefined-outer-name import os import boto3 import pytest from boto3_type_annotations.s3 import Client as S3Client from boto3_type_annotations.ses import Client as SESClient from db_schema.postgres import connection from db_schema.schemas import slz from moto import mock_s3, mock_ses from slz_gdpr_email_sender.entities import Config from .stubs import content_status_stub, email_stub, unit_of_work_stub @pytest.fixture def test_config(): return Config( sender='test_sender@gmail.com', sentry_secret_key='test/key', rds_secret_key='test/key' ) @pytest.fixture(scope='function', autouse=True) def aws_credentials(): 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'] = 'us-east-1' boto3.setup_default_session( aws_access_key_id='testing', aws_secret_access_key='testing', aws_session_token='testing', region_name='testing', ) @pytest.fixture def s3_client(): with mock_s3(): s3_client_: S3Client = boto3.client('s3') for bucket in ['bucket-decompressed']: s3_client_.create_bucket(Bucket=bucket) yield s3_client_ s3_resource = boto3.resource('s3') for bucket in ['bucket-decompressed']: bucket = s3_resource.Bucket(bucket) bucket.objects.all().delete() bucket.delete() @pytest.fixture def ses_client(test_config): with mock_ses(): ses_client_: SESClient = boto3.client('ses') ses_client_.verify_email_address(EmailAddress=test_config.sender) yield ses_client_ @pytest.fixture(scope='session') def db(): pg = connection.get_session( host=os.environ.get('PG_HOST', '0.0.0.0'), port=os.environ.get('PG_PORT', 5432), db=os.environ.get('PG_DB', 'slz_test'), user=os.environ.get('PG_USER', 'admin'), password=os.environ.get('PG_PASSWORD', 'admin'), engine_params={'echo': True} ) yield pg @pytest.fixture(scope='function') def clean_db(db): yield for model in [slz.ContentFailureLog, slz.Email, slz.ContentStatus, slz.UnitOfWork]: db.query(model).delete() db.commit() db.close() @pytest.fixture(scope='function') def email_mock(db): uow = unit_of_work_stub(db) content_status = content_status_stub(uow) email = email_stub(content_status) db.add_all([uow, content_status, email]) db.commit() yield email