Skip to content

Commit

Permalink
Fix unhandled error in AccountRegister (saleor#15501)
Browse files Browse the repository at this point in the history
  • Loading branch information
IKarbowiak authored Feb 29, 2024
1 parent 952a07b commit 1373efe
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 60 deletions.
37 changes: 16 additions & 21 deletions saleor/graphql/account/mutations/account/account_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def mutate(cls, root, info: ResolveInfo, **data):
@classmethod
def clean_input(cls, info: ResolveInfo, instance, data, **kwargs):
site = get_site_promise(info.context).get()

if not site.settings.enable_account_confirmation_by_email:
return super().clean_input(info, instance, data, **kwargs)
elif not data.get("redirect_url"):
Expand Down Expand Up @@ -159,9 +158,8 @@ def save(cls, info: ResolveInfo, user, cleaned_input):

with traced_atomic_transaction():
user.is_confirmed = False
user.save()
if site.settings.enable_account_confirmation_by_email:
user.save()

# Notifications will be deprecated in the future
token = default_token_generator.make_token(user)
notifications.send_account_confirmation(
Expand All @@ -171,25 +169,22 @@ def save(cls, info: ResolveInfo, user, cleaned_input):
channel_slug=cleaned_input["channel"],
token=token,
)
else:
user.save()

if redirect_url:
params = urlencode(
{
"email": user.email,
"token": token or default_token_generator.make_token(user),
}
if redirect_url:
params = urlencode(
{
"email": user.email,
"token": token or default_token_generator.make_token(user),
}
)
redirect_url = prepare_url(params, redirect_url)

cls.call_event(
manager.account_confirmation_requested,
user,
cleaned_input["channel"],
token,
redirect_url,
)
redirect_url = prepare_url(params, redirect_url)

cls.call_event(
manager.account_confirmation_requested,
user,
cleaned_input["channel"],
token,
redirect_url,
)

cls.call_event(manager.customer_created, user)
account_events.customer_account_created_event(user=user)
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,10 @@

ACCOUNT_REGISTER_MUTATION = """
mutation RegisterAccount(
$password: String!,
$email: String!,
$firstName: String,
$lastName: String,
$redirectUrl: String,
$languageCode: LanguageCodeEnum
$metadata: [MetadataInput!],
$channel: String
$input: AccountRegisterInput!
) {
accountRegister(
input: {
password: $password,
email: $email,
firstName: $firstName,
lastName: $lastName,
redirectUrl: $redirectUrl,
languageCode: $languageCode,
metadata: $metadata,
channel: $channel
}
input: $input
) {
errors {
field
Expand Down Expand Up @@ -69,14 +53,16 @@ def test_customer_register(

redirect_url = "http://localhost:3000"
variables = {
"email": email,
"password": "Password",
"redirectUrl": redirect_url,
"firstName": "saleor",
"lastName": "rocks",
"languageCode": "PL",
"metadata": [{"key": "meta", "value": "data"}],
"channel": channel_PLN.slug,
"input": {
"email": email,
"password": "Password",
"redirectUrl": redirect_url,
"firstName": "saleor",
"lastName": "rocks",
"languageCode": "PL",
"metadata": [{"key": "meta", "value": "data"}],
"channel": channel_PLN.slug,
}
}
query = ACCOUNT_REGISTER_MUTATION
mutation_name = "accountRegister"
Expand All @@ -98,8 +84,8 @@ def test_customer_register(
}
assert new_user.metadata == {"meta": "data"}
assert new_user.language_code == "pl"
assert new_user.first_name == variables["firstName"]
assert new_user.last_name == variables["lastName"]
assert new_user.first_name == variables["input"]["firstName"]
assert new_user.last_name == variables["input"]["lastName"]
assert new_user.search_document == generate_user_fields_search_document_value(
new_user
)
Expand Down Expand Up @@ -137,14 +123,16 @@ def test_customer_register_generates_valid_token(
email = "customer@example.com"
redirect_url = "http://localhost:3000"
variables = {
"email": email,
"password": "Password",
"redirectUrl": redirect_url,
"firstName": "saleor",
"lastName": "rocks",
"languageCode": "PL",
"metadata": [{"key": "meta", "value": "data"}],
"channel": channel_PLN.slug,
"input": {
"email": email,
"password": "Password",
"redirectUrl": redirect_url,
"firstName": "saleor",
"lastName": "rocks",
"languageCode": "PL",
"metadata": [{"key": "meta", "value": "data"}],
"channel": channel_PLN.slug,
}
}

# when
Expand All @@ -168,7 +156,7 @@ def test_customer_register_disabled_email_confirmation(
site_settings.save(update_fields=["enable_account_confirmation_by_email"])

email = "customer@example.com"
variables = {"email": email, "password": "Password"}
variables = {"input": {"email": email, "password": "Password"}}

# when
response = api_client.post_graphql(ACCOUNT_REGISTER_MUTATION, variables)
Expand All @@ -189,7 +177,7 @@ def test_customer_register_no_redirect_url(mocked_notify, api_client, site_setti
site_settings.enable_account_confirmation_by_email = True
site_settings.save(update_fields=["enable_account_confirmation_by_email"])

variables = {"email": "customer@example.com", "password": "Password"}
variables = {"input": {"email": "customer@example.com", "password": "Password"}}

# when
response = api_client.post_graphql(ACCOUNT_REGISTER_MUTATION, variables)
Expand All @@ -207,7 +195,7 @@ def test_customer_register_upper_case_email(api_client, site_settings):
site_settings.save(update_fields=["enable_account_confirmation_by_email"])

email = "CUSTOMER@example.com"
variables = {"email": email, "password": "Password"}
variables = {"input": {"email": email, "password": "Password"}}

# when
response = api_client.post_graphql(ACCOUNT_REGISTER_MUTATION, variables)
Expand All @@ -217,3 +205,37 @@ def test_customer_register_upper_case_email(api_client, site_settings):
data = content["data"]["accountRegister"]
assert not data["errors"]
assert data["user"]["email"].lower()


@patch("saleor.plugins.manager.PluginsManager.notify")
def test_customer_register_no_channel_email_confirmation_unset(
mocked_notify, api_client, channel_PLN, site_settings
):
# given
site_settings.enable_account_confirmation_by_email = False
site_settings.save(update_fields=["enable_account_confirmation_by_email"])

email = "customer@example.com"
redirect_url = "http://localhost:3000"
variables = {
"input": {
"email": email,
"password": "Password",
"redirectUrl": redirect_url,
"firstName": "saleor",
"lastName": "rocks",
"languageCode": "PL",
"metadata": [{"key": "meta", "value": "data"}],
}
}

# when
response = api_client.post_graphql(ACCOUNT_REGISTER_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["accountRegister"]

# then
data = content["data"]["accountRegister"]
assert not data["errors"]
assert data["user"]["email"].lower()
mocked_notify.assert_not_called()

0 comments on commit 1373efe

Please sign in to comment.