From 4bae0339c2f09b84639c64a7f7776bbc03aa87e5 Mon Sep 17 00:00:00 2001 From: Ivan Dromigny Date: Wed, 5 Feb 2020 12:03:24 +0100 Subject: [PATCH 1/3] Add Filter parameter for cognitoidp list_users() --- moto/cognitoidp/responses.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/moto/cognitoidp/responses.py b/moto/cognitoidp/responses.py index 80247b076..a170b7541 100644 --- a/moto/cognitoidp/responses.py +++ b/moto/cognitoidp/responses.py @@ -279,9 +279,13 @@ class CognitoIdpResponse(BaseResponse): user_pool_id = self._get_param("UserPoolId") limit = self._get_param("Limit") token = self._get_param("PaginationToken") + filt = self._get_param("Filter") users, token = cognitoidp_backends[self.region].list_users( user_pool_id, limit=limit, pagination_token=token ) + if filt: + name, value = filt.replace('"', '').split('=') + users = [user for user in users for attribute in user.attributes if attribute['Name'] == name and attribute['Value'] == value] response = {"Users": [user.to_json(extended=True) for user in users]} if token: response["PaginationToken"] = str(token) From 8115dd2d1b0b6b89a8ceba50d8a7edc309f49e52 Mon Sep 17 00:00:00 2001 From: Ivan Dromigny Date: Wed, 5 Feb 2020 12:03:33 +0100 Subject: [PATCH 2/3] Add test --- tests/test_cognitoidp/test_cognitoidp.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_cognitoidp/test_cognitoidp.py b/tests/test_cognitoidp/test_cognitoidp.py index 6a13683f0..27a6841f4 100644 --- a/tests/test_cognitoidp/test_cognitoidp.py +++ b/tests/test_cognitoidp/test_cognitoidp.py @@ -958,6 +958,15 @@ def test_list_users(): result["Users"].should.have.length_of(1) result["Users"][0]["Username"].should.equal(username) + username_bis = str(uuid.uuid4()) + conn.admin_create_user( + UserPoolId=user_pool_id, Username=username_bis, + UserAttributes=[{'Name': 'phone_number', 'Value': '+33666666666'}] + ) + result = conn.list_users(UserPoolId=user_pool_id, Filter='phone_number="+33666666666') + result["Users"].should.have.length_of(1) + result["Users"][0]["Username"].should.equal(username_bis) + @mock_cognitoidp def test_list_users_returns_limit_items(): From d8d057711dcf88a526b7bb454fa21e6050f122b4 Mon Sep 17 00:00:00 2001 From: Ivan Dromigny Date: Wed, 5 Feb 2020 14:19:08 +0100 Subject: [PATCH 3/3] Change from black linter --- moto/cognitoidp/responses.py | 9 +++++++-- tests/test_cognitoidp/test_cognitoidp.py | 9 ++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/moto/cognitoidp/responses.py b/moto/cognitoidp/responses.py index a170b7541..fa3b7b0b5 100644 --- a/moto/cognitoidp/responses.py +++ b/moto/cognitoidp/responses.py @@ -284,8 +284,13 @@ class CognitoIdpResponse(BaseResponse): user_pool_id, limit=limit, pagination_token=token ) if filt: - name, value = filt.replace('"', '').split('=') - users = [user for user in users for attribute in user.attributes if attribute['Name'] == name and attribute['Value'] == value] + name, value = filt.replace('"', "").split("=") + users = [ + user + for user in users + for attribute in user.attributes + if attribute["Name"] == name and attribute["Value"] == value + ] response = {"Users": [user.to_json(extended=True) for user in users]} if token: response["PaginationToken"] = str(token) diff --git a/tests/test_cognitoidp/test_cognitoidp.py b/tests/test_cognitoidp/test_cognitoidp.py index 27a6841f4..2f7ed11e5 100644 --- a/tests/test_cognitoidp/test_cognitoidp.py +++ b/tests/test_cognitoidp/test_cognitoidp.py @@ -960,10 +960,13 @@ def test_list_users(): username_bis = str(uuid.uuid4()) conn.admin_create_user( - UserPoolId=user_pool_id, Username=username_bis, - UserAttributes=[{'Name': 'phone_number', 'Value': '+33666666666'}] + UserPoolId=user_pool_id, + Username=username_bis, + UserAttributes=[{"Name": "phone_number", "Value": "+33666666666"}], + ) + result = conn.list_users( + UserPoolId=user_pool_id, Filter='phone_number="+33666666666' ) - result = conn.list_users(UserPoolId=user_pool_id, Filter='phone_number="+33666666666') result["Users"].should.have.length_of(1) result["Users"][0]["Username"].should.equal(username_bis)