Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only return invalid user id when it's the only error #171

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.4.9 (TBA)

### Enhancements

* [`PowAssent.Ecto.UserIdentities.Context`] Changed `PowAssent.Ecto.UserIdentities.Context.create_user/4` to return `{:invalid_user_id_field, changeset}` when there is only error(s) on the user id field

## v0.4.8 (2020-05-18)

### Enhancements
Expand Down
14 changes: 7 additions & 7 deletions lib/pow_assent/ecto/user_identities/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ defmodule PowAssent.Ecto.UserIdentities.Context do
|> user_mod.user_identity_changeset(params, user_params, user_id_params)
|> Context.do_insert(config)
|> user_user_identity_bound_different_user_error()
|> invalid_user_id_error(config)
|> maybe_invalid_user_id_error(config)
end

defp user_user_identity_bound_different_user_error({:error, %{changes: %{user_identities: [%{errors: errors}]}} = changeset}) do
Expand All @@ -228,16 +228,16 @@ defmodule PowAssent.Ecto.UserIdentities.Context do
end)
end

defp invalid_user_id_error({:error, %{errors: errors} = changeset}, config) do
defp maybe_invalid_user_id_error({:error, %{errors: errors} = changeset}, config) do
user_mod = user!(config)
user_id_field = user_mod.pow_user_id_field()

Enum.find_value(errors, {:error, changeset}, fn
{^user_id_field, _error} -> {:error, {:invalid_user_id_field, changeset}}
_any -> false
end)
case Keyword.keys(errors) do
[^user_id_field] -> {:error, {:invalid_user_id_field, changeset}}
_any -> {:error, changeset}
end
end
defp invalid_user_id_error(any, _config), do: any
defp maybe_invalid_user_id_error(any, _config), do: any

@doc """
Deletes a user identity for the provider and user.
Expand Down
18 changes: 17 additions & 1 deletion test/pow_assent/ecto/user_identities/context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,23 @@ defmodule PowAssent.Ecto.UserIdentities.ContextTest do
end

test "when user id field is missing" do
assert {:error, {:invalid_user_id_field, _changeset}} = Context.create_user(@user_identity_params, Map.delete(@user_params, :email), nil, @config)
user_params = Map.delete(@user_params, :email)

assert {:error, {:invalid_user_id_field, _changeset}} = Context.create_user(@user_identity_params, user_params, nil, @config)

assert {:error, %Changeset{}} = Context.create_user(@user_identity_params, Map.delete(user_params, :name), nil, @config)
end

test "when user id field is invalid" do
user_params = Map.put(@user_params, :email, "invalid")

assert {:error, {:invalid_user_id_field, _changeset}} = Context.create_user(@user_identity_params, user_params, nil, @config)

assert {:error, %Changeset{}} = Context.create_user(@user_identity_params, Map.delete(user_params, :name), nil, @config)
end

test "when user id field is invalid but also has other errors" do
assert {:error, _changeset} = Context.create_user(@user_identity_params, Map.put(@user_params, :email, "invalid"), nil, @config)
end
end

Expand Down