Skip to content

Commit 9df845c

Browse files
authored
Align organizations PolicyNotFoundException with boto3 response (#6955)
1 parent e234693 commit 9df845c

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

moto/organizations/exceptions.py

+7
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,10 @@ def __init__(self) -> None:
109109
super().__init__(
110110
"TargetNotFoundException", "You specified a target that doesn't exist."
111111
)
112+
113+
114+
class PolicyNotFoundException(JsonRESTError):
115+
code = 400
116+
117+
def __init__(self, message: str) -> None:
118+
super().__init__("PolicyNotFoundException", message)

moto/organizations/models.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
AWSOrganizationsNotInUseException,
1717
AccountNotRegisteredException,
1818
RootNotFoundException,
19+
PolicyNotFoundException,
1920
PolicyTypeAlreadyEnabledException,
2021
PolicyTypeNotEnabledException,
2122
TargetNotFoundException,
@@ -599,8 +600,7 @@ def describe_policy(self, **kwargs: Any) -> Dict[str, Any]:
599600
(p for p in self.policies if p.id == kwargs["PolicyId"]), None
600601
)
601602
if policy is None:
602-
raise RESTError(
603-
"PolicyNotFoundException",
603+
raise PolicyNotFoundException(
604604
"You specified a policy that doesn't exist.",
605605
)
606606
else:
@@ -612,8 +612,7 @@ def get_policy_by_id(self, policy_id: str) -> FakePolicy:
612612
(policy for policy in self.policies if policy.id == policy_id), None
613613
)
614614
if policy is None:
615-
raise RESTError(
616-
"PolicyNotFoundException",
615+
raise PolicyNotFoundException(
617616
"We can't find a policy with the PolicyId that you specified.",
618617
)
619618
return policy
@@ -668,8 +667,7 @@ def delete_policy(self, **kwargs: Any) -> None:
668667
)
669668
del self.policies[idx]
670669
return
671-
raise RESTError(
672-
"PolicyNotFoundException",
670+
raise PolicyNotFoundException(
673671
"We can't find a policy with the PolicyId that you specified.",
674672
)
675673

@@ -735,8 +733,7 @@ def list_targets_for_policy(self, **kwargs: Any) -> Dict[str, Any]:
735733
(p for p in self.policies if p.id == kwargs["PolicyId"]), None
736734
)
737735
if policy is None:
738-
raise RESTError(
739-
"PolicyNotFoundException",
736+
raise PolicyNotFoundException(
740737
"You specified a policy that doesn't exist.",
741738
)
742739
else:

tests/test_organizations/test_organizations_boto3.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,10 @@ def test_describe_policy_exception():
683683
client.describe_policy(PolicyId=policy_id)
684684
ex = e.value
685685
assert ex.operation_name == "DescribePolicy"
686-
assert ex.response["Error"]["Code"] == "400"
687-
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
686+
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
687+
assert (
688+
ex.response["Error"]["Message"] == "You specified a policy that doesn't exist."
689+
)
688690
with pytest.raises(ClientError) as e:
689691
client.describe_policy(PolicyId="meaninglessstring")
690692
ex = e.value
@@ -896,8 +898,11 @@ def test_delete_policy_exception():
896898
client.delete_policy(PolicyId=non_existent_policy_id)
897899
ex = e.value
898900
assert ex.operation_name == "DeletePolicy"
899-
assert ex.response["Error"]["Code"] == "400"
900-
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
901+
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
902+
assert (
903+
ex.response["Error"]["Message"]
904+
== "We can't find a policy with the PolicyId that you specified."
905+
)
901906

902907
# Attempt to delete an attached policy
903908
policy_id = client.create_policy(
@@ -993,8 +998,11 @@ def test_update_policy_exception():
993998
client.update_policy(PolicyId=non_existent_policy_id)
994999
ex = e.value
9951000
assert ex.operation_name == "UpdatePolicy"
996-
assert ex.response["Error"]["Code"] == "400"
997-
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
1001+
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
1002+
assert (
1003+
ex.response["Error"]["Message"]
1004+
== "We can't find a policy with the PolicyId that you specified."
1005+
)
9981006

9991007

10001008
@mock_organizations
@@ -1145,8 +1153,10 @@ def test_list_targets_for_policy_exception():
11451153
client.list_targets_for_policy(PolicyId=policy_id)
11461154
ex = e.value
11471155
assert ex.operation_name == "ListTargetsForPolicy"
1148-
assert ex.response["Error"]["Code"] == "400"
1149-
assert "PolicyNotFoundException" in ex.response["Error"]["Message"]
1156+
assert ex.response["Error"]["Code"] == "PolicyNotFoundException"
1157+
assert (
1158+
ex.response["Error"]["Message"] == "You specified a policy that doesn't exist."
1159+
)
11501160
with pytest.raises(ClientError) as e:
11511161
client.list_targets_for_policy(PolicyId="meaninglessstring")
11521162
ex = e.value

0 commit comments

Comments
 (0)