From a79df7bc87f676957e13eb76d32616d18ee3eddd Mon Sep 17 00:00:00 2001 From: Iain Samuel McLean Elder Date: Sat, 30 Apr 2022 11:03:19 +0100 Subject: [PATCH] fix(org): close_account matches public API (#5072) --- moto/organizations/models.py | 30 +++++----------- .../organizations_test_utils.py | 14 -------- .../test_organizations_boto3.py | 36 +++++++++++-------- 3 files changed, 30 insertions(+), 50 deletions(-) diff --git a/moto/organizations/models.py b/moto/organizations/models.py index 93408a353..18d706187 100644 --- a/moto/organizations/models.py +++ b/moto/organizations/models.py @@ -95,19 +95,6 @@ class FakeAccount(BaseModel): } } - @property - def close_account_status(self): - return { - "CloseAccountStatus": { - "Id": self.create_account_status_id, - "AccountName": self.name, - "State": "SUCCEEDED", - "RequestedTimestamp": unix_time(datetime.datetime.utcnow()), - "CompletedTimestamp": unix_time(datetime.datetime.utcnow()), - "AccountId": self.id, - } - } - def describe(self): return { "Id": self.id, @@ -119,6 +106,11 @@ class FakeAccount(BaseModel): "JoinedTimestamp": unix_time(self.create_time), } + def close(self): + # TODO: The CloseAccount spec allows the account to pass through a + # "PENDING_CLOSURE" state before reaching the SUSPNEDED state. + self.status = "SUSPENDED" + class FakeOrganizationalUnit(BaseModel): def __init__(self, organization, **kwargs): @@ -459,14 +451,10 @@ class OrganizationsBackend(BaseBackend): return new_account.create_account_status def close_account(self, **kwargs): - for account_index in range(len(self.accounts)): - if kwargs["AccountId"] == self.accounts[account_index].id: - closed_account_status = self.accounts[ - account_index - ].close_account_status - del self.accounts[account_index] - return closed_account_status - + for account in self.accounts: + if account.id == kwargs["AccountId"]: + account.close() + return raise AccountNotFoundException def get_account_by_id(self, account_id): diff --git a/tests/test_organizations/organizations_test_utils.py b/tests/test_organizations/organizations_test_utils.py index 1a3caae22..15bee591b 100644 --- a/tests/test_organizations/organizations_test_utils.py +++ b/tests/test_organizations/organizations_test_utils.py @@ -135,17 +135,3 @@ def validate_service_control_policy(org, response): response.should.have.key("PolicySummary").should.be.a(dict) response.should.have.key("Content").should.be.a(str) validate_policy_summary(org, response["PolicySummary"]) - - -def validate_account_created(accounts_list, account_id): - account_created = False - for account in accounts_list: - if account_id == account["Id"]: - account_created = True - assert account_created - - -def validate_account_closed(accounts_list, account_id): - for account in accounts_list: - if account_id == account["Id"]: - assert False diff --git a/tests/test_organizations/test_organizations_boto3.py b/tests/test_organizations/test_organizations_boto3.py index 52f9c8063..8d02309f0 100644 --- a/tests/test_organizations/test_organizations_boto3.py +++ b/tests/test_organizations/test_organizations_boto3.py @@ -27,8 +27,6 @@ from .organizations_test_utils import ( validate_create_account_status, validate_service_control_policy, validate_policy_summary, - validate_account_created, - validate_account_closed, ) @@ -176,30 +174,38 @@ def test_create_account(): @mock_organizations -def test_close_account(): +def test_close_account_returns_nothing(): client = boto3.client("organizations", region_name="us-east-1") client.create_organization(FeatureSet="ALL") create_status = client.create_account(AccountName=mockname, Email=mockemail)[ "CreateAccountStatus" ] created_account_id = create_status["AccountId"] - accounts_list_before = client.list_accounts()["Accounts"] - validate_account_created( - accounts_list=accounts_list_before, - account_id=created_account_id, - ) - client.close_account(AccountId=created_account_id) + resp = client.close_account(AccountId=created_account_id) - accounts_list_after = client.list_accounts()["Accounts"] - validate_account_closed(accounts_list_after, created_account_id) - number_accounts_before = len(accounts_list_before) - number_accounts_after = len(accounts_list_after) - (number_accounts_before - number_accounts_after).should.equal(1) + del resp["ResponseMetadata"] + + assert resp == {} @mock_organizations -def test_close_account_exception(): +def test_close_account_puts_account_in_suspended_status(): + client = boto3.client("organizations", region_name="us-east-1") + client.create_organization(FeatureSet="ALL") + create_status = client.create_account(AccountName=mockname, Email=mockemail)[ + "CreateAccountStatus" + ] + created_account_id = create_status["AccountId"] + + client.close_account(AccountId=created_account_id) + + account = client.describe_account(AccountId=created_account_id)["Account"] + account["Status"].should.equal("SUSPENDED") + + +@mock_organizations +def test_close_account_id_not_in_org_raises_exception(): client = boto3.client("organizations", region_name="us-east-1") client.create_organization(FeatureSet="ALL") uncreated_fake_account_id = "123456789101"