Skip to content

Commit

Permalink
fix: handle updates profile's language and contact_method fields
Browse files Browse the repository at this point in the history
In graphene 3 when Enum's are used as an input to a field the
resolver now receives the Enum member directly rather than
the value. This was a breaking change that wasn't taken into
account when upgrading to the new graphene major version.

Refs: HP-2266, https://github.com/graphql-python/graphene/wiki/v3-release-notes#better-enum-support
  • Loading branch information
charn committed Mar 12, 2024
1 parent da3bcdf commit c4e795b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions profiles/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ def email_change_makes_it_unverified(item, field, value):

profile_had_primary_email = bool(profile.get_primary_email_value())

if language := profile_data.pop("language", None):
profile.language = language.value

if contact_method := profile_data.pop("contact_method", None):
profile.contact_method = contact_method.value

for field, value in profile_data.items():
setattr(profile, field, value)
profile.save()
Expand Down
7 changes: 7 additions & 0 deletions profiles/tests/test_gql_create_my_profile_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def test_normal_user_can_create_profile(
createMyProfile(
input: {
profile: {
language: FINNISH,
contactMethod: EMAIL,
nickname: "${nickname}",
addEmails: [
{emailType: ${email_type}, email:"${email}", primary: ${primary}}
Expand All @@ -30,6 +32,8 @@ def test_normal_user_can_create_profile(
}
) {
profile {
language,
contactMethod,
nickname,
emails {
edges {
Expand All @@ -53,6 +57,8 @@ def test_normal_user_can_create_profile(
expected_data = {
"createMyProfile": {
"profile": {
"language": "FINNISH",
"contactMethod": "EMAIL",
"nickname": profile_data["nickname"],
"emails": {
"edges": [
Expand All @@ -79,6 +85,7 @@ def test_normal_user_can_create_profile(
ssn=ssn,
)
executed = user_gql_client.execute(mutation)
assert "errors" not in executed
assert executed["data"] == expected_data


Expand Down
6 changes: 6 additions & 0 deletions profiles/tests/test_gql_create_profile_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def test_staff_user_can_create_a_profile(
input: {
serviceType: GODCHILDREN_OF_CULTURE,
profile: {
language: FINNISH,
contactMethod: EMAIL,
firstName: "${first_name}",
lastName: "${last_name}",
${email_input}
Expand All @@ -42,6 +44,8 @@ def test_staff_user_can_create_a_profile(
}
) {
profile {
language,
contactMethod,
firstName
lastName
phones {
Expand Down Expand Up @@ -97,6 +101,8 @@ def test_staff_user_can_create_a_profile(
expected_data = {
"createProfile": {
"profile": {
"language": "FINNISH",
"contactMethod": "EMAIL",
"firstName": "John",
"lastName": "Doe",
"phones": {
Expand Down
75 changes: 75 additions & 0 deletions profiles/tests/test_gql_update_my_profile_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from utils import keycloak

from ..helpers import to_global_id
from ..schema import ContactMethod, Language
from .factories import (
AddressFactory,
EmailFactory,
Expand Down Expand Up @@ -155,6 +156,80 @@ def test_update_profile_without_email(user_gql_client, profile_data):
assert executed["data"] == expected_data


@pytest.mark.parametrize("lang", Language, ids=repr)
def test_update_profile_language(user_gql_client, lang):
profile = ProfileWithPrimaryEmailFactory(user=user_gql_client.user)
t = Template(
"""
mutation {
updateMyProfile(
input: {
profile: {
language: ${language}
}
}
) {
profile {
language
}
}
}
"""
)
expected_data = {
"updateMyProfile": {
"profile": {
"language": lang.name,
}
}
}
mutation = t.substitute(language=lang.name)

executed = user_gql_client.execute(mutation)

assert "errors" not in executed
assert dict(executed["data"]) == expected_data
profile.refresh_from_db()
assert profile.language == lang.value


@pytest.mark.parametrize("contact_method", ContactMethod, ids=repr)
def test_update_profile_contact_method(user_gql_client, contact_method):
profile = ProfileWithPrimaryEmailFactory(user=user_gql_client.user)
t = Template(
"""
mutation {
updateMyProfile(
input: {
profile: {
contactMethod: ${contact_method}
}
}
) {
profile {
contactMethod
}
}
}
"""
)
expected_data = {
"updateMyProfile": {
"profile": {
"contactMethod": contact_method.name,
}
}
}
mutation = t.substitute(contact_method=contact_method.name)

executed = user_gql_client.execute(mutation)

assert "errors" not in executed
assert dict(executed["data"]) == expected_data
profile.refresh_from_db()
assert profile.contact_method == contact_method.value


def test_add_email(user_gql_client, email_data):
profile = ProfileWithPrimaryEmailFactory(user=user_gql_client.user)
email = profile.emails.first()
Expand Down

0 comments on commit c4e795b

Please sign in to comment.