From 158db1f5d635a3504003e3a444fcbd31a4054aa2 Mon Sep 17 00:00:00 2001 From: gruebel Date: Thu, 21 Nov 2019 22:03:25 +0100 Subject: [PATCH] Move exception to dedicated class --- moto/organizations/exceptions.py | 15 +++++++++++++++ moto/organizations/models.py | 16 ++++------------ .../test_organizations_boto3.py | 18 +++++++++--------- 3 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 moto/organizations/exceptions.py diff --git a/moto/organizations/exceptions.py b/moto/organizations/exceptions.py new file mode 100644 index 000000000..834165bcb --- /dev/null +++ b/moto/organizations/exceptions.py @@ -0,0 +1,15 @@ +from __future__ import unicode_literals + +import json +from werkzeug.exceptions import BadRequest + + +class InvalidInputError(BadRequest): + def __init__(self): + super(InvalidInputError, self).__init__() + self.description = json.dumps( + { + "message": "You provided a value that does not match the required pattern.", + "__type": "InvalidInputException", + } + ) diff --git a/moto/organizations/models.py b/moto/organizations/models.py index 2717d7ef8..7a9c73e9e 100644 --- a/moto/organizations/models.py +++ b/moto/organizations/models.py @@ -8,6 +8,7 @@ from moto.core import BaseBackend, BaseModel from moto.core.exceptions import RESTError from moto.core.utils import unix_time from moto.organizations import utils +from moto.organizations.exceptions import InvalidInputError class FakeOrganization(BaseModel): @@ -447,10 +448,7 @@ class OrganizationsBackend(BaseBackend): account = next((a for a in self.accounts if a.id == kwargs["ResourceId"]), None) if account is None: - raise RESTError( - "InvalidInputException", - "You provided a value that does not match the required pattern.", - ) + raise InvalidInputError new_tags = {tag["Key"]: tag["Value"] for tag in kwargs["Tags"]} account.tags.update(new_tags) @@ -459,10 +457,7 @@ class OrganizationsBackend(BaseBackend): account = next((a for a in self.accounts if a.id == kwargs["ResourceId"]), None) if account is None: - raise RESTError( - "InvalidInputException", - "You provided a value that does not match the required pattern.", - ) + raise InvalidInputError tags = [{"Key": key, "Value": value} for key, value in account.tags.items()] return dict(Tags=tags) @@ -471,10 +466,7 @@ class OrganizationsBackend(BaseBackend): account = next((a for a in self.accounts if a.id == kwargs["ResourceId"]), None) if account is None: - raise RESTError( - "InvalidInputException", - "You provided a value that does not match the required pattern.", - ) + raise InvalidInputError for key in kwargs["TagKeys"]: account.tags.pop(key, None) diff --git a/tests/test_organizations/test_organizations_boto3.py b/tests/test_organizations/test_organizations_boto3.py index fb3ab3b24..dd79ae787 100644 --- a/tests/test_organizations/test_organizations_boto3.py +++ b/tests/test_organizations/test_organizations_boto3.py @@ -641,9 +641,9 @@ def test_tag_resource_errors(): ) ex = e.exception ex.operation_name.should.equal("TagResource") - ex.response["Error"]["Code"].should.equal("400") - ex.response["Error"]["Message"].should.contain("InvalidInputException") - ex.response["Error"]["Message"].should.contain( + ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) + ex.response["Error"]["Code"].should.contain("InvalidInputException") + ex.response["Error"]["Message"].should.equal( "You provided a value that does not match the required pattern." ) @@ -671,9 +671,9 @@ def test_list_tags_for_resource_errors(): client.list_tags_for_resource(ResourceId="000000000000") ex = e.exception ex.operation_name.should.equal("ListTagsForResource") - ex.response["Error"]["Code"].should.equal("400") - ex.response["Error"]["Message"].should.contain("InvalidInputException") - ex.response["Error"]["Message"].should.contain( + ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) + ex.response["Error"]["Code"].should.contain("InvalidInputException") + ex.response["Error"]["Message"].should.equal( "You provided a value that does not match the required pattern." ) @@ -708,8 +708,8 @@ def test_untag_resource_errors(): client.untag_resource(ResourceId="000000000000", TagKeys=["key"]) ex = e.exception ex.operation_name.should.equal("UntagResource") - ex.response["Error"]["Code"].should.equal("400") - ex.response["Error"]["Message"].should.contain("InvalidInputException") - ex.response["Error"]["Message"].should.contain( + ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) + ex.response["Error"]["Code"].should.contain("InvalidInputException") + ex.response["Error"]["Message"].should.equal( "You provided a value that does not match the required pattern." )