Fix queue name
[akkoma] / test / web / ostatus / activity_representer_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Web.OStatus.ActivityRepresenter
9 alias Pleroma.{User, Activity, Object}
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.OStatus
12
13 import Pleroma.Factory
14 import Tesla.Mock
15
16 setup do
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 test "an external note activity" do
22 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
23 {:ok, [activity]} = OStatus.handle_incoming(incoming)
24
25 user = User.get_cached_by_ap_id(activity.data["actor"])
26
27 tuple = ActivityRepresenter.to_simple_form(activity, user)
28
29 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
30
31 assert String.contains?(
32 res,
33 ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>}
34 )
35 end
36
37 test "a note activity" do
38 note_activity = insert(:note_activity)
39
40 user = User.get_cached_by_ap_id(note_activity.data["actor"])
41
42 expected = """
43 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
44 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
45 <id>#{note_activity.data["object"]["id"]}</id>
46 <title>New note by #{user.nickname}</title>
47 <content type="html">#{note_activity.data["object"]["content"]}</content>
48 <published>#{note_activity.data["object"]["published"]}</published>
49 <updated>#{note_activity.data["object"]["published"]}</updated>
50 <ostatus:conversation ref="#{note_activity.data["context"]}">#{note_activity.data["context"]}</ostatus:conversation>
51 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
52 <summary>#{note_activity.data["object"]["summary"]}</summary>
53 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
54 <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
55 <category term="2hu"/>
56 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
57 <link name="2hu" rel="emoji" href="corndog.png" />
58 """
59
60 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
61
62 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
63
64 assert clean(res) == clean(expected)
65 end
66
67 test "a reply note" do
68 note = insert(:note_activity)
69 answer = insert(:note_activity)
70 object = answer.data["object"]
71 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
72
73 data = %{answer.data | "object" => object}
74 answer = %{answer | data: data}
75
76 note_object = Object.get_by_ap_id(note.data["object"]["id"])
77
78 Repo.update!(
79 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
80 )
81
82 user = User.get_cached_by_ap_id(answer.data["actor"])
83
84 expected = """
85 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
86 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
87 <id>#{answer.data["object"]["id"]}</id>
88 <title>New note by #{user.nickname}</title>
89 <content type="html">#{answer.data["object"]["content"]}</content>
90 <published>#{answer.data["object"]["published"]}</published>
91 <updated>#{answer.data["object"]["published"]}</updated>
92 <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>
93 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
94 <summary>2hu</summary>
95 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
96 <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" />
97 <category term="2hu"/>
98 <thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" />
99 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
100 <link name="2hu" rel="emoji" href="corndog.png" />
101 """
102
103 tuple = ActivityRepresenter.to_simple_form(answer, user)
104
105 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
106
107 assert clean(res) == clean(expected)
108 end
109
110 test "an announce activity" do
111 note = insert(:note_activity)
112 user = insert(:user)
113 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
114
115 {:ok, announce, _object} = ActivityPub.announce(user, object)
116
117 announce = Repo.get(Activity, announce.id)
118
119 note_user = User.get_cached_by_ap_id(note.data["actor"])
120 note = Repo.get(Activity, note.id)
121
122 note_xml =
123 ActivityRepresenter.to_simple_form(note, note_user, true)
124 |> :xmerl.export_simple_content(:xmerl_xml)
125 |> to_string
126
127 expected = """
128 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
129 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
130 <id>#{announce.data["id"]}</id>
131 <title>#{user.nickname} repeated a notice</title>
132 <content type="html">RT #{note.data["object"]["content"]}</content>
133 <published>#{announce.data["published"]}</published>
134 <updated>#{announce.data["published"]}</updated>
135 <ostatus:conversation ref="#{announce.data["context"]}">#{announce.data["context"]}</ostatus:conversation>
136 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
137 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
138 <activity:object>
139 #{note_xml}
140 </activity:object>
141 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
142 note.data["actor"]
143 }"/>
144 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
145 """
146
147 announce_xml =
148 ActivityRepresenter.to_simple_form(announce, user)
149 |> :xmerl.export_simple_content(:xmerl_xml)
150 |> to_string
151
152 assert clean(expected) == clean(announce_xml)
153 end
154
155 test "a like activity" do
156 note = insert(:note)
157 user = insert(:user)
158 {:ok, like, _note} = ActivityPub.like(user, note)
159
160 tuple = ActivityRepresenter.to_simple_form(like, user)
161 refute is_nil(tuple)
162
163 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
164
165 expected = """
166 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
167 <id>#{like.data["id"]}</id>
168 <title>New favorite by #{user.nickname}</title>
169 <content type="html">#{user.nickname} favorited something</content>
170 <published>#{like.data["published"]}</published>
171 <updated>#{like.data["published"]}</updated>
172 <activity:object>
173 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
174 <id>#{note.data["id"]}</id>
175 </activity:object>
176 <ostatus:conversation ref="#{like.data["context"]}">#{like.data["context"]}</ostatus:conversation>
177 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
178 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
179 <thr:in-reply-to ref="#{note.data["id"]}" />
180 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
181 note.data["actor"]
182 }"/>
183 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
184 """
185
186 assert clean(res) == clean(expected)
187 end
188
189 test "a follow activity" do
190 follower = insert(:user)
191 followed = insert(:user)
192
193 {:ok, activity} =
194 ActivityPub.insert(%{
195 "type" => "Follow",
196 "actor" => follower.ap_id,
197 "object" => followed.ap_id,
198 "to" => [followed.ap_id]
199 })
200
201 tuple = ActivityRepresenter.to_simple_form(activity, follower)
202
203 refute is_nil(tuple)
204
205 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
206
207 expected = """
208 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
209 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
210 <id>#{activity.data["id"]}</id>
211 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
212 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
213 <published>#{activity.data["published"]}</published>
214 <updated>#{activity.data["published"]}</updated>
215 <activity:object>
216 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
217 <id>#{activity.data["object"]}</id>
218 <uri>#{activity.data["object"]}</uri>
219 </activity:object>
220 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
221 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
222 activity.data["object"]
223 }"/>
224 """
225
226 assert clean(res) == clean(expected)
227 end
228
229 test "an unfollow activity" do
230 follower = insert(:user)
231 followed = insert(:user)
232 {:ok, _activity} = ActivityPub.follow(follower, followed)
233 {:ok, activity} = ActivityPub.unfollow(follower, followed)
234
235 tuple = ActivityRepresenter.to_simple_form(activity, follower)
236
237 refute is_nil(tuple)
238
239 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
240
241 expected = """
242 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
243 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
244 <id>#{activity.data["id"]}</id>
245 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
246 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
247 <published>#{activity.data["published"]}</published>
248 <updated>#{activity.data["published"]}</updated>
249 <activity:object>
250 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
251 <id>#{followed.ap_id}</id>
252 <uri>#{followed.ap_id}</uri>
253 </activity:object>
254 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
255 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
256 followed.ap_id
257 }"/>
258 """
259
260 assert clean(res) == clean(expected)
261 end
262
263 test "a delete" do
264 user = insert(:user)
265
266 activity = %Activity{
267 data: %{
268 "id" => "ap_id",
269 "type" => "Delete",
270 "actor" => user.ap_id,
271 "object" => "some_id",
272 "published" => "2017-06-18T12:00:18+00:00"
273 }
274 }
275
276 tuple = ActivityRepresenter.to_simple_form(activity, nil)
277
278 refute is_nil(tuple)
279
280 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
281
282 expected = """
283 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
284 <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
285 <id>#{activity.data["object"]}</id>
286 <title>An object was deleted</title>
287 <content type="html">An object was deleted</content>
288 <published>#{activity.data["published"]}</published>
289 <updated>#{activity.data["published"]}</updated>
290 """
291
292 assert clean(res) == clean(expected)
293 end
294
295 test "an unknown activity" do
296 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
297 assert is_nil(tuple)
298 end
299
300 defp clean(string) do
301 String.replace(string, ~r/\s/, "")
302 end
303 end