fix messages errors for user registrations
This commit is contained in:
parent
d51b8b2d82
commit
8afc5d72ad
40
lib/pleroma/ecto_helper.ex
Normal file
40
lib/pleroma/ecto_helper.ex
Normal file
@ -0,0 +1,40 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.EctoHelper do
|
||||
@moduledoc false
|
||||
|
||||
@spec pretty_errors(map(), map()) :: map()
|
||||
def pretty_errors(errors, mapping_fields \\ %{}) do
|
||||
Enum.reduce(errors, %{}, fn {field, _} = error, acc ->
|
||||
field_errors = Map.get(acc, field, []) ++ [do_prettify(error, mapping_fields)]
|
||||
Map.merge(acc, %{field => field_errors})
|
||||
end)
|
||||
end
|
||||
|
||||
defp field_name(field_name, mapping_fields) do
|
||||
Map.get(mapping_fields, field_name, Phoenix.Naming.humanize(field_name))
|
||||
end
|
||||
|
||||
defp do_prettify({field_name, msg}, mapping_fields) when is_binary(msg) do
|
||||
field_name(field_name, mapping_fields) <> " " <> msg
|
||||
end
|
||||
|
||||
defp do_prettify({field_name, {msg, variables}}, mapping_fields) do
|
||||
compound_message = do_interpolate(msg, variables)
|
||||
do_prettify({field_name, compound_message}, mapping_fields)
|
||||
end
|
||||
|
||||
defp do_interpolate(string, [{name, value} | rest]) do
|
||||
n = Atom.to_string(name)
|
||||
msg = String.replace(string, "%{#{n}}", do_to_string(value))
|
||||
do_interpolate(msg, rest)
|
||||
end
|
||||
|
||||
defp do_interpolate(string, []), do: string
|
||||
|
||||
defp do_to_string(value) when is_integer(value), do: Integer.to_string(value)
|
||||
defp do_to_string(value) when is_bitstring(value), do: value
|
||||
defp do_to_string(value) when is_atom(value), do: Atom.to_string(value)
|
||||
end
|
@ -49,11 +49,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||
{:ok, user}
|
||||
|
||||
{:error, changeset} ->
|
||||
errors =
|
||||
changeset
|
||||
|> Ecto.Changeset.traverse_errors(fn {msg, _opts} -> msg end)
|
||||
|
||||
{:error, errors}
|
||||
{:error, Pleroma.EctoHelper.pretty_errors(changeset.errors)}
|
||||
end
|
||||
end
|
||||
|
||||
|
35
test/pleroma/ecto_helper_test.exs
Normal file
35
test/pleroma/ecto_helper_test.exs
Normal file
@ -0,0 +1,35 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.EctoHelperTest do
|
||||
use Pleroma.DataCase
|
||||
|
||||
describe "pretty_errors/2" do
|
||||
test "returns errors messages" do
|
||||
errors = [
|
||||
name:
|
||||
{"should be at least %{count} character(s)",
|
||||
[count: 5, validation: :length, kind: :min, type: :string]},
|
||||
name: {"has invalid format", [validation: :format]}
|
||||
]
|
||||
|
||||
assert Pleroma.EctoHelper.pretty_errors(errors) == %{
|
||||
name: ["Name should be at least 5 character(s)", "Name has invalid format"]
|
||||
}
|
||||
end
|
||||
|
||||
test "returns errors messages with mapping field" do
|
||||
errors = [
|
||||
name:
|
||||
{"should be at least %{count} character(s)",
|
||||
[count: 5, validation: :length, kind: :min, type: :string]},
|
||||
name: {"has invalid format", [validation: :format]}
|
||||
]
|
||||
|
||||
assert Pleroma.EctoHelper.pretty_errors(errors, %{name: "Username"}) == %{
|
||||
name: ["Username should be at least 5 character(s)", "Username has invalid format"]
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
@ -83,7 +83,7 @@ defmodule Pleroma.Web.MastodonAPI.RegistrationUserTest do
|
||||
|
||||
assert response == %{
|
||||
"error" => "Please review the submission",
|
||||
"fields" => %{"email" => ["Invalid email"]},
|
||||
"fields" => %{"email" => ["Email Invalid email"]},
|
||||
"identifier" => "review_submission"
|
||||
}
|
||||
|
||||
@ -247,7 +247,7 @@ defmodule Pleroma.Web.MastodonAPI.RegistrationUserTest do
|
||||
|
||||
assert res == %{
|
||||
"error" => "Please review the submission",
|
||||
"fields" => %{"email" => ["has already been taken"]},
|
||||
"fields" => %{"email" => ["Email has already been taken"]},
|
||||
"identifier" => "review_submission"
|
||||
}
|
||||
end
|
||||
@ -314,7 +314,7 @@ defmodule Pleroma.Web.MastodonAPI.RegistrationUserTest do
|
||||
|
||||
assert res == %{
|
||||
"error" => "Please review the submission",
|
||||
"fields" => %{"email" => ["can't be blank"]},
|
||||
"fields" => %{"email" => ["Email can't be blank"]},
|
||||
"identifier" => "review_submission"
|
||||
}
|
||||
end
|
||||
@ -652,6 +652,32 @@ defmodule Pleroma.Web.MastodonAPI.RegistrationUserTest do
|
||||
|> post("/api/v1/accounts", params)
|
||||
|> json_response_and_validate_schema(:bad_request)
|
||||
end
|
||||
|
||||
test "returns an error if captcha is invalid and invite token invalid", %{conn: conn} do
|
||||
clear_config([:instance, :registrations_open], false)
|
||||
|
||||
# invite = insert(:user_invite_token, %{invite_type: "one_time"})
|
||||
|
||||
params = %{
|
||||
username: "lain",
|
||||
email: "lain@example.org",
|
||||
password: "PlzDontHackLain",
|
||||
agreement: true,
|
||||
token: "invite.token",
|
||||
captcha_solution: "cofe",
|
||||
captcha_token: "cofe",
|
||||
captcha_answer_data: "cofe"
|
||||
}
|
||||
|
||||
assert %{
|
||||
"error" => "Please review the submission",
|
||||
"fields" => %{"captcha" => ["Invalid answer data"]},
|
||||
"identifier" => "review_submission"
|
||||
} ==
|
||||
conn
|
||||
|> post("/api/v1/accounts", params)
|
||||
|> json_response_and_validate_schema(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
describe "api spec errors" do
|
||||
|
@ -421,8 +421,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
||||
{:error, error} = TwitterAPI.register_user(data)
|
||||
|
||||
assert error == %{
|
||||
password: ["can't be blank"],
|
||||
password_confirmation: ["can't be blank"]
|
||||
password: ["Password can't be blank"],
|
||||
password_confirmation: ["Password confirmation can't be blank"]
|
||||
}
|
||||
|
||||
refute User.get_cached_by_nickname("lain")
|
||||
|
Loading…
Reference in New Issue
Block a user