Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating user in cognito #62

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/cognito/utils/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ def update_user(self, username: str, preferred_username: str, email: str) -> boo
old_attributes = user_attributes_to_dict(user['UserAttributes'])
new_attributes: list[AttributeTypeTypeDef] = []
reset_password = False
if old_attributes.get('preferred_username') != preferred_username:
if old_attributes.get('email') != email:
new_attributes.append({'Name': 'email', 'Value': email})
new_attributes.append({'Name': 'email_verified', 'Value': 'true'})
reset_password = True
if old_attributes.get('email') != email:
if old_attributes.get('preferred_username') != preferred_username:
new_attributes.append({'Name': 'preferred_username', 'Value': preferred_username})
if new_attributes:
self.client.admin_update_user_attributes(
Expand Down
20 changes: 20 additions & 0 deletions app/tests/cognito/test_cognito_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ def test_update_user_updates_managed(boto3, cognito_user_response_factory):
) in boto3.mock_calls


@patch('cognito.utils.client.client')
def test_update_user_updates_partial_managed(boto3, cognito_user_response_factory):
boto3.return_value.admin_get_user.return_value = cognito_user_response_factory(
'2ihg2ox304po', '1234', managed=True, attributes_key='UserAttributes'
)

client = Client()
updated = client.update_user('2ihg2ox304po', '5678', 'test@example.org')
assert updated is True
assert '().admin_update_user_attributes' in [call[0] for call in boto3.mock_calls]
assert '().admin_reset_user_password' not in [call[0] for call in boto3.mock_calls]
assert call().admin_update_user_attributes(
UserPoolId=client.user_pool_id,
Username='2ihg2ox304po',
UserAttributes=[{
'Name': 'preferred_username', 'Value': '5678'
}]
) in boto3.mock_calls


@patch('cognito.utils.client.client')
def test_update_user_does_not_update_unchanged_managed(boto3, cognito_user_response_factory):
boto3.return_value.admin_get_user.return_value = cognito_user_response_factory(
Expand Down
Loading