diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index 90ebf9a57..81611ace0 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -1687,7 +1687,7 @@ - [ ] admin_reset_user_password - [ ] admin_respond_to_auth_challenge - [ ] admin_set_user_mfa_preference -- [ ] admin_set_user_password +- [X] admin_set_user_password - [ ] admin_set_user_settings - [ ] admin_update_auth_event_feedback - [ ] admin_update_device_status diff --git a/moto/cognitoidp/models.py b/moto/cognitoidp/models.py index bfa7177f1..6ee71cbc0 100644 --- a/moto/cognitoidp/models.py +++ b/moto/cognitoidp/models.py @@ -353,7 +353,6 @@ class CognitoIdpUser(BaseModel): class CognitoResourceServer(BaseModel): def __init__(self, user_pool_id, identifier, name, scopes): - self.user_pool_id = user_pool_id self.identifier = identifier self.name = name @@ -1035,6 +1034,14 @@ class CognitoIdpBackend(BaseBackend): else: raise NotAuthorizedError(access_token) + def admin_set_user_password(self, user_pool_id, username, password, permanent): + user = self.admin_get_user(user_pool_id, username) + user.password = password + if permanent: + user.status = UserStatus["CONFIRMED"] + else: + user.status = UserStatus["FORCE_CHANGE_PASSWORD"] + cognitoidp_backends = {} for region in Session().get_available_regions("cognito-idp"): diff --git a/moto/cognitoidp/responses.py b/moto/cognitoidp/responses.py index 78725bcf1..e10a12282 100644 --- a/moto/cognitoidp/responses.py +++ b/moto/cognitoidp/responses.py @@ -449,6 +449,16 @@ class CognitoIdpResponse(BaseResponse): ) return "" + def admin_set_user_password(self): + user_pool_id = self._get_param("UserPoolId") + username = self._get_param("Username") + password = self._get_param("Password") + permanent = self._get_param("Permanent") + cognitoidp_backends[self.region].admin_set_user_password( + user_pool_id, username, password, permanent + ) + return "" + class CognitoIdpJsonWebKeyResponse(BaseResponse): def __init__(self): diff --git a/tests/test_cognitoidp/test_cognitoidp.py b/tests/test_cognitoidp/test_cognitoidp.py index 06dae9951..a5212b82e 100644 --- a/tests/test_cognitoidp/test_cognitoidp.py +++ b/tests/test_cognitoidp/test_cognitoidp.py @@ -1816,6 +1816,30 @@ def test_respond_to_auth_challenge_with_invalid_secret_hash(): caught.should.be.true +@mock_cognitoidp +def test_admin_set_user_password(): + conn = boto3.client("cognito-idp", "us-west-2") + + username = str(uuid.uuid4()) + value = str(uuid.uuid4()) + password = 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}], + ) + conn.admin_set_user_password( + UserPoolId=user_pool_id, Username=username, Password=password, Permanent=True + ) + result = conn.admin_get_user(UserPoolId=user_pool_id, Username=username) + result["Username"].should.equal(username) + result["UserAttributes"].should.have.length_of(1) + result["UserAttributes"][0]["Name"].should.equal("thing") + result["UserAttributes"][0]["Value"].should.equal(value) + result["UserStatus"].should.equal("CONFIRMED") + + # Test will retrieve public key from cognito.amazonaws.com/.well-known/jwks.json, # which isnt mocked in ServerMode if not settings.TEST_SERVER_MODE: