diff --git a/profiles/schema.py b/profiles/schema.py index ce46fd0b..d7c78de4 100644 --- a/profiles/schema.py +++ b/profiles/schema.py @@ -1142,12 +1142,19 @@ def _handle_primary_email(profile, primary_email_input): email_address = primary_email_input["email"] verified = primary_email_input.get("verified", False) - email, email_created = profile.emails.get_or_create( - email=email_address, - defaults={"email_type": EmailType.NONE, "verified": verified}, - ) - - profile.emails.exclude(pk=email.pk).filter(primary=True).update(primary=False) + email = profile.emails.filter(email=email_address).first() + if email: + profile.emails.exclude(pk=email.pk).filter(primary=True).update( + primary=False + ) + else: + profile.emails.filter(primary=True).update(primary=False) + email = profile.emails.create( + email=email_address, + email_type=EmailType.NONE, + primary=True, + verified=verified, + ) if not email.primary or email.verified is not verified: email.primary = True diff --git a/profiles/tests/test_gql_create_or_update_profile_with_verified_personal_information_mutation.py b/profiles/tests/test_gql_create_or_update_profile_with_verified_personal_information_mutation.py index 7be8fb70..ac5156c7 100644 --- a/profiles/tests/test_gql_create_or_update_profile_with_verified_personal_information_mutation.py +++ b/profiles/tests/test_gql_create_or_update_profile_with_verified_personal_information_mutation.py @@ -47,6 +47,7 @@ def execute_mutation(input_data, gql_client): def execute_successful_mutation(input_data, gql_client): executed = execute_mutation(input_data, gql_client) + assert "errors" not in executed global_profile_id = executed["data"]["prof"]["profile"]["id"] profile_id = uuid.UUID(from_global_id(global_profile_id)[1]) @@ -339,6 +340,40 @@ def test_existing_primary_email_remains_when_trying_to_set_the_same_email_as_a_p assert email.email_type == old_email.email_type +def test_change_primary_email_when_there_duplicate_emails_for_an_existing_profile( + user_gql_client, +): + """When a user has updated their email e.g. using updateMyProfile mutation, there's + no check for duplicate emails. Primary email update coming e.g. keycloak shouldn't fail + if there's duplicate emails. Instead, using the first matching email should be enough. + """ + original_email = EmailFactory(verified=False, primary=True) + EmailFactory( + email=original_email.email, + profile=original_email.profile, + verified=False, + primary=False, + ) + user_id = original_email.profile.user.uuid + + input_data = primary_email_input_data(user_id, email=original_email.email) + input_data["profile"]["primaryEmail"]["verified"] = True + profile = execute_successful_mutation(input_data, user_gql_client) + + assert profile.emails.count() == 2 + assert profile.emails.filter(email=original_email.email).count() == 2 + + email = profile.emails.get(primary=True) + assert email.email == original_email.email + assert email.email_type == EmailType.NONE + assert email.verified is True + + email = profile.emails.get(primary=False) + assert email.email == original_email.email + assert email.email_type == EmailType.NONE + assert email.verified is False + + @pytest.mark.parametrize( "test_email", ("", " ", "not_an_email", " extra_white_space@address.example") ) diff --git a/profiles/tests/test_gql_create_or_update_user_profile_mutation.py b/profiles/tests/test_gql_create_or_update_user_profile_mutation.py index 052d910e..9db71829 100644 --- a/profiles/tests/test_gql_create_or_update_user_profile_mutation.py +++ b/profiles/tests/test_gql_create_or_update_user_profile_mutation.py @@ -47,6 +47,7 @@ def execute_mutation(input_data, gql_client): def execute_successful_mutation(input_data, gql_client): executed = execute_mutation(input_data, gql_client) + assert "errors" not in executed global_profile_id = executed["data"]["prof"]["profile"]["id"] profile_id = uuid.UUID(from_global_id(global_profile_id)[1])