From 3d1ede6900a5f1dbd57dbffce97ccb2e433c7e7a Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sat, 11 Jun 2022 14:08:54 +0100 Subject: [PATCH] fix emoji tests --- config/test.exs | 3 +- lib/pleroma/http.ex | 1 - .../emoji_react_validator.ex | 27 ++++++++- lib/pleroma/web/common_api.ex | 2 +- mix.exs | 2 +- test/fixtures/custom-emoji-reaction.json | 28 +++++++++ test/fixtures/misskey-like.json | 17 +++++- test/pleroma/emoji_test.exs | 6 +- test/pleroma/http_test.exs | 1 + .../emoji_react_handling_test.exs | 60 ++++++++++++++++++- .../web/activity_pub/pipeline_test.exs | 1 + .../emoji_react_handling_test.exs | 41 ++++++++++++- .../transmogrifier/like_handling_test.exs | 2 +- 13 files changed, 176 insertions(+), 15 deletions(-) create mode 100644 test/fixtures/custom-emoji-reaction.json diff --git a/config/test.exs b/config/test.exs index f5eb6e5c2..908b67f9d 100644 --- a/config/test.exs +++ b/config/test.exs @@ -48,7 +48,8 @@ config :pleroma, Pleroma.Repo, database: "pleroma_test", hostname: System.get_env("DB_HOST") || "localhost", pool: Ecto.Adapters.SQL.Sandbox, - pool_size: 50 + pool_size: 50, + queue_target: 5000 config :pleroma, :dangerzone, override_repo_pool_size: true diff --git a/lib/pleroma/http.ex b/lib/pleroma/http.ex index d41061538..cdb3baab4 100644 --- a/lib/pleroma/http.ex +++ b/lib/pleroma/http.ex @@ -69,7 +69,6 @@ defmodule Pleroma.HTTP do adapter = Application.get_env(:tesla, :adapter) client = Tesla.client(adapter_middlewares(adapter), adapter) - maybe_limit( fn -> request(client, request) diff --git a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex index 4f2c3ea79..4baddf638 100644 --- a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do use Ecto.Schema + alias Pleroma.Emoji alias Pleroma.Object alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes @@ -77,6 +78,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do cond do Pleroma.Emoji.is_unicode_emoji?(emoji) -> data + |> CommonFixes.fix_activity_context(object) + |> CommonFixes.fix_object_action_recipients(object) Pleroma.Emoji.is_unicode_emoji?(new_emoji) -> data |> Map.put("content", new_emoji) @@ -86,12 +89,15 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do end end + defp matches_shortcode?(nil), do: false + defp matches_shortcode?(s), do: Regex.match?(@emoji_regex, s) + defp fix_emoji_qualification(data), do: data defp validate_emoji(cng) do content = get_field(cng, :content) IO.inspect(Pleroma.Emoji.is_unicode_emoji?(content)) - if Pleroma.Emoji.is_unicode_emoji?(content) || Regex.match?(@emoji_regex, content) do + if Pleroma.Emoji.is_unicode_emoji?(content) || matches_shortcode?(content) do cng else cng @@ -99,6 +105,24 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do end end + defp maybe_validate_tag_presence(cng) do + content = get_field(cng, :content) + + if Emoji.is_unicode_emoji?(content) do + cng + else + tag = get_field(cng, :tag) + emoji_name = Emoji.stripped_name(content) + case tag do + [%{name: ^emoji_name, type: "Emoji", icon: %{url: _}}] -> + cng + _ -> + cng + |> add_error(:tag, "does not contain an Emoji tag") + end + end + end + defp validate_data(data_cng) do data_cng |> validate_inclusion(:type, ["EmojiReact"]) @@ -106,5 +130,6 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do |> validate_actor_presence() |> validate_object_presence() |> validate_emoji() + |> maybe_validate_tag_presence() end end diff --git a/lib/pleroma/web/common_api.ex b/lib/pleroma/web/common_api.ex index 62ab6b69c..a8ef0164d 100644 --- a/lib/pleroma/web/common_api.ex +++ b/lib/pleroma/web/common_api.ex @@ -289,7 +289,7 @@ defmodule Pleroma.Web.CommonAPI do {:ok, activity, _} <- Pipeline.common_pipeline(undo, local: true) do {:ok, activity} else - _ -> + e -> {:error, dgettext("errors", "Could not remove reaction emoji")} end end diff --git a/mix.exs b/mix.exs index 8dd7a8cc5..6580bd03a 100644 --- a/mix.exs +++ b/mix.exs @@ -137,7 +137,7 @@ defmodule Pleroma.Mixfile do {:calendar, "~> 1.0"}, {:cachex, "~> 3.2"}, {:poison, "~> 3.0", override: true}, - {:tesla, "~> 1.4.0", override: true}, + {:tesla, "~> 1.4.4", override: true}, {:castore, "~> 0.1"}, {:cowlib, "~> 2.9", override: true}, {:gun, "~> 2.0.0-rc.1", override: true}, diff --git a/test/fixtures/custom-emoji-reaction.json b/test/fixtures/custom-emoji-reaction.json new file mode 100644 index 000000000..003de0511 --- /dev/null +++ b/test/fixtures/custom-emoji-reaction.json @@ -0,0 +1,28 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "Hashtag": "as:Hashtag" + } + ], + "type": "Like", + "id": "https://misskey.local.live/likes/917ocsybgp", + "actor": "https://misskey.local.live/users/8x8yep20u2", + "object": "https://pleroma.local.live/objects/89937a53-2692-4631-bb62-770091267391", + "content": ":hanapog:", + "_misskey_reaction": ":hanapog:", + "tag": [ + { + "id": "https://misskey.local.live/emojis/hanapog", + "type": "Emoji", + "name": ":hanapog:", + "updated": "2022-06-07T12:00:05.773Z", + "icon": { + "type": "Image", + "mediaType": "image/png", + "url": "https://misskey.local.live/files/webpublic-8f8a9768-7264-4171-88d6-2356aabeadcd" + } + } + ] +} diff --git a/test/fixtures/misskey-like.json b/test/fixtures/misskey-like.json index 84d56f473..81b297859 100644 --- a/test/fixtures/misskey-like.json +++ b/test/fixtures/misskey-like.json @@ -4,11 +4,24 @@ "https://w3id.org/security/v1", {"Hashtag" : "as:Hashtag"} ], - "_misskey_reaction" : "pudding", + "_misskey_reaction" : ":pudding:", + "content": ":pudding:", "actor": "http://mastodon.example.org/users/admin", "cc" : ["https://testing.pleroma.lol/users/lain"], "id" : "https://misskey.xyz/75149198-2f45-46e4-930a-8b0538297075", "nickname" : "lain", "object" : "https://testing.pleroma.lol/objects/c331bbf7-2eb9-4801-a709-2a6103492a5a", - "type" : "Like" + "type" : "Like", + "tag": [ + { + "id": "https://somewhere/emoji/pudding", + "name": ":pudding:", + "type": "Emoji", + "icon": { + "type": "Image", + "mediaType": "image/png", + "url": "http://somewhere" + } + } + ] } diff --git a/test/pleroma/emoji_test.exs b/test/pleroma/emoji_test.exs index 18063c223..6ccb6ce9c 100644 --- a/test/pleroma/emoji_test.exs +++ b/test/pleroma/emoji_test.exs @@ -11,10 +11,10 @@ defmodule Pleroma.EmojiTest do refute Emoji.is_unicode_emoji?("X") refute Emoji.is_unicode_emoji?("ね") - # Only accept fully-qualified (RGI) emoji + # Accept fully-qualified and unqualified emoji # See http://www.unicode.org/reports/tr51/ - refute Emoji.is_unicode_emoji?("❤") - refute Emoji.is_unicode_emoji?("☂") + assert Emoji.is_unicode_emoji?("❤") + assert Emoji.is_unicode_emoji?("☂") assert Emoji.is_unicode_emoji?("🥺") assert Emoji.is_unicode_emoji?("🤰") diff --git a/test/pleroma/http_test.exs b/test/pleroma/http_test.exs index de359e599..41811185b 100644 --- a/test/pleroma/http_test.exs +++ b/test/pleroma/http_test.exs @@ -9,6 +9,7 @@ defmodule Pleroma.HTTPTest do alias Pleroma.HTTP setup do + clear_config([:http, :send_user_agent], false) mock(fn %{ method: :get, diff --git a/test/pleroma/web/activity_pub/object_validators/emoji_react_handling_test.exs b/test/pleroma/web/activity_pub/object_validators/emoji_react_handling_test.exs index bbdb09c4c..9bb291a38 100644 --- a/test/pleroma/web/activity_pub/object_validators/emoji_react_handling_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/emoji_react_handling_test.exs @@ -38,16 +38,70 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactHandlingTest do assert {:content, {"can't be blank", [validation: :required]}} in cng.errors end - test "it is not valid with a non-emoji content field", %{valid_emoji_react: valid_emoji_react} do + test "it is valid when custom emoji is used", %{valid_emoji_react: valid_emoji_react} do without_emoji_content = valid_emoji_react - |> Map.put("content", "x") + |> Map.put("content", ":hello:") + |> Map.put("tag", [ + %{ + "type" => "Emoji", + "name" => ":hello:", + "icon" => %{"url" => "http://somewhere", "type" => "Image"} + } + ]) + + {:ok, _, _} = ObjectValidator.validate(without_emoji_content, []) + end + + test "it is not valid when custom emoji don't have a matching tag", %{ + valid_emoji_react: valid_emoji_react + } do + without_emoji_content = + valid_emoji_react + |> Map.put("content", ":hello:") + |> Map.put("tag", [ + %{ + "type" => "Emoji", + "name" => ":whoops:", + "icon" => %{"url" => "http://somewhere", "type" => "Image"} + } + ]) {:error, cng} = ObjectValidator.validate(without_emoji_content, []) refute cng.valid? - assert {:content, {"must be a single character emoji", []}} in cng.errors + assert {:tag, {"does not contain an Emoji tag", []}} in cng.errors + end + + test "it is not valid when custom emoji have no tags", %{ + valid_emoji_react: valid_emoji_react + } do + without_emoji_content = + valid_emoji_react + |> Map.put("content", ":hello:") + |> Map.put("tag", []) + + {:error, cng} = ObjectValidator.validate(without_emoji_content, []) + + refute cng.valid? + + assert {:tag, {"does not contain an Emoji tag", []}} in cng.errors + end + + test "it is not valid when custom emoji doesn't match a shortcode format", %{ + valid_emoji_react: valid_emoji_react + } do + without_emoji_content = + valid_emoji_react + |> Map.put("content", "hello") + |> Map.put("tag", []) + + {:error, cng} = ObjectValidator.validate(without_emoji_content, []) + + refute cng.valid? + + assert {:tag, {"does not contain an Emoji tag", []}} in cng.errors end end end diff --git a/test/pleroma/web/activity_pub/pipeline_test.exs b/test/pleroma/web/activity_pub/pipeline_test.exs index 5b65d1014..475de62d9 100644 --- a/test/pleroma/web/activity_pub/pipeline_test.exs +++ b/test/pleroma/web/activity_pub/pipeline_test.exs @@ -28,6 +28,7 @@ defmodule Pleroma.Web.ActivityPub.PipelineTest do SideEffectsMock |> expect(:handle, fn o, m -> {:ok, o, m} end) |> expect(:handle_after_transaction, fn m -> m end) + |> expect(:handle_after_transaction, fn m -> m end) :ok end diff --git a/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs index 9d99df27c..3ca3b25e2 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs @@ -34,7 +34,46 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.EmojiReactHandlingTest do object = Object.get_by_ap_id(data["object"]) assert object.data["reaction_count"] == 1 - assert match?([["👌", _]], object.data["reactions"]) + assert match?([["👌", _, nil]], object.data["reactions"]) + end + + test "it works for incoming custom emoji reactions" do + user = insert(:user) + other_user = insert(:user, local: false) + {:ok, activity} = CommonAPI.post(user, %{status: "hello"}) + + data = + File.read!("test/fixtures/custom-emoji-reaction.json") + |> Jason.decode!() + |> Map.put("object", activity.data["object"]) + |> Map.put("actor", other_user.ap_id) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["actor"] == other_user.ap_id + assert data["type"] == "EmojiReact" + assert data["id"] == "https://misskey.local.live/likes/917ocsybgp" + assert data["object"] == activity.data["object"] + assert data["content"] == ":hanapog:" + + assert data["tag"] == [ + %{ + "id" => "https://misskey.local.live/emojis/hanapog", + "type" => "Emoji", + "name" => "hanapog", + "updated" => "2022-06-07T12:00:05.773Z", + "icon" => %{ + "type" => "Image", + "url" => + "https://misskey.local.live/files/webpublic-8f8a9768-7264-4171-88d6-2356aabeadcd" + } + } + ] + + object = Object.get_by_ap_id(data["object"]) + + assert object.data["reaction_count"] == 1 + assert match?([["hanapog", _, "https://misskey.local.live/files/webpublic-8f8a9768-7264-4171-88d6-2356aabeadcd"]], object.data["reactions"]) end test "it works for incoming unqualified emoji reactions" do diff --git a/test/pleroma/web/activity_pub/transmogrifier/like_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/like_handling_test.exs index c02f66d77..0a258cf0f 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/like_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/like_handling_test.exs @@ -51,7 +51,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.LikeHandlingTest do assert activity_data["type"] == "EmojiReact" assert activity_data["id"] == data["id"] assert activity_data["object"] == activity.data["object"] - assert activity_data["content"] == "🍮" + assert activity_data["content"] == ":pudding:" end test "it works for incoming misskey likes that contain unicode emojis, turning them into EmojiReacts" do