diff --git a/moto/iam/models.py b/moto/iam/models.py index 5608a87d..b64c9402 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -1233,6 +1233,14 @@ class IAMBackend(BaseBackend): group = self.get_group(group_name) return group.get_policy(policy_name) + def delete_group(self, group_name): + try: + del self.groups[group_name] + except KeyError: + raise IAMNotFoundException( + "The group with name {0} cannot be found.".format(group_name) + ) + def create_user(self, user_name, path="/"): if user_name in self.users: raise IAMConflictException( diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 08fe13dc..4bd1aa80 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -428,6 +428,12 @@ class IamResponse(BaseResponse): template = self.response_template(GET_GROUP_POLICY_TEMPLATE) return template.render(name="GetGroupPolicyResponse", **policy_result) + def delete_group(self): + group_name = self._get_param("GroupName") + iam_backend.delete_group(group_name) + template = self.response_template(GENERIC_EMPTY_TEMPLATE) + return template.render(name="DeleteGroup") + def create_user(self): user_name = self._get_param("UserName") path = self._get_param("Path") diff --git a/tests/test_iam/test_iam_groups.py b/tests/test_iam/test_iam_groups.py index 7fd29928..7b73e89e 100644 --- a/tests/test_iam/test_iam_groups.py +++ b/tests/test_iam/test_iam_groups.py @@ -8,6 +8,7 @@ import sure # noqa from nose.tools import assert_raises from boto.exception import BotoServerError +from botocore.exceptions import ClientError from moto import mock_iam, mock_iam_deprecated MOCK_POLICY = """ @@ -182,3 +183,25 @@ def test_list_group_policies(): conn.list_group_policies(GroupName="my-group")["PolicyNames"].should.equal( ["my-policy"] ) + + +@mock_iam +def test_delete_group(): + conn = boto3.client("iam", region_name="us-east-1") + conn.create_group(GroupName="my-group") + groups = conn.list_groups() + assert groups["Groups"][0]["GroupName"] == "my-group" + assert len(groups["Groups"]) == 1 + conn.delete_group(GroupName="my-group") + conn.list_groups()["Groups"].should.be.empty + + +@mock_iam +def test_delete_unknown_group(): + conn = boto3.client("iam", region_name="us-east-1") + with assert_raises(ClientError) as err: + conn.delete_group(GroupName="unknown-group") + err.exception.response["Error"]["Code"].should.equal("NoSuchEntity") + err.exception.response["Error"]["Message"].should.equal( + "The group with name unknown-group cannot be found." + )