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