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