Cognito: update UserLastModifiedDate when changing user attributes (#7464)

This commit is contained in:
João Pedro Schmitt 2024-03-14 16:10:35 -07:00 committed by GitHub
parent 565442e23d
commit 18a392bd0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 0 deletions

View File

@ -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):

View File

@ -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")