d310705465fb0811309243bcfbd71e106c5102b4
[anni] / test / pleroma / web / activity_pub / transmogrifier / question_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.Transmogrifier.QuestionHandlingTest do
6   use Pleroma.DataCase
7
8   alias Pleroma.Activity
9   alias Pleroma.Object
10   alias Pleroma.Web.ActivityPub.Transmogrifier
11   alias Pleroma.Web.CommonAPI
12
13   import Pleroma.Factory
14
15   setup_all do
16     Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17     :ok
18   end
19
20   test "Mastodon Question activity" do
21     data = File.read!("test/fixtures/mastodon-question-activity.json") |> Jason.decode!()
22
23     {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
24
25     object = Object.normalize(activity, fetch: false)
26
27     assert object.data["url"] == "https://mastodon.sdf.org/@rinpatch/102070944809637304"
28
29     assert object.data["closed"] == "2019-05-11T09:03:36Z"
30
31     assert object.data["context"] == activity.data["context"]
32
33     assert object.data["context"] ==
34              "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation"
35
36     assert object.data["anyOf"] == []
37
38     assert Enum.sort(object.data["oneOf"]) ==
39              Enum.sort([
40                %{
41                  "name" => "25 char limit is dumb",
42                  "replies" => %{"totalItems" => 0, "type" => "Collection"},
43                  "type" => "Note"
44                },
45                %{
46                  "name" => "Dunno",
47                  "replies" => %{"totalItems" => 0, "type" => "Collection"},
48                  "type" => "Note"
49                },
50                %{
51                  "name" => "Everyone knows that!",
52                  "replies" => %{"totalItems" => 1, "type" => "Collection"},
53                  "type" => "Note"
54                },
55                %{
56                  "name" => "I can't even fit a funny",
57                  "replies" => %{"totalItems" => 1, "type" => "Collection"},
58                  "type" => "Note"
59                }
60              ])
61
62     user = insert(:user)
63
64     {:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id})
65
66     reply_object = Object.normalize(reply_activity, fetch: false)
67
68     assert reply_object.data["context"] == object.data["context"]
69   end
70
71   test "Mastodon Question activity with HTML tags in plaintext" do
72     options = [
73       %{
74         "type" => "Note",
75         "name" => "<input type=\"date\">",
76         "replies" => %{"totalItems" => 0, "type" => "Collection"}
77       },
78       %{
79         "type" => "Note",
80         "name" => "<input type=\"date\"/>",
81         "replies" => %{"totalItems" => 0, "type" => "Collection"}
82       },
83       %{
84         "type" => "Note",
85         "name" => "<input type=\"date\" />",
86         "replies" => %{"totalItems" => 1, "type" => "Collection"}
87       },
88       %{
89         "type" => "Note",
90         "name" => "<input type=\"date\"></input>",
91         "replies" => %{"totalItems" => 1, "type" => "Collection"}
92       }
93     ]
94
95     data =
96       File.read!("test/fixtures/mastodon-question-activity.json")
97       |> Jason.decode!()
98       |> Kernel.put_in(["object", "oneOf"], options)
99
100     {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
101     object = Object.normalize(activity, fetch: false)
102
103     assert Enum.sort(object.data["oneOf"]) == Enum.sort(options)
104   end
105
106   test "Mastodon Question activity with custom emojis" do
107     options = [
108       %{
109         "type" => "Note",
110         "name" => ":blobcat:",
111         "replies" => %{"totalItems" => 0, "type" => "Collection"}
112       },
113       %{
114         "type" => "Note",
115         "name" => ":blobfox:",
116         "replies" => %{"totalItems" => 0, "type" => "Collection"}
117       }
118     ]
119
120     tag = [
121       %{
122         "icon" => %{
123           "type" => "Image",
124           "url" => "https://blob.cat/emoji/custom/blobcats/blobcat.png"
125         },
126         "id" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
127         "name" => ":blobcat:",
128         "type" => "Emoji",
129         "updated" => "1970-01-01T00:00:00Z"
130       },
131       %{
132         "icon" => %{"type" => "Image", "url" => "https://blob.cat/emoji/blobfox/blobfox.png"},
133         "id" => "https://blob.cat/emoji/blobfox/blobfox.png",
134         "name" => ":blobfox:",
135         "type" => "Emoji",
136         "updated" => "1970-01-01T00:00:00Z"
137       }
138     ]
139
140     data =
141       File.read!("test/fixtures/mastodon-question-activity.json")
142       |> Jason.decode!()
143       |> Kernel.put_in(["object", "oneOf"], options)
144       |> Kernel.put_in(["object", "tag"], tag)
145
146     {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
147     object = Object.normalize(activity, fetch: false)
148
149     assert object.data["oneOf"] == options
150
151     assert object.data["emoji"] == %{
152              "blobcat" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
153              "blobfox" => "https://blob.cat/emoji/blobfox/blobfox.png"
154            }
155   end
156
157   test "returns same activity if received a second time" do
158     data = File.read!("test/fixtures/mastodon-question-activity.json") |> Jason.decode!()
159
160     assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
161
162     assert {:ok, ^activity} = Transmogrifier.handle_incoming(data)
163   end
164
165   test "accepts a Question with no content" do
166     data =
167       File.read!("test/fixtures/mastodon-question-activity.json")
168       |> Jason.decode!()
169       |> Kernel.put_in(["object", "content"], "")
170
171     assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
172   end
173 end