Skip to content

Commit 982a747

Browse files
committed
Only return invalid user id when it's the only error
1 parent c1cbf1f commit 982a747

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.4.8 (TBA)
4+
5+
### Enhancements
6+
7+
* [`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
8+
39
## v0.4.7 (2020-04-22)
410

511
Now support Phoenix 1.5 and requires Pow `~> 1.0.19` and Elixir 1.7.

lib/pow_assent/ecto/user_identities/context.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ defmodule PowAssent.Ecto.UserIdentities.Context do
210210
|> user_mod.user_identity_changeset(params, user_params, user_id_params)
211211
|> Context.do_insert(config)
212212
|> user_user_identity_bound_different_user_error()
213-
|> invalid_user_id_error(config)
213+
|> maybe_invalid_user_id_error(config)
214214
end
215215

216216
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
228228
end)
229229
end
230230

231-
defp invalid_user_id_error({:error, %{errors: errors} = changeset}, config) do
231+
defp maybe_invalid_user_id_error({:error, %{errors: errors} = changeset}, config) do
232232
user_mod = user!(config)
233233
user_id_field = user_mod.pow_user_id_field()
234234

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

242242
@doc """
243243
Deletes a user identity for the provider and user.

test/pow_assent/ecto/user_identities/context_test.exs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,23 @@ defmodule PowAssent.Ecto.UserIdentities.ContextTest do
166166
end
167167

168168
test "when user id field is missing" do
169-
assert {:error, {:invalid_user_id_field, _changeset}} = Context.create_user(@user_identity_params, Map.delete(@user_params, :email), nil, @config)
169+
user_params = Map.delete(@user_params, :email)
170+
171+
assert {:error, {:invalid_user_id_field, _changeset}} = Context.create_user(@user_identity_params, user_params, nil, @config)
172+
173+
assert {:error, %Changeset{}} = Context.create_user(@user_identity_params, Map.delete(user_params, :name), nil, @config)
174+
end
175+
176+
test "when user id field is invalid" do
177+
user_params = Map.put(@user_params, :email, "invalid")
178+
179+
assert {:error, {:invalid_user_id_field, _changeset}} = Context.create_user(@user_identity_params, user_params, nil, @config)
180+
181+
assert {:error, %Changeset{}} = Context.create_user(@user_identity_params, Map.delete(user_params, :name), nil, @config)
182+
end
183+
184+
test "when user id field is invalid but also has other errors" do
185+
assert {:error, _changeset} = Context.create_user(@user_identity_params, Map.put(@user_params, :email, "invalid"), nil, @config)
170186
end
171187
end
172188

0 commit comments

Comments
 (0)