import os import boto3 import pytest import redis from apollo_main_db.base import Base from moto import mock_secretsmanager from send_push_notifications import config from send_push_notifications.main_db import engine, session_scope from tests import factories ENV_VARS = ['MYSQL_DB_NAME', 'MYSQL_DB_HOST', 'MYSQL_DB_USER', 'MYSQL_DB_PASS', 'REDIS_HOST', 'SENTRY_DSN'] @pytest.fixture def aws_credentials(): """Mocked AWS Credentials for boto3.""" 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' @pytest.fixture def mocked_secrets_manager(aws_credentials): """Mocked AWS Secret Manager.""" with mock_secretsmanager(): secrets_manager = boto3.client( 'secretsmanager', region_name='us-east-1') for env in ENV_VARS: value = os.environ[env] secrets_manager.create_secret(Name=value, SecretString=value) yield secrets_manager def register_factories(session): """Pass session to factory model""" for factory in ( factories.UserDeviceFactory, ): factory._meta.sqlalchemy_session = session @pytest.yield_fixture def db_session(): """Returns an sqlalchemy session, and after the test tears down.""" Base.metadata.create_all(engine) with session_scope() as session: register_factories(session) yield session Base.metadata.drop_all(engine) @pytest.fixture def redis_client(): """Redis client""" _config = config.Config return redis.StrictRedis(_config.REDIS_HOST, _config.REDIS_PORT) @pytest.fixture(autouse=True) def redis_flush(redis_client): """Flush all before each test""" redis_client.flushall()