Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma 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
8 import Pleroma.Factory
9
10 test "a note activity" do
11 note_activity = insert(:note_activity)
12
13 user = User.get_cached_by_ap_id(note_activity.data["actor"])
14
15 expected = """
16 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
17 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
18 <id>#{note_activity.data["object"]["id"]}</id>
19 <title>New note by #{user.nickname}</title>
20 <content type="html">#{note_activity.data["object"]["content"]}</content>
21 <published>#{note_activity.data["published"]}</published>
22 <updated>#{note_activity.data["published"]}</updated>
23 <ostatus:conversation>#{note_activity.data["context"]}</ostatus:conversation>
24 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
25 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
26 <category term="2hu"/>
27 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
28 """
29
30 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
31
32 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
33
34 assert clean(res) == clean(expected)
35 end
36
37 test "a reply note" do
38 note = insert(:note_activity)
39 answer = insert(:note_activity)
40 object = answer.data["object"]
41 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
42
43 data = %{answer.data | "object" => object}
44 answer = %{answer | data: data}
45
46 user = User.get_cached_by_ap_id(answer.data["actor"])
47
48 expected = """
49 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
50 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
51 <id>#{answer.data["object"]["id"]}</id>
52 <title>New note by #{user.nickname}</title>
53 <content type="html">#{answer.data["object"]["content"]}</content>
54 <published>#{answer.data["published"]}</published>
55 <updated>#{answer.data["published"]}</updated>
56 <ostatus:conversation>#{answer.data["context"]}</ostatus:conversation>
57 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
58 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
59 <category term="2hu"/>
60 <thr:in-reply-to ref="#{note.data["object"]["id"]}" />
61 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
62 """
63
64 tuple = ActivityRepresenter.to_simple_form(answer, user)
65
66 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
67
68 assert clean(res) == clean(expected)
69 end
70
71 test "an announce activity" do
72 note = insert(:note_activity)
73 user = insert(:user)
74 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
75
76 {:ok, announce, object} = ActivityPub.announce(user, object)
77
78 announce = Repo.get(Activity, announce.id)
79
80 note_user = User.get_cached_by_ap_id(note.data["actor"])
81 note = Repo.get(Activity, note.id)
82 note_xml = ActivityRepresenter.to_simple_form(note, note_user, true)
83 |> :xmerl.export_simple_content(:xmerl_xml)
84 |> to_string
85
86 expected = """
87 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
88 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
89 <id>#{announce.data["id"]}</id>
90 <title>#{user.nickname} repeated a notice</title>
91 <content type="html">RT #{note.data["object"]["content"]}</content>
92 <published>#{announce.data["published"]}</published>
93 <updated>#{announce.data["published"]}</updated>
94 <ostatus:conversation>#{announce.data["context"]}</ostatus:conversation>
95 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
96 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
97 <activity:object>
98 #{note_xml}
99 </activity:object>
100 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
101 """
102
103 announce_xml = ActivityRepresenter.to_simple_form(announce, user)
104 |> :xmerl.export_simple_content(:xmerl_xml)
105 |> to_string
106
107 assert clean(expected) == clean(announce_xml)
108 end
109
110 test "a like activity" do
111 note = insert(:note)
112 user = insert(:user)
113 {:ok, like, _note} = ActivityPub.like(user, note)
114
115 tuple = ActivityRepresenter.to_simple_form(like, user)
116 refute is_nil(tuple)
117
118 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
119
120 expected = """
121 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
122 <id>#{like.data["id"]}</id>
123 <title>New favorite by #{user.nickname}</title>
124 <content type="html">#{user.nickname} favorited something</content>
125 <published>#{like.data["published"]}</published>
126 <updated>#{like.data["published"]}</updated>
127 <activity:object>
128 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
129 <id>#{note.data["id"]}</id>
130 </activity:object>
131 <ostatus:conversation>#{like.data["context"]}</ostatus:conversation>
132 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
133 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
134 <thr:in-reply-to ref="#{note.data["id"]}" />
135 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
136 """
137
138 assert clean(res) == clean(expected)
139 end
140
141 test "a follow activity" do
142 follower = insert(:user)
143 followed = insert(:user)
144 {:ok, activity} = ActivityPub.insert(%{
145 "type" => "Follow",
146 "actor" => follower.ap_id,
147 "object" => followed.ap_id,
148 "to" => [followed.ap_id]
149 })
150
151 tuple = ActivityRepresenter.to_simple_form(activity, follower)
152
153 refute is_nil(tuple)
154
155 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
156
157 expected = """
158 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
159 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
160 <id>#{activity.data["id"]}</id>
161 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
162 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
163 <published>#{activity.data["published"]}</published>
164 <updated>#{activity.data["published"]}</updated>
165 <activity:object>
166 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
167 <id>#{activity.data["object"]}</id>
168 <uri>#{activity.data["object"]}</uri>
169 </activity:object>
170 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
171 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{activity.data["object"]}"/>
172 """
173
174 assert clean(res) == clean(expected)
175 end
176
177 test "an unfollow activity" do
178 follower = insert(:user)
179 followed = insert(:user)
180 {:ok, _activity} = ActivityPub.follow(follower, followed)
181 {:ok, activity} = ActivityPub.unfollow(follower, followed)
182
183 tuple = ActivityRepresenter.to_simple_form(activity, follower)
184
185 refute is_nil(tuple)
186
187 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
188
189 expected = """
190 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
191 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
192 <id>#{activity.data["id"]}</id>
193 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
194 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
195 <published>#{activity.data["published"]}</published>
196 <updated>#{activity.data["published"]}</updated>
197 <activity:object>
198 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
199 <id>#{followed.ap_id}</id>
200 <uri>#{followed.ap_id}</uri>
201 </activity:object>
202 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
203 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{followed.ap_id}"/>
204 """
205
206 assert clean(res) == clean(expected)
207 end
208
209 test "an unknown activity" do
210 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
211 assert is_nil(tuple)
212 end
213
214 defp clean(string) do
215 String.replace(string, ~r/\s/, "")
216 end
217 end