I am trying to figure out how to pass a mock boto3 client to init.py with pytest, but am having some trouble. When I pass my fixture into the function I'm trying to test, the function called in init.py still tries to use a real instance of the boto client when get_secret is called instead of the one I'm mocking. Wondering what I am doing wrong here?
As an example:
init.py
from helpers.token_helper import get_token
from my_jobs.jobs import job_1, job_2
params = {'token': get_token()}
job_1()
job_2(params)
token_helper.py
import os
from aws_helper import get_secret
def get_token():
token = os.getenv("MY_TOKEN")
if not token:
try:
token = get_secret("my-secret/my-token")
except Exception as e:
raise Exception(f"could not get token")
return token
aws_helper.py
import boto3
def get_secret(secret_name):
client = boto3.client("secretsmanager", region_name="us-east-1")
try:
response = client.get_secret_value(SecretId=secret_name)
return response["SecretString"]
except Exception as e:
print(e)
raise e
jobs.py
def job_1():
return 'yay'
def job_2(params):
print(params)
return 'boo'
test_jobs.py
import pytest
import boto3
import moto
from my_jobs.jobs import job_1
@pytest.fixture(scope="session")
def mock_boto3_client():
with moto.mock_secretsmanager():
client = boto3.client("secretsmanager", region_name="us-east-1")
secret_name = "my-test-secret"
secret_value = "my-test-secret-value"
client.create_secret(Name=secret_name, SecretString=secret_value)
yield client
def test_job_1(mock_boto3_client):
response = job_1()
assert response == 'yay'