From 1db42fb865aabffdd9b6c60e286231a64e87f77f Mon Sep 17 00:00:00 2001 From: usmangani1 Date: Mon, 27 Jul 2020 20:02:41 +0530 Subject: [PATCH] FIX : IAM - Added support for pathPrefix in list_users_function (#3180) * FIX:IAM-Added support for pathPrefix in list_users_function * removed changes for roles * Added test for non decorator * changed filter function Co-authored-by: usmankb --- moto/iam/models.py | 8 ++++++++ moto/iam/responses.py | 1 - tests/test_iam/test_iam.py | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/moto/iam/models.py b/moto/iam/models.py index 82dc84be5..49755e57a 100755 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -946,6 +946,10 @@ class AccountSummary(BaseModel): return len(self._iam_backend.users) +def filter_items_with_path_prefix(path_prefix, items): + return [role for role in items if role.path.startswith(path_prefix)] + + class IAMBackend(BaseBackend): def __init__(self): self.instance_profiles = {} @@ -1490,7 +1494,11 @@ class IAMBackend(BaseBackend): def list_users(self, path_prefix, marker, max_items): users = None try: + users = self.users.values() + if path_prefix: + users = filter_items_with_path_prefix(path_prefix, users) + except KeyError: raise IAMNotFoundException( "Users {0}, {1}, {2} not found".format(path_prefix, marker, max_items) diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 8eb1730ea..6f785f8ac 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -337,7 +337,6 @@ class IamResponse(BaseResponse): def list_roles(self): roles = iam_backend.get_roles() - template = self.response_template(LIST_ROLES_TEMPLATE) return template.render(roles=roles) diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index 4ae5ad49e..610333303 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -762,6 +762,12 @@ def test_list_users(): user["Path"].should.equal("/") user["Arn"].should.equal("arn:aws:iam::{}:user/my-user".format(ACCOUNT_ID)) + conn.create_user(UserName="my-user-1", Path="myUser") + response = conn.list_users(PathPrefix="my") + user = response["Users"][0] + user["UserName"].should.equal("my-user-1") + user["Path"].should.equal("myUser") + @mock_iam() def test_user_policies():