def test_from_dict_missing_role_session_name(self): mha_dict = { "access_steps": [ {"role_name": "test_role_name1", "external_id": "abcd", "account_id": "1234"}, {"role_name": "test_role_name2", "external_id": None, "account_id": "5678"}, {"role_name": "test_role_name3", "external_id": None, "account_id": None}, ] } with self.assertRaises(ValueError): MultiHopAccessor.from_dict(mha_dict)
def test_with_access_steps_final_with_account_id(self): access_steps = [ AccessStep(role_name="test_role_name1", account_id="1234"), AccessStep(role_name="test_role_name2", account_id="5678"), ] with self.assertRaises(ValueError): MultiHopAccessor(role_session_name="test_role_session_name", access_steps=access_steps)
def test_get_session(self): access_steps = [ AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"), AccessStep(role_name="test_role_name2", account_id="5678"), AccessStep(role_name="test_role_name3"), ] mha = MultiHopAccessor( role_session_name="test_role_session_name", access_steps=access_steps ) session = mha.get_session("4566") self.assertIsInstance(session, boto3.Session) expected_cache_sorted_keys = [ "1234:test_role_name1:test_role_session_name:None", "4566:test_role_name3:test_role_session_name:None", "5678:test_role_name2:test_role_session_name:None", ] self.assertEqual(sorted(mha.session_cache._cache.keys()), expected_cache_sorted_keys)
def test_to_dict(self): access_steps = [ AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"), AccessStep(role_name="test_role_name2", account_id="5678"), AccessStep(role_name="test_role_name3"), ] mha = MultiHopAccessor( role_session_name="test_role_session_name", access_steps=access_steps ) expected_dict = { "role_session_name": "test_role_session_name", "access_steps": [ {"role_name": "test_role_name1", "external_id": "abcd", "account_id": "1234"}, {"role_name": "test_role_name2", "external_id": None, "account_id": "5678"}, {"role_name": "test_role_name3", "external_id": None, "account_id": None}, ], } self.assertEqual(mha.to_dict(), expected_dict)
def test_from_dict(self): mha_dict = { "role_session_name": "test_role_session_name", "access_steps": [ {"role_name": "test_role_name1", "external_id": "abcd", "account_id": "1234"}, {"role_name": "test_role_name2", "external_id": None, "account_id": "5678"}, {"role_name": "test_role_name3", "external_id": None, "account_id": None}, ], } mha = MultiHopAccessor.from_dict(mha_dict) self.assertEqual(mha_dict, mha.to_dict())
def test_str(self): access_steps = [ AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"), AccessStep(role_name="test_role_name2", account_id="5678"), AccessStep(role_name="test_role_name3"), ] mha = MultiHopAccessor( role_session_name="test_role_session_name", access_steps=access_steps ) expected_str = "accessor:test_role_session_name:test_role_name1@1234,test_role_name2@5678,test_role_name3@target" self.assertEqual(str(mha), expected_str)
def test_cache_usage(self): access_steps = [ AccessStep(role_name="test_role_name1", account_id="1234", external_id="abcd"), AccessStep(role_name="test_role_name2", account_id="5678"), AccessStep(role_name="test_role_name3"), ] mha = MultiHopAccessor( role_session_name="test_role_session_name", access_steps=access_steps ) mha.get_session("4567") mha.get_session("4567") mha.get_session("8901") mha.get_session("8901") expected_cache_sorted_keys = [ "1234:test_role_name1:test_role_session_name:None", "4567:test_role_name3:test_role_session_name:None", "5678:test_role_name2:test_role_session_name:None", "8901:test_role_name3:test_role_session_name:None", ] self.assertEqual(sorted(mha.session_cache._cache.keys()), expected_cache_sorted_keys)
def test_from_dict_missing_access_steps(self): mha_dict = {"role_session_name": "test_role_session_name"} with self.assertRaises(ValueError): MultiHopAccessor.from_dict(mha_dict)
def test_without_access_steps(self): with self.assertRaises(ValueError): MultiHopAccessor(role_session_name="test_role_session_name", access_steps=[])