diff options
author | Maksim Pechnikov <parallel588@gmail.com> | 2020-11-05 10:34:25 +0300 |
---|---|---|
committer | Maksim Pechnikov <parallel588@gmail.com> | 2020-11-05 10:34:25 +0300 |
commit | 8afc5d72ad5c458c995d32aa598d654b7c17e2c3 (patch) | |
tree | 496ca5b92a85c2573d352263934c7327dcd49182 | |
parent | d51b8b2d82f2812b4d4e52c30970eb51f57e4326 (diff) | |
download | pleroma-8afc5d72ad5c458c995d32aa598d654b7c17e2c3.tar.gz |
fix messages errors for user registrations
-rw-r--r-- | lib/pleroma/ecto_helper.ex | 40 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 6 | ||||
-rw-r--r-- | test/pleroma/ecto_helper_test.exs | 35 | ||||
-rw-r--r-- | test/pleroma/web/mastodon_api/registration_user_test.exs | 32 | ||||
-rw-r--r-- | test/pleroma/web/twitter_api/twitter_api_test.exs | 4 |
5 files changed, 107 insertions, 10 deletions
diff --git a/lib/pleroma/ecto_helper.ex b/lib/pleroma/ecto_helper.ex new file mode 100644 index 000000000..b174ea41c --- /dev/null +++ b/lib/pleroma/ecto_helper.ex @@ -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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 92b071a81..5c3ce0272 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -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 diff --git a/test/pleroma/ecto_helper_test.exs b/test/pleroma/ecto_helper_test.exs new file mode 100644 index 000000000..40b416f3d --- /dev/null +++ b/test/pleroma/ecto_helper_test.exs @@ -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 diff --git a/test/pleroma/web/mastodon_api/registration_user_test.exs b/test/pleroma/web/mastodon_api/registration_user_test.exs index 086d29832..5121bdafe 100644 --- a/test/pleroma/web/mastodon_api/registration_user_test.exs +++ b/test/pleroma/web/mastodon_api/registration_user_test.exs @@ -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 diff --git a/test/pleroma/web/twitter_api/twitter_api_test.exs b/test/pleroma/web/twitter_api/twitter_api_test.exs index d433b507b..c36c7a691 100644 --- a/test/pleroma/web/twitter_api/twitter_api_test.exs +++ b/test/pleroma/web/twitter_api/twitter_api_test.exs @@ -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") |