Skip to content

Commit 6cec8bb

Browse files
sodreclaude
andcommitted
✅ test(repository): cover STS session creation on aiobotocore path
The migration changed the _get_caller_identity_arn session-creation line (self._session = get_session()), but the existing test pre-set _session, leaving that branch uncovered and failing the 100% patch-coverage gate. Add a success-path test that forces _session=None so the branch runs. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent f6b6f08 commit 6cec8bb

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

tests/unit/test_repository.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,37 @@ async def test_get_caller_identity_handles_sts_failure(self, repo):
16121612
assert repo._caller_identity_fetched is True
16131613
assert repo._caller_identity_arn is None
16141614

1615+
@pytest.mark.asyncio
1616+
async def test_get_caller_identity_creates_session_when_missing(self, repo):
1617+
"""When no session exists yet, the STS path creates one via get_session()."""
1618+
from unittest.mock import AsyncMock, MagicMock, patch
1619+
1620+
# Force the session-creation branch (no cached session/identity)
1621+
repo._session = None
1622+
repo._caller_identity_fetched = False
1623+
repo._caller_identity_arn = None
1624+
1625+
mock_sts_client = AsyncMock()
1626+
mock_sts_client.get_caller_identity.return_value = {
1627+
"Arn": "arn:aws:iam::123456789012:user/test"
1628+
}
1629+
mock_sts_client.__aenter__ = AsyncMock(return_value=mock_sts_client)
1630+
mock_sts_client.__aexit__ = AsyncMock(return_value=None)
1631+
1632+
mock_session = MagicMock()
1633+
mock_session.create_client.return_value = mock_sts_client
1634+
1635+
with patch(
1636+
"zae_limiter.repository.get_session", return_value=mock_session
1637+
) as mock_get_session:
1638+
arn = await repo._get_caller_identity_arn()
1639+
1640+
# Session was created and the ARN resolved
1641+
assert arn == "arn:aws:iam::123456789012:user/test"
1642+
mock_get_session.assert_called_once()
1643+
assert mock_session.create_client.call_args.args[0] == "sts"
1644+
assert repo._caller_identity_fetched is True
1645+
16151646
@pytest.mark.asyncio
16161647
async def test_get_audit_retention_days_from_stack_options(self, repo):
16171648
"""Should return audit_retention_days from stack_options if available."""

tests/unit/test_sync_repository.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,30 @@ def test_get_caller_identity_handles_sts_failure(self, repo):
11711171
assert repo._caller_identity_fetched is True
11721172
assert repo._caller_identity_arn is None
11731173

1174+
def test_get_caller_identity_creates_session_when_missing(self, repo):
1175+
"""When no session exists yet, the STS path creates one via boto3.Session()."""
1176+
from unittest.mock import MagicMock, patch
1177+
1178+
repo._session = None
1179+
repo._caller_identity_fetched = False
1180+
repo._caller_identity_arn = None
1181+
mock_sts_client = MagicMock()
1182+
mock_sts_client.get_caller_identity.return_value = {
1183+
"Arn": "arn:aws:iam::123456789012:user/test"
1184+
}
1185+
mock_sts_client.__enter__ = MagicMock(return_value=mock_sts_client)
1186+
mock_sts_client.__exit__ = MagicMock(return_value=None)
1187+
mock_session = MagicMock()
1188+
mock_session.client.return_value = mock_sts_client
1189+
with patch(
1190+
"zae_limiter.sync_repository.boto3.Session", return_value=mock_session
1191+
) as mock_get_session:
1192+
arn = repo._get_caller_identity_arn()
1193+
assert arn == "arn:aws:iam::123456789012:user/test"
1194+
mock_get_session.assert_called_once()
1195+
assert mock_session.client.call_args.args[0] == "sts"
1196+
assert repo._caller_identity_fetched is True
1197+
11741198
def test_get_audit_retention_days_from_stack_options(self, repo):
11751199
"""Should return audit_retention_days from stack_options if available."""
11761200
from zae_limiter.models import StackOptions

0 commit comments

Comments
 (0)