Skip to content

Commit

Permalink
Align organizations PolicyNotFoundException with boto3 response (#6955)
Browse files Browse the repository at this point in the history
  • Loading branch information
siluk-aws authored Oct 27, 2023
1 parent e234693 commit 9df845c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
7 changes: 7 additions & 0 deletions moto/organizations/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,10 @@ def __init__(self) -> None:
super().__init__(
"TargetNotFoundException", "You specified a target that doesn't exist."
)


class PolicyNotFoundException(JsonRESTError):
code = 400

def __init__(self, message: str) -> None:
super().__init__("PolicyNotFoundException", message)
13 changes: 5 additions & 8 deletions moto/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
AWSOrganizationsNotInUseException,
AccountNotRegisteredException,
RootNotFoundException,
PolicyNotFoundException,
PolicyTypeAlreadyEnabledException,
PolicyTypeNotEnabledException,
TargetNotFoundException,
Expand Down Expand Up @@ -599,8 +600,7 @@ def describe_policy(self, **kwargs: Any) -> Dict[str, Any]:
(p for p in self.policies if p.id == kwargs["PolicyId"]), None
)
if policy is None:
raise RESTError(
"PolicyNotFoundException",
raise PolicyNotFoundException(
"You specified a policy that doesn't exist.",
)
else:
Expand All @@ -612,8 +612,7 @@ def get_policy_by_id(self, policy_id: str) -> FakePolicy:
(policy for policy in self.policies if policy.id == policy_id), None
)
if policy is None:
raise RESTError(
"PolicyNotFoundException",
raise PolicyNotFoundException(
"We can't find a policy with the PolicyId that you specified.",
)
return policy
Expand Down Expand Up @@ -668,8 +667,7 @@ def delete_policy(self, **kwargs: Any) -> None:
)
del self.policies[idx]
return
raise RESTError(
"PolicyNotFoundException",
raise PolicyNotFoundException(
"We can't find a policy with the PolicyId that you specified.",
)

Expand Down Expand Up @@ -735,8 +733,7 @@ def list_targets_for_policy(self, **kwargs: Any) -> Dict[str, Any]:
(p for p in self.policies if p.id == kwargs["PolicyId"]), None
)
if policy is None:
raise RESTError(
"PolicyNotFoundException",
raise PolicyNotFoundException(
"You specified a policy that doesn't exist.",
)
else:
Expand Down
26 changes: 18 additions & 8 deletions tests/test_organizations/test_organizations_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,10 @@ def test_describe_policy_exception():
client.describe_policy(PolicyId=policy_id)
ex = e.value
assert ex.operation_name == "DescribePolicy"
assert ex.response["Error"]["Code"] == "400"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert (
ex.response["Error"]["Message"] == "You specified a policy that doesn't exist."
)
with pytest.raises(ClientError) as e:
client.describe_policy(PolicyId="meaninglessstring")
ex = e.value
Expand Down Expand Up @@ -896,8 +898,11 @@ def test_delete_policy_exception():
client.delete_policy(PolicyId=non_existent_policy_id)
ex = e.value
assert ex.operation_name == "DeletePolicy"
assert ex.response["Error"]["Code"] == "400"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert (
ex.response["Error"]["Message"]
== "We can't find a policy with the PolicyId that you specified."
)

# Attempt to delete an attached policy
policy_id = client.create_policy(
Expand Down Expand Up @@ -993,8 +998,11 @@ def test_update_policy_exception():
client.update_policy(PolicyId=non_existent_policy_id)
ex = e.value
assert ex.operation_name == "UpdatePolicy"
assert ex.response["Error"]["Code"] == "400"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert (
ex.response["Error"]["Message"]
== "We can't find a policy with the PolicyId that you specified."
)


@mock_organizations
Expand Down Expand Up @@ -1145,8 +1153,10 @@ def test_list_targets_for_policy_exception():
client.list_targets_for_policy(PolicyId=policy_id)
ex = e.value
assert ex.operation_name == "ListTargetsForPolicy"
assert ex.response["Error"]["Code"] == "400"
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
assert (
ex.response["Error"]["Message"] == "You specified a policy that doesn't exist."
)
with pytest.raises(ClientError) as e:
client.list_targets_for_policy(PolicyId="meaninglessstring")
ex = e.value
Expand Down

0 comments on commit 9df845c

Please sign in to comment.