From 18a392bd0ae965ef18b76d0adfd5527a8a9d87f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Schmitt?= Date: Thu, 14 Mar 2024 16:10:35 -0700 Subject: [PATCH] Cognito: update UserLastModifiedDate when changing user attributes (#7464) --- moto/cognitoidp/models.py | 2 + tests/test_cognitoidp/test_cognitoidp.py | 60 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/moto/cognitoidp/models.py b/moto/cognitoidp/models.py index 0e960764e..051eb7ee4 100644 --- a/moto/cognitoidp/models.py +++ b/moto/cognitoidp/models.py @@ -869,6 +869,7 @@ class CognitoIdpUser(BaseModel): flat_attributes.update(flatten_attrs(new_attributes)) self.attribute_lookup = flat_attributes self.attributes = expand_attrs(flat_attributes) + self.last_modified_date = utcnow() def delete_attributes(self, attrs_to_delete: List[str]) -> None: flat_attributes = flatten_attrs(self.attributes) @@ -891,6 +892,7 @@ class CognitoIdpUser(BaseModel): ) self.attribute_lookup = flat_attributes self.attributes = expand_attrs(flat_attributes) + self.last_modified_date = utcnow() class CognitoResourceServer(BaseModel): diff --git a/tests/test_cognitoidp/test_cognitoidp.py b/tests/test_cognitoidp/test_cognitoidp.py index 1ff151bfb..ee4691d99 100644 --- a/tests/test_cognitoidp/test_cognitoidp.py +++ b/tests/test_cognitoidp/test_cognitoidp.py @@ -6,6 +6,7 @@ import json import os import random import re +import time import uuid from unittest import SkipTest, mock @@ -2346,6 +2347,65 @@ def test_admin_get_missing_user_with_username_attributes(): assert err["Message"] == "User does not exist." +@mock_aws +def test_new_user_contains_same_created_and_updated_dates(): + conn = boto3.client("cognito-idp", "us-west-2") + + username = "test@example.com" + user_pool_id = conn.create_user_pool( + PoolName=str(uuid.uuid4()), UsernameAttributes=["email"] + )["UserPool"]["Id"] + + resp = conn.admin_create_user(UserPoolId=user_pool_id, Username=username) + new_user = resp["User"] + + assert new_user["UserCreateDate"] == new_user["UserLastModifiedDate"] + + +@mock_aws +def test_update_user_attributes_also_changes_last_modified_date(): + conn = boto3.client("cognito-idp", "us-west-2") + + username = "test@example.com" + user_pool_id = conn.create_user_pool( + PoolName=str(uuid.uuid4()), UsernameAttributes=["email"] + )["UserPool"]["Id"] + conn.admin_create_user(UserPoolId=user_pool_id, Username=username) + + time.sleep(1) # Wait enough to account for a different in the time format + conn.admin_update_user_attributes( + UserPoolId=user_pool_id, + Username=username, + UserAttributes=[{"Name": "phone_number", "Value": "+123456789"}], + ) + + user = conn.admin_get_user(UserPoolId=user_pool_id, Username=username) + assert user["UserCreateDate"] < user["UserLastModifiedDate"] + + +@mock_aws +def test_delete_user_attributes_also_changes_last_modified_date(): + conn = boto3.client("cognito-idp", "us-west-2") + + username = "test@example.com" + user_pool_id = conn.create_user_pool( + PoolName=str(uuid.uuid4()), UsernameAttributes=["email"] + )["UserPool"]["Id"] + conn.admin_create_user( + UserPoolId=user_pool_id, + Username=username, + UserAttributes=[{"Name": "phone_number", "Value": "+123456789"}], + ) + + time.sleep(1) # Wait enough to account for a different in the time format + conn.admin_delete_user_attributes( + UserPoolId=user_pool_id, Username=username, UserAttributeNames=["phone_number"] + ) + + user = conn.admin_get_user(UserPoolId=user_pool_id, Username=username) + assert user["UserCreateDate"] < user["UserLastModifiedDate"] + + @mock_aws def test_get_user(): conn = boto3.client("cognito-idp", "us-west-2")