Skip to content

Commit

Permalink
Merge pull request #468 from City-of-Helsinki/HP-2191-email-change-ja…
Browse files Browse the repository at this point in the history
…m-fix

HP-2191 | fix: settings primary email shouldn't fail on duplicate emails
  • Loading branch information
charn authored Feb 9, 2024
2 parents 878e322 + 2ddb084 commit 3ff2861
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
19 changes: 13 additions & 6 deletions profiles/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -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", " [email protected]")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down

0 comments on commit 3ff2861

Please sign in to comment.