From e84dad6509d1b752ca868116a099deb801b6776f Mon Sep 17 00:00:00 2001 From: Ernesto Ruy Sanchez Date: Wed, 20 Jul 2016 15:12:02 -0700 Subject: [PATCH 1/2] implemented iam list users --- moto/iam/models.py | 9 +++++++++ moto/iam/responses.py | 26 ++++++++++++++++++++++++++ tests/test_iam/test_iam.py | 8 ++++++++ 3 files changed, 43 insertions(+) diff --git a/moto/iam/models.py b/moto/iam/models.py index 0f95d6453..4d7711170 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -364,6 +364,15 @@ class IAMBackend(BaseBackend): return user + def list_users(self, path_prefix, marker, max_items): + users = None + try: + users = self.users + except KeyError: + raise IAMNotFoundException("Users {0}, {1}, {2} not found".format(path_prefix, marker, max_items)) + + return users + def create_login_profile(self, user_name, password): # This does not currently deal with PasswordPolicyViolation. user = self.get_user(user_name) diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 6895aa45a..8a237e60b 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -165,6 +165,14 @@ class IamResponse(BaseResponse): template = self.response_template(USER_TEMPLATE) return template.render(action='Get', user=user) + def list_users(self): + path_prefix = self._get_param('PathPrefix') + marker = self._get_param('Marker') + max_items = self._get_param('MaxItems') + user = iam_backend.list_users(path_prefix, marker, max_items) + template = self.response_template(LIST_USERS_TEMPLATE) + return template.render(action='List', user=user) + def create_login_profile(self): user_name = self._get_param('UserName') password = self._get_param('Password') @@ -592,6 +600,24 @@ USER_TEMPLATE = """<{{ action }}UserResponse> """ +LIST_USERS_TEMPLATE = """<{{ action }}UsersResponse> + <{{ action }}UsersResult> + + {% for user in users %} + + {{ user.id }} + {{ user.path }} + {{ user.name }} + arn:aws:iam::123456789012:user/{{ user.path }}/{{ user.name }} + + {% endfor %} + + + + 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE + +""" + CREATE_LOGIN_PROFILE_TEMPLATE = """ diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index 39f16e836..c5d92af6b 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals import boto +import boto3 import sure # noqa from nose.tools import assert_raises, assert_equals, assert_not_equals @@ -175,6 +176,13 @@ def test_get_user(): conn.create_user('my-user') conn.get_user('my-user') +@mock_iam() +def test_list_users(): + path_prefix = '/' + max_items = 10 + conn = boto3.client('iam') + conn.create_user(UserName='my-user') + conn.list_users(PathPrefix=path_prefix, MaxItems=max_items) @mock_iam() def test_create_login_profile(): From 9c587ec6b17431888d2e66fa2e7b45c158f27d37 Mon Sep 17 00:00:00 2001 From: Ernesto Ruy Sanchez Date: Mon, 25 Jul 2016 08:59:57 -0700 Subject: [PATCH 2/2] added assert response returns Users --- tests/test_iam/test_iam.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index c5d92af6b..89153ff69 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -182,7 +182,12 @@ def test_list_users(): max_items = 10 conn = boto3.client('iam') conn.create_user(UserName='my-user') - conn.list_users(PathPrefix=path_prefix, MaxItems=max_items) + response = conn.list_users(PathPrefix=path_prefix, MaxItems=max_items) + assert_equals( + response['Users'], + [] + ) + @mock_iam() def test_create_login_profile():