diff --git a/CHANGELOG.md b/CHANGELOG.md index e7fc514..d1780ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/pow_assent/ecto/user_identities/context.ex b/lib/pow_assent/ecto/user_identities/context.ex index f59ad17..579a803 100644 --- a/lib/pow_assent/ecto/user_identities/context.ex +++ b/lib/pow_assent/ecto/user_identities/context.ex @@ -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 @@ -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. diff --git a/test/pow_assent/ecto/user_identities/context_test.exs b/test/pow_assent/ecto/user_identities/context_test.exs index 54329ca..aa6fe20 100644 --- a/test/pow_assent/ecto/user_identities/context_test.exs +++ b/test/pow_assent/ecto/user_identities/context_test.exs @@ -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