-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathtest_account_alias.py
More file actions
48 lines (41 loc) · 1.67 KB
/
test_account_alias.py
File metadata and controls
48 lines (41 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Tests the account alias configuration lambda
"""
import unittest
import boto3
from botocore.stub import Stubber
from botocore.exceptions import ClientError
from aws_xray_sdk import global_sdk_config
from ..configure_account_alias import create_account_alias
global_sdk_config.set_sdk_enabled(False)
class SuccessTestCase(unittest.TestCase):
# pylint: disable=W0106
def test_account_alias(self):
test_account = {"account_id": 123456789012, "alias": "MyCoolAlias"}
iam_client = boto3.client("iam")
stubber = Stubber(iam_client)
create_alias_response = {}
stubber.add_response(
"create_account_alias", create_alias_response, {"AccountAlias": "MyCoolAlias"}
),
stubber.activate()
response = create_account_alias(test_account, iam_client)
self.assertEqual(response, test_account)
class FailureTestCase(unittest.TestCase):
# pylint: disable=W0106
def test_account_alias_when_nonunique(self):
test_account = {"account_id": 123456789012, "alias": "nonunique"}
iam_client = boto3.client("iam")
stubber = Stubber(iam_client)
stubber.add_client_error(
'create_account_alias',
'EntityAlreadyExistsException',
f"An error occurred (EntityAlreadyExists) when calling the CreateAccountAlias operation: The account alias {test_account.get('alias')} already exists."
)
stubber.activate()
with self.assertRaises(ClientError) as _error:
create_account_alias(test_account, iam_client)
self.assertRegex(
str(_error.exception),
r'.*The account alias nonunique already exists.*'
)