Align organizations PolicyNotFoundException with boto3 response (#6955)

This commit is contained in:
Luke Simons 2023-10-27 13:59:26 +01:00 committed by GitHub
parent e234693ec5
commit 9df845cc1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 16 deletions

View File

@ -109,3 +109,10 @@ class TargetNotFoundException(JsonRESTError):
super().__init__( super().__init__(
"TargetNotFoundException", "You specified a target that doesn't exist." "TargetNotFoundException", "You specified a target that doesn't exist."
) )
class PolicyNotFoundException(JsonRESTError):
code = 400
def __init__(self, message: str) -> None:
super().__init__("PolicyNotFoundException", message)

View File

@ -16,6 +16,7 @@ from moto.organizations.exceptions import (
AWSOrganizationsNotInUseException, AWSOrganizationsNotInUseException,
AccountNotRegisteredException, AccountNotRegisteredException,
RootNotFoundException, RootNotFoundException,
PolicyNotFoundException,
PolicyTypeAlreadyEnabledException, PolicyTypeAlreadyEnabledException,
PolicyTypeNotEnabledException, PolicyTypeNotEnabledException,
TargetNotFoundException, TargetNotFoundException,
@ -599,8 +600,7 @@ class OrganizationsBackend(BaseBackend):
(p for p in self.policies if p.id == kwargs["PolicyId"]), None (p for p in self.policies if p.id == kwargs["PolicyId"]), None
) )
if policy is None: if policy is None:
raise RESTError( raise PolicyNotFoundException(
"PolicyNotFoundException",
"You specified a policy that doesn't exist.", "You specified a policy that doesn't exist.",
) )
else: else:
@ -612,8 +612,7 @@ class OrganizationsBackend(BaseBackend):
(policy for policy in self.policies if policy.id == policy_id), None (policy for policy in self.policies if policy.id == policy_id), None
) )
if policy is None: if policy is None:
raise RESTError( raise PolicyNotFoundException(
"PolicyNotFoundException",
"We can't find a policy with the PolicyId that you specified.", "We can't find a policy with the PolicyId that you specified.",
) )
return policy return policy
@ -668,8 +667,7 @@ class OrganizationsBackend(BaseBackend):
) )
del self.policies[idx] del self.policies[idx]
return return
raise RESTError( raise PolicyNotFoundException(
"PolicyNotFoundException",
"We can't find a policy with the PolicyId that you specified.", "We can't find a policy with the PolicyId that you specified.",
) )
@ -735,8 +733,7 @@ class OrganizationsBackend(BaseBackend):
(p for p in self.policies if p.id == kwargs["PolicyId"]), None (p for p in self.policies if p.id == kwargs["PolicyId"]), None
) )
if policy is None: if policy is None:
raise RESTError( raise PolicyNotFoundException(
"PolicyNotFoundException",
"You specified a policy that doesn't exist.", "You specified a policy that doesn't exist.",
) )
else: else:

View File

@ -683,8 +683,10 @@ def test_describe_policy_exception():
client.describe_policy(PolicyId=policy_id) client.describe_policy(PolicyId=policy_id)
ex = e.value ex = e.value
assert ex.operation_name == "DescribePolicy" assert ex.operation_name == "DescribePolicy"
assert ex.response["Error"]["Code"] == "400" assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"] assert (
ex.response["Error"]["Message"] == "You specified a policy that doesn't exist."
)
with pytest.raises(ClientError) as e: with pytest.raises(ClientError) as e:
client.describe_policy(PolicyId="meaninglessstring") client.describe_policy(PolicyId="meaninglessstring")
ex = e.value ex = e.value
@ -896,8 +898,11 @@ def test_delete_policy_exception():
client.delete_policy(PolicyId=non_existent_policy_id) client.delete_policy(PolicyId=non_existent_policy_id)
ex = e.value ex = e.value
assert ex.operation_name == "DeletePolicy" assert ex.operation_name == "DeletePolicy"
assert ex.response["Error"]["Code"] == "400" assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"] assert (
ex.response["Error"]["Message"]
== "We can't find a policy with the PolicyId that you specified."
)
# Attempt to delete an attached policy # Attempt to delete an attached policy
policy_id = client.create_policy( policy_id = client.create_policy(
@ -993,8 +998,11 @@ def test_update_policy_exception():
client.update_policy(PolicyId=non_existent_policy_id) client.update_policy(PolicyId=non_existent_policy_id)
ex = e.value ex = e.value
assert ex.operation_name == "UpdatePolicy" assert ex.operation_name == "UpdatePolicy"
assert ex.response["Error"]["Code"] == "400" assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"] assert (
ex.response["Error"]["Message"]
== "We can't find a policy with the PolicyId that you specified."
)
@mock_organizations @mock_organizations
@ -1145,8 +1153,10 @@ def test_list_targets_for_policy_exception():
client.list_targets_for_policy(PolicyId=policy_id) client.list_targets_for_policy(PolicyId=policy_id)
ex = e.value ex = e.value
assert ex.operation_name == "ListTargetsForPolicy" assert ex.operation_name == "ListTargetsForPolicy"
assert ex.response["Error"]["Code"] == "400" assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"] assert (
ex.response["Error"]["Message"] == "You specified a policy that doesn't exist."
)
with pytest.raises(ClientError) as e: with pytest.raises(ClientError) as e:
client.list_targets_for_policy(PolicyId="meaninglessstring") client.list_targets_for_policy(PolicyId="meaninglessstring")
ex = e.value ex = e.value