Skip to content

Commit 58a5925

Browse files
Update registration to check new live field
1 parent fec52ef commit 58a5925

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

backend/compact-connect/lambdas/python/provider-data-v1/handlers/provider_users.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,34 @@ def _post_provider_email_verify(event: dict, context: LambdaContext): # noqa: A
351351
new_email = provider_data.pendingEmailAddress
352352

353353
# Update email in Cognito
354-
config.cognito_client.admin_update_user_attributes(
355-
UserPoolId=config.provider_user_pool_id,
356-
Username=current_email, # Current username (email)
357-
UserAttributes=[
358-
{'Name': 'email', 'Value': new_email},
359-
{'Name': 'email_verified', 'Value': 'true'},
360-
],
361-
)
354+
try:
355+
config.cognito_client.admin_update_user_attributes(
356+
UserPoolId=config.provider_user_pool_id,
357+
Username=current_email, # Current username (email)
358+
UserAttributes=[
359+
{'Name': 'email', 'Value': new_email},
360+
{'Name': 'email_verified', 'Value': 'true'},
361+
],
362+
)
363+
except ClientError as e:
364+
if e.response['Error']['Code'] == 'AliasExistsException':
365+
# Another user was created with this email between verification start and finish
366+
logger.warning(
367+
'Email address became unavailable during verification process',
368+
compact=compact,
369+
provider_id=provider_id,
370+
new_email=new_email,
371+
)
372+
# Clear the verification data since the email is no longer available
373+
config.data_client.clear_provider_email_verification_data(
374+
compact=compact,
375+
provider_id=provider_id,
376+
)
377+
raise CCInvalidRequestException(
378+
'Email address is no longer available. Please try again with a different email address.'
379+
) from e
380+
# Re-raise other Cognito errors
381+
raise
362382

363383
# Update the provider record with new email and clear verification data
364384
config.data_client.complete_provider_email_update(

backend/compact-connect/lambdas/python/provider-data-v1/tests/function/test_handlers/test_provider_users_email.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,58 @@ def test_endpoint_calls_email_service_client_with_change_notification(self, mock
507507
mock_email_service_client.send_provider_email_change_notification.assert_called_once_with(
508508
compact=DEFAULT_COMPACT, old_email_address=TEST_OLD_EMAIL, new_email_address=TEST_NEW_EMAIL
509509
)
510+
511+
@patch('cc_common.config._Config.email_service_client')
512+
def test_endpoint_returns_400_if_new_email_becomes_unavailable_during_verification(self, mock_email_service_client):
513+
from cc_common.data_model.schema.provider import ProviderData
514+
from handlers.provider_users import provider_users_api_handler
515+
516+
# First create the old email user in Cognito so we can update it
517+
self.config.cognito_client.admin_create_user(
518+
UserPoolId=self.config.provider_user_pool_id,
519+
Username=TEST_OLD_EMAIL,
520+
UserAttributes=[
521+
{'Name': 'email', 'Value': TEST_OLD_EMAIL},
522+
{'Name': 'email_verified', 'Value': 'true'},
523+
{'Name': 'custom:compact', 'Value': DEFAULT_COMPACT},
524+
{'Name': 'custom:providerId', 'Value': DEFAULT_PROVIDER_ID},
525+
],
526+
MessageAction='SUPPRESS',
527+
)
528+
529+
# Create another user with the new email address to simulate it becoming unavailable
530+
# This simulates someone else registering with this email between verification start and finish
531+
self.config.cognito_client.admin_create_user(
532+
UserPoolId=self.config.provider_user_pool_id,
533+
Username=TEST_NEW_EMAIL,
534+
UserAttributes=[
535+
{'Name': 'email', 'Value': TEST_NEW_EMAIL},
536+
{'Name': 'email_verified', 'Value': 'true'},
537+
],
538+
MessageAction='SUPPRESS',
539+
)
540+
541+
event = self._when_testing_provider_user_event_with_custom_claims()
542+
543+
resp = provider_users_api_handler(event, self.mock_context)
544+
545+
self.assertEqual(400, resp['statusCode'])
546+
resp_body = json.loads(resp['body'])
547+
self.assertEqual(
548+
'Email address is no longer available. Please try again with a different email address.',
549+
resp_body['message'],
550+
)
551+
552+
# Verify pending fields were cleared from the provider record
553+
test_provider_record = self.test_data_generator.generate_default_provider()
554+
stored_provider_data = ProviderData.from_database_record(
555+
self.test_data_generator.load_provider_data_record_from_database(test_provider_record)
556+
)
557+
558+
# Pending fields should be cleared
559+
self.assertIsNone(stored_provider_data.pendingEmailAddress)
560+
self.assertIsNone(stored_provider_data.emailVerificationCode)
561+
self.assertIsNone(stored_provider_data.emailVerificationExpiry)
562+
563+
# Original email should remain unchanged
564+
self.assertEqual(TEST_OLD_EMAIL, stored_provider_data.compactConnectRegisteredEmailAddress)

0 commit comments

Comments
 (0)