Merge develop
[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 user = insert(:user)
71 note_object = insert(:note)
72 _note = insert(:note_activity, %{note: note_object})
73 object = insert(:note, %{data: %{"inReplyTo" => note_object.data["id"]}})
74 answer = insert(:note_activity, %{note: object})
75
76 Repo.update!(
77 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
78 )
79
80 expected = """
81 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
82 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
83 <id>#{object.data["id"]}</id>
84 <title>New note by #{user.nickname}</title>
85 <content type="html">#{object.data["content"]}</content>
86 <published>#{object.data["published"]}</published>
87 <updated>#{object.data["published"]}</updated>
88 <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>
89 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
90 <summary>2hu</summary>
91 <link type="application/atom+xml" href="#{object.data["id"]}" rel="self" />
92 <link type="text/html" href="#{object.data["id"]}" rel="alternate" />
93 <category term="2hu"/>
94 <thr:in-reply-to ref="#{note_object.data["id"]}" href="someurl" />
95 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
96 <link name="2hu" rel="emoji" href="corndog.png" />
97 """
98
99 tuple = ActivityRepresenter.to_simple_form(answer, user)
100
101 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
102
103 assert clean(res) == clean(expected)
104 end
105
106 test "an announce activity" do
107 note = insert(:note_activity)
108 user = insert(:user)
109 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
110
111 {:ok, announce, _object} = ActivityPub.announce(user, object)
112
113 announce = Activity.get_by_id(announce.id)
114
115 note_user = User.get_cached_by_ap_id(note.data["actor"])
116 note = Activity.get_by_id(note.id)
117
118 note_xml =
119 ActivityRepresenter.to_simple_form(note, note_user, true)
120 |> :xmerl.export_simple_content(:xmerl_xml)
121 |> to_string
122
123 expected = """
124 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
125 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
126 <id>#{announce.data["id"]}</id>
127 <title>#{user.nickname} repeated a notice</title>
128 <content type="html">RT #{note.data["object"]["content"]}</content>
129 <published>#{announce.data["published"]}</published>
130 <updated>#{announce.data["published"]}</updated>
131 <ostatus:conversation ref="#{announce.data["context"]}">#{announce.data["context"]}</ostatus:conversation>
132 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
133 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
134 <activity:object>
135 #{note_xml}
136 </activity:object>
137 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
138 note.data["actor"]
139 }"/>
140 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
141 """
142
143 announce_xml =
144 ActivityRepresenter.to_simple_form(announce, user)
145 |> :xmerl.export_simple_content(:xmerl_xml)
146 |> to_string
147
148 assert clean(expected) == clean(announce_xml)
149 end
150
151 test "a like activity" do
152 note = insert(:note)
153 user = insert(:user)
154 {:ok, like, _note} = ActivityPub.like(user, note)
155
156 tuple = ActivityRepresenter.to_simple_form(like, user)
157 refute is_nil(tuple)
158
159 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
160
161 expected = """
162 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
163 <id>#{like.data["id"]}</id>
164 <title>New favorite by #{user.nickname}</title>
165 <content type="html">#{user.nickname} favorited something</content>
166 <published>#{like.data["published"]}</published>
167 <updated>#{like.data["published"]}</updated>
168 <activity:object>
169 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
170 <id>#{note.data["id"]}</id>
171 </activity:object>
172 <ostatus:conversation ref="#{like.data["context"]}">#{like.data["context"]}</ostatus:conversation>
173 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
174 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
175 <thr:in-reply-to ref="#{note.data["id"]}" />
176 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
177 note.data["actor"]
178 }"/>
179 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
180 """
181
182 assert clean(res) == clean(expected)
183 end
184
185 test "a follow activity" do
186 follower = insert(:user)
187 followed = insert(:user)
188
189 {:ok, activity} =
190 ActivityPub.insert(%{
191 "type" => "Follow",
192 "actor" => follower.ap_id,
193 "object" => followed.ap_id,
194 "to" => [followed.ap_id]
195 })
196
197 tuple = ActivityRepresenter.to_simple_form(activity, follower)
198
199 refute is_nil(tuple)
200
201 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
202
203 expected = """
204 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
205 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
206 <id>#{activity.data["id"]}</id>
207 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
208 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
209 <published>#{activity.data["published"]}</published>
210 <updated>#{activity.data["published"]}</updated>
211 <activity:object>
212 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
213 <id>#{activity.data["object"]}</id>
214 <uri>#{activity.data["object"]}</uri>
215 </activity:object>
216 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
217 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
218 activity.data["object"]
219 }"/>
220 """
221
222 assert clean(res) == clean(expected)
223 end
224
225 test "an unfollow activity" do
226 follower = insert(:user)
227 followed = insert(:user)
228 {:ok, _activity} = ActivityPub.follow(follower, followed)
229 {:ok, activity} = ActivityPub.unfollow(follower, followed)
230
231 tuple = ActivityRepresenter.to_simple_form(activity, follower)
232
233 refute is_nil(tuple)
234
235 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
236
237 expected = """
238 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
239 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
240 <id>#{activity.data["id"]}</id>
241 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
242 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
243 <published>#{activity.data["published"]}</published>
244 <updated>#{activity.data["published"]}</updated>
245 <activity:object>
246 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
247 <id>#{followed.ap_id}</id>
248 <uri>#{followed.ap_id}</uri>
249 </activity:object>
250 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
251 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
252 followed.ap_id
253 }"/>
254 """
255
256 assert clean(res) == clean(expected)
257 end
258
259 test "a delete" do
260 user = insert(:user)
261
262 activity = %Activity{
263 data: %{
264 "id" => "ap_id",
265 "type" => "Delete",
266 "actor" => user.ap_id,
267 "object" => "some_id",
268 "published" => "2017-06-18T12:00:18+00:00"
269 }
270 }
271
272 tuple = ActivityRepresenter.to_simple_form(activity, nil)
273
274 refute is_nil(tuple)
275
276 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
277
278 expected = """
279 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
280 <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
281 <id>#{activity.data["object"]}</id>
282 <title>An object was deleted</title>
283 <content type="html">An object was deleted</content>
284 <published>#{activity.data["published"]}</published>
285 <updated>#{activity.data["published"]}</updated>
286 """
287
288 assert clean(res) == clean(expected)
289 end
290
291 test "an unknown activity" do
292 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
293 assert is_nil(tuple)
294 end
295
296 defp clean(string) do
297 String.replace(string, ~r/\s/, "")
298 end
299 end