Skip to content

Commit 4f787a4

Browse files
updated config to pick cred&URL by env
1 parent 68674bd commit 4f787a4

File tree

6 files changed

+123
-9
lines changed

6 files changed

+123
-9
lines changed

src/eligibility_signposting_api/config.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,34 @@
1919

2020
@cache
2121
def config() -> dict[str, Any]:
22+
env = os.getenv("ENV")
23+
24+
eligibility_table_name = TableName(os.getenv("ELIGIBILITY_TABLE_NAME", "test_eligibility_datastore"))
25+
rules_bucket_name = BucketName(os.getenv("RULES_BUCKET_NAME", "test-rules-bucket"))
26+
aws_default_region = AwsRegion(os.getenv("AWS_DEFAULT_REGION", "eu-west-1"))
27+
log_level = LOG_LEVEL
28+
29+
if env:
30+
return {
31+
"aws_access_key_id": None,
32+
"aws_default_region": aws_default_region,
33+
"aws_secret_access_key": None,
34+
"dynamodb_endpoint": None,
35+
"eligibility_table_name": eligibility_table_name,
36+
"s3_endpoint": None,
37+
"rules_bucket_name": rules_bucket_name,
38+
"log_level": log_level,
39+
}
40+
2241
return {
2342
"aws_access_key_id": AwsAccessKey(os.getenv("AWS_ACCESS_KEY_ID", "dummy_key")),
24-
"aws_default_region": AwsRegion(os.getenv("AWS_DEFAULT_REGION", "eu-west-1")),
43+
"aws_default_region": aws_default_region,
2544
"aws_secret_access_key": AwsSecretAccessKey(os.getenv("AWS_SECRET_ACCESS_KEY", "dummy_secret")),
2645
"dynamodb_endpoint": URL(os.getenv("DYNAMODB_ENDPOINT", "http://localhost:4566")),
27-
"eligibility_table_name": TableName(os.getenv("ELIGIBILITY_TABLE_NAME", "test_eligibility_datastore")),
28-
"log_level": LOG_LEVEL,
29-
"rules_bucket_name": BucketName(os.getenv("RULES_BUCKET_NAME", "test-rules-bucket")),
46+
"eligibility_table_name": eligibility_table_name,
47+
"s3_endpoint": URL(os.getenv("S3_ENDPOINT", "http://localhost:4566")),
48+
"rules_bucket_name": rules_bucket_name,
49+
"log_level": log_level,
3050
}
3151

3252

src/eligibility_signposting_api/repos/factory.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def boto3_session_factory(
2727
def dynamodb_resource_factory(
2828
session: Session, dynamodb_endpoint: Annotated[URL, Inject(param="dynamodb_endpoint")]
2929
) -> ServiceResource:
30-
return session.resource("dynamodb", endpoint_url=str(dynamodb_endpoint))
30+
endpoint_url = str(dynamodb_endpoint) if dynamodb_endpoint is not None else None
31+
return session.resource("dynamodb", endpoint_url=endpoint_url)
3132

3233

3334
@service(qualifier="s3")
34-
def s3_service_factory(
35-
session: Session, dynamodb_endpoint: Annotated[URL, Inject(param="dynamodb_endpoint")]
36-
) -> BaseClient:
37-
return session.client("s3", endpoint_url=str(dynamodb_endpoint))
35+
def s3_service_factory(session: Session, s3_endpoint: Annotated[URL, Inject(param="s3_endpoint")]) -> BaseClient:
36+
endpoint_url = str(s3_endpoint) if s3_endpoint is not None else None
37+
return session.client("s3", endpoint_url=endpoint_url)

tests/integration/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def flask_function(lambda_client: BaseClient, iam_role: str, lambda_zip: Path) -
164164
Environment={
165165
"Variables": {
166166
"DYNAMODB_ENDPOINT": os.getenv("LOCALSTACK_INTERNAL_ENDPOINT", "http://localstack:4566/"),
167+
"S3_ENDPOINT": os.getenv("LOCALSTACK_INTERNAL_ENDPOINT", "http://localstack:4566/"),
167168
"AWS_REGION": AWS_REGION,
168169
"LOG_LEVEL": "DEBUG",
169170
}

tests/unit/repos/__init__.py

Whitespace-only changes.

tests/unit/repos/test_factory.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from unittest.mock import Mock
2+
3+
import pytest
4+
from yarl import URL
5+
6+
7+
def service(qualifier=None): # noqa: ARG001
8+
def decorator(fn):
9+
return fn
10+
11+
return decorator
12+
13+
14+
class Inject:
15+
def __init__(self, param):
16+
self.param = param
17+
18+
19+
@service(qualifier="dynamodb")
20+
def dynamodb_resource_factory(session, dynamodb_endpoint: URL):
21+
endpoint_url = str(dynamodb_endpoint) if dynamodb_endpoint is not None else None
22+
return session.resource("dynamodb", endpoint_url=endpoint_url)
23+
24+
25+
@service(qualifier="s3")
26+
def s3_service_factory(session, s3_endpoint: URL):
27+
endpoint_url = str(s3_endpoint) if s3_endpoint is not None else None
28+
return session.client("s3", endpoint_url=endpoint_url)
29+
30+
31+
# --- Pytest Unit Tests ---
32+
33+
34+
@pytest.fixture
35+
def mock_session():
36+
return Mock()
37+
38+
39+
def test_dynamodb_resource_factory_with_endpoint(mock_session):
40+
mock_resource = Mock()
41+
mock_session.resource.return_value = mock_resource
42+
endpoint = URL("http://localhost:4566")
43+
44+
result = dynamodb_resource_factory(mock_session, endpoint)
45+
46+
mock_session.resource.assert_called_once_with("dynamodb", endpoint_url="http://localhost:4566")
47+
assert result is mock_resource
48+
49+
50+
def test_dynamodb_resource_factory_without_endpoint(mock_session):
51+
mock_resource = Mock()
52+
mock_session.resource.return_value = mock_resource
53+
54+
result = dynamodb_resource_factory(mock_session, None)
55+
56+
mock_session.resource.assert_called_once_with("dynamodb", endpoint_url=None)
57+
assert result is mock_resource
58+
59+
60+
def test_s3_service_factory_with_endpoint(mock_session):
61+
mock_client = Mock()
62+
mock_session.client.return_value = mock_client
63+
endpoint = URL("http://localhost:4566")
64+
65+
result = s3_service_factory(mock_session, endpoint)
66+
67+
mock_session.client.assert_called_once_with("s3", endpoint_url="http://localhost:4566")
68+
assert result is mock_client
69+
70+
71+
def test_s3_service_factory_without_endpoint(mock_session):
72+
mock_client = Mock()
73+
mock_session.client.return_value = mock_client
74+
75+
result = s3_service_factory(mock_session, None)
76+
77+
mock_session.client.assert_called_once_with("s3", endpoint_url=None)
78+
assert result is mock_client

tests/unit/test_config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from eligibility_signposting_api.config import AwsRegion, config
2+
from eligibility_signposting_api.repos.eligibility_repo import TableName
3+
from eligibility_signposting_api.repos.rules_repo import BucketName
4+
5+
6+
def test_config_with_env_variable(monkeypatch):
7+
monkeypatch.setenv("ENV", "PROD")
8+
9+
config_data = config()
10+
11+
assert config_data["aws_access_key_id"] is None
12+
assert config_data["aws_secret_access_key"] is None
13+
assert config_data["aws_default_region"] == AwsRegion("eu-west-1")
14+
assert config_data["eligibility_table_name"] == TableName("test_eligibility_datastore")
15+
assert config_data["rules_bucket_name"] == BucketName("test-rules-bucket")

0 commit comments

Comments
 (0)