total rebase
[anni] / test / pleroma / web / activity_pub / object_validators / emoji_react_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactHandlingTest do
6   use Pleroma.DataCase, async: true
7
8   alias Pleroma.Web.ActivityPub.Builder
9   alias Pleroma.Web.ActivityPub.ObjectValidator
10   alias Pleroma.Web.CommonAPI
11
12   import Pleroma.Factory
13
14   describe "EmojiReacts" do
15     setup do
16       user = insert(:user)
17       {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
18
19       object = Pleroma.Object.get_by_ap_id(post_activity.data["object"])
20
21       {:ok, valid_emoji_react, []} = Builder.emoji_react(user, object, "👌")
22
23       %{user: user, post_activity: post_activity, valid_emoji_react: valid_emoji_react}
24     end
25
26     test "it validates a valid EmojiReact", %{valid_emoji_react: valid_emoji_react} do
27       assert {:ok, _, _} = ObjectValidator.validate(valid_emoji_react, [])
28     end
29
30     test "it is not valid without a 'content' field", %{valid_emoji_react: valid_emoji_react} do
31       without_content =
32         valid_emoji_react
33         |> Map.delete("content")
34
35       {:error, cng} = ObjectValidator.validate(without_content, [])
36
37       refute cng.valid?
38       assert {:content, {"can't be blank", [validation: :required]}} in cng.errors
39     end
40
41     test "it is valid when custom emoji is used", %{valid_emoji_react: valid_emoji_react} do
42       without_emoji_content =
43         valid_emoji_react
44         |> Map.put("content", ":hello:")
45         |> Map.put("tag", [
46           %{
47             "type" => "Emoji",
48             "name" => ":hello:",
49             "icon" => %{"url" => "http://somewhere", "type" => "Image"}
50           }
51         ])
52
53       {:ok, _, _} = ObjectValidator.validate(without_emoji_content, [])
54     end
55
56     test "it is not valid when custom emoji don't have a matching tag", %{
57       valid_emoji_react: valid_emoji_react
58     } do
59       without_emoji_content =
60         valid_emoji_react
61         |> Map.put("content", ":hello:")
62         |> Map.put("tag", [
63           %{
64             "type" => "Emoji",
65             "name" => ":whoops:",
66             "icon" => %{"url" => "http://somewhere", "type" => "Image"}
67           }
68         ])
69
70       {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
71
72       refute cng.valid?
73
74       assert {:tag, {"does not contain an Emoji tag", []}} in cng.errors
75     end
76
77     test "it is not valid when custom emoji have no tags", %{
78       valid_emoji_react: valid_emoji_react
79     } do
80       without_emoji_content =
81         valid_emoji_react
82         |> Map.put("content", ":hello:")
83         |> Map.put("tag", [])
84
85       {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
86
87       refute cng.valid?
88
89       assert {:tag, {"does not contain an Emoji tag", []}} in cng.errors
90     end
91
92     test "it is not valid when custom emoji doesn't match a shortcode format", %{
93       valid_emoji_react: valid_emoji_react
94     } do
95       without_emoji_content =
96         valid_emoji_react
97         |> Map.put("content", "hello")
98         |> Map.put("tag", [])
99
100       {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
101
102       refute cng.valid?
103
104       assert {:tag, {"does not contain an Emoji tag", []}} in cng.errors
105     end
106   end
107 end