Merge branch 'chores/bump-copyright' into 'develop'
[akkoma] / test / pleroma / web / activity_pub / transmogrifier / question_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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["context_id"]
37
38 assert object.data["anyOf"] == []
39
40 assert Enum.sort(object.data["oneOf"]) ==
41 Enum.sort([
42 %{
43 "name" => "25 char limit is dumb",
44 "replies" => %{"totalItems" => 0, "type" => "Collection"},
45 "type" => "Note"
46 },
47 %{
48 "name" => "Dunno",
49 "replies" => %{"totalItems" => 0, "type" => "Collection"},
50 "type" => "Note"
51 },
52 %{
53 "name" => "Everyone knows that!",
54 "replies" => %{"totalItems" => 1, "type" => "Collection"},
55 "type" => "Note"
56 },
57 %{
58 "name" => "I can't even fit a funny",
59 "replies" => %{"totalItems" => 1, "type" => "Collection"},
60 "type" => "Note"
61 }
62 ])
63
64 user = insert(:user)
65
66 {:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id})
67
68 reply_object = Object.normalize(reply_activity, fetch: false)
69
70 assert reply_object.data["context"] == object.data["context"]
71 assert reply_object.data["context_id"] == object.data["context_id"]
72 end
73
74 test "Mastodon Question activity with HTML tags in plaintext" do
75 options = [
76 %{
77 "type" => "Note",
78 "name" => "<input type=\"date\">",
79 "replies" => %{"totalItems" => 0, "type" => "Collection"}
80 },
81 %{
82 "type" => "Note",
83 "name" => "<input type=\"date\"/>",
84 "replies" => %{"totalItems" => 0, "type" => "Collection"}
85 },
86 %{
87 "type" => "Note",
88 "name" => "<input type=\"date\" />",
89 "replies" => %{"totalItems" => 1, "type" => "Collection"}
90 },
91 %{
92 "type" => "Note",
93 "name" => "<input type=\"date\"></input>",
94 "replies" => %{"totalItems" => 1, "type" => "Collection"}
95 }
96 ]
97
98 data =
99 File.read!("test/fixtures/mastodon-question-activity.json")
100 |> Jason.decode!()
101 |> Kernel.put_in(["object", "oneOf"], options)
102
103 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
104 object = Object.normalize(activity, fetch: false)
105
106 assert Enum.sort(object.data["oneOf"]) == Enum.sort(options)
107 end
108
109 test "Mastodon Question activity with custom emojis" do
110 options = [
111 %{
112 "type" => "Note",
113 "name" => ":blobcat:",
114 "replies" => %{"totalItems" => 0, "type" => "Collection"}
115 },
116 %{
117 "type" => "Note",
118 "name" => ":blobfox:",
119 "replies" => %{"totalItems" => 0, "type" => "Collection"}
120 }
121 ]
122
123 tag = [
124 %{
125 "icon" => %{
126 "type" => "Image",
127 "url" => "https://blob.cat/emoji/custom/blobcats/blobcat.png"
128 },
129 "id" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
130 "name" => ":blobcat:",
131 "type" => "Emoji",
132 "updated" => "1970-01-01T00:00:00Z"
133 },
134 %{
135 "icon" => %{"type" => "Image", "url" => "https://blob.cat/emoji/blobfox/blobfox.png"},
136 "id" => "https://blob.cat/emoji/blobfox/blobfox.png",
137 "name" => ":blobfox:",
138 "type" => "Emoji",
139 "updated" => "1970-01-01T00:00:00Z"
140 }
141 ]
142
143 data =
144 File.read!("test/fixtures/mastodon-question-activity.json")
145 |> Jason.decode!()
146 |> Kernel.put_in(["object", "oneOf"], options)
147 |> Kernel.put_in(["object", "tag"], tag)
148
149 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
150 object = Object.normalize(activity, fetch: false)
151
152 assert object.data["oneOf"] == options
153
154 assert object.data["emoji"] == %{
155 "blobcat" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
156 "blobfox" => "https://blob.cat/emoji/blobfox/blobfox.png"
157 }
158 end
159
160 test "returns same activity if received a second time" do
161 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Jason.decode!()
162
163 assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
164
165 assert {:ok, ^activity} = Transmogrifier.handle_incoming(data)
166 end
167
168 test "accepts a Question with no content" do
169 data =
170 File.read!("test/fixtures/mastodon-question-activity.json")
171 |> Jason.decode!()
172 |> Kernel.put_in(["object", "content"], "")
173
174 assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
175 end
176 end