From fa3904df2962190fa7636763aa39133af2ef5ea3 Mon Sep 17 00:00:00 2001 From: Antonin Date: Mon, 10 Feb 2020 18:09:15 +0100 Subject: [PATCH] MessageAction for cognito admin_create_user is now handled If an invitation is attempted to be reset to a pool we validate that the user is indeed already in the pool else we raise a UserNotFoundException to match AWS behaviour --- moto/cognitoidp/models.py | 7 +++- moto/cognitoidp/responses.py | 2 + tests/test_cognitoidp/test_cognitoidp.py | 49 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/moto/cognitoidp/models.py b/moto/cognitoidp/models.py index 96b23a404..810077faf 100644 --- a/moto/cognitoidp/models.py +++ b/moto/cognitoidp/models.py @@ -564,12 +564,15 @@ class CognitoIdpBackend(BaseBackend): user.groups.discard(group) # User - def admin_create_user(self, user_pool_id, username, temporary_password, attributes): + def admin_create_user(self, user_pool_id, username, message_action, temporary_password, attributes): user_pool = self.user_pools.get(user_pool_id) if not user_pool: raise ResourceNotFoundError(user_pool_id) - if username in user_pool.users: + if message_action and message_action == "RESEND": + if username not in user_pool.users: + raise UserNotFoundError(username) + elif username in user_pool.users: raise UsernameExistsException(username) user = CognitoIdpUser( diff --git a/moto/cognitoidp/responses.py b/moto/cognitoidp/responses.py index fa3b7b0b5..6c89c4806 100644 --- a/moto/cognitoidp/responses.py +++ b/moto/cognitoidp/responses.py @@ -259,10 +259,12 @@ class CognitoIdpResponse(BaseResponse): def admin_create_user(self): user_pool_id = self._get_param("UserPoolId") username = self._get_param("Username") + message_action = self._get_param("MessageAction") temporary_password = self._get_param("TemporaryPassword") user = cognitoidp_backends[self.region].admin_create_user( user_pool_id, username, + message_action, temporary_password, self._get_param("UserAttributes", []), ) diff --git a/tests/test_cognitoidp/test_cognitoidp.py b/tests/test_cognitoidp/test_cognitoidp.py index 2f7ed11e5..d0a462c5c 100644 --- a/tests/test_cognitoidp/test_cognitoidp.py +++ b/tests/test_cognitoidp/test_cognitoidp.py @@ -911,6 +911,55 @@ def test_admin_create_existing_user(): caught.should.be.true +@mock_cognitoidp +def test_admin_resend_invitation_existing_user(): + conn = boto3.client("cognito-idp", "us-west-2") + + username = str(uuid.uuid4()) + value = str(uuid.uuid4()) + user_pool_id = conn.create_user_pool(PoolName=str(uuid.uuid4()))["UserPool"]["Id"] + conn.admin_create_user( + UserPoolId=user_pool_id, + Username=username, + UserAttributes=[{"Name": "thing", "Value": value}], + ) + + caught = False + try: + conn.admin_create_user( + UserPoolId=user_pool_id, + Username=username, + UserAttributes=[{"Name": "thing", "Value": value}], + MessageAction="RESEND", + ) + except conn.exceptions.UsernameExistsException: + caught = True + + caught.should.be.false + + +@mock_cognitoidp +def test_admin_resend_invitation_missing_user(): + conn = boto3.client("cognito-idp", "us-west-2") + + username = str(uuid.uuid4()) + value = str(uuid.uuid4()) + user_pool_id = conn.create_user_pool(PoolName=str(uuid.uuid4()))["UserPool"]["Id"] + + caught = False + try: + conn.admin_create_user( + UserPoolId=user_pool_id, + Username=username, + UserAttributes=[{"Name": "thing", "Value": value}], + MessageAction="RESEND", + ) + except conn.exceptions.UserNotFoundException: + caught = True + + caught.should.be.true + + @mock_cognitoidp def test_admin_get_user(): conn = boto3.client("cognito-idp", "us-west-2")