Merge branch 'captcha' into 'develop'
[akkoma] / test / web / twitter_api / views / activity_view_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.TwitterAPI.ActivityViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.TwitterAPI.ActivityView
11 alias Pleroma.Web.TwitterAPI.UserView
12 alias Pleroma.Web.TwitterAPI.TwitterAPI
13 alias Pleroma.Repo
14 alias Pleroma.Activity
15 alias Pleroma.User
16 alias Pleroma.Web.ActivityPub.ActivityPub
17
18 import Pleroma.Factory
19 import Tesla.Mock
20
21 setup do
22 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 :ok
24 end
25
26 import Mock
27
28 test "a create activity with a html status" do
29 text = """
30 #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
31 """
32
33 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
34
35 result = ActivityView.render("activity.json", activity: activity)
36
37 assert result["statusnet_html"] ==
38 "<a data-tag=\"bike\" href=\"http://localhost:4001/tag/bike\">#Bike</a> log - Commute Tuesday<br /><a href=\"https://pla.bike/posts/20181211/\">https://pla.bike/posts/20181211/</a><br /><a data-tag=\"cycling\" href=\"http://localhost:4001/tag/cycling\">#cycling</a> <a data-tag=\"chscycling\" href=\"http://localhost:4001/tag/chscycling\">#CHScycling</a> <a data-tag=\"commute\" href=\"http://localhost:4001/tag/commute\">#commute</a><br />MVIMG_20181211_054020.jpg"
39
40 assert result["text"] ==
41 "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
42 end
43
44 test "a create activity with a note" do
45 user = insert(:user)
46 other_user = insert(:user, %{nickname: "shp"})
47
48 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
49
50 result = ActivityView.render("activity.json", activity: activity)
51
52 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
53
54 expected = %{
55 "activity_type" => "post",
56 "attachments" => [],
57 "attentions" => [
58 UserView.render("show.json", %{user: other_user})
59 ],
60 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
61 "external_url" => activity.data["object"]["id"],
62 "fave_num" => 0,
63 "favorited" => false,
64 "id" => activity.id,
65 "in_reply_to_status_id" => nil,
66 "in_reply_to_screen_name" => nil,
67 "in_reply_to_user_id" => nil,
68 "in_reply_to_profileurl" => nil,
69 "in_reply_to_ostatus_uri" => nil,
70 "is_local" => true,
71 "is_post_verb" => true,
72 "possibly_sensitive" => false,
73 "repeat_num" => 0,
74 "repeated" => false,
75 "statusnet_conversation_id" => convo_id,
76 "statusnet_html" =>
77 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
78 "tags" => [],
79 "text" => "Hey @shp!",
80 "uri" => activity.data["object"]["id"],
81 "user" => UserView.render("show.json", %{user: user}),
82 "visibility" => "direct",
83 "summary" => nil
84 }
85
86 assert result == expected
87 end
88
89 test "a list of activities" do
90 user = insert(:user)
91 other_user = insert(:user, %{nickname: "shp"})
92 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
93
94 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
95
96 mocks = [
97 {
98 TwitterAPI,
99 [],
100 [context_to_conversation_id: fn _ -> false end]
101 },
102 {
103 User,
104 [:passthrough],
105 [get_cached_by_ap_id: fn _ -> nil end]
106 }
107 ]
108
109 with_mocks mocks do
110 [result] = ActivityView.render("index.json", activities: [activity])
111
112 assert result["statusnet_conversation_id"] == convo_id
113 assert result["user"]
114 refute called(TwitterAPI.context_to_conversation_id(:_))
115 refute called(User.get_cached_by_ap_id(user.ap_id))
116 refute called(User.get_cached_by_ap_id(other_user.ap_id))
117 end
118 end
119
120 test "an activity that is a reply" do
121 user = insert(:user)
122 other_user = insert(:user, %{nickname: "shp"})
123
124 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
125
126 {:ok, answer} =
127 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
128
129 result = ActivityView.render("activity.json", %{activity: answer})
130
131 assert result["in_reply_to_status_id"] == activity.id
132 end
133
134 test "a like activity" do
135 user = insert(:user)
136 other_user = insert(:user, %{nickname: "shp"})
137
138 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
139 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
140
141 result = ActivityView.render("activity.json", activity: like)
142 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
143
144 expected = %{
145 "activity_type" => "like",
146 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
147 "external_url" => like.data["id"],
148 "id" => like.id,
149 "in_reply_to_status_id" => activity.id,
150 "is_local" => true,
151 "is_post_verb" => false,
152 "favorited_status" => ActivityView.render("activity.json", activity: activity),
153 "statusnet_html" => "shp favorited a status.",
154 "text" => "shp favorited a status.",
155 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
156 "user" => UserView.render("show.json", user: other_user)
157 }
158
159 assert result == expected
160 end
161
162 test "a like activity for deleted post" do
163 user = insert(:user)
164 other_user = insert(:user, %{nickname: "shp"})
165
166 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
167 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
168 CommonAPI.delete(activity.id, user)
169
170 result = ActivityView.render("activity.json", activity: like)
171
172 expected = %{
173 "activity_type" => "like",
174 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
175 "external_url" => like.data["id"],
176 "id" => like.id,
177 "in_reply_to_status_id" => nil,
178 "is_local" => true,
179 "is_post_verb" => false,
180 "favorited_status" => nil,
181 "statusnet_html" => "shp favorited a status.",
182 "text" => "shp favorited a status.",
183 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
184 "user" => UserView.render("show.json", user: other_user)
185 }
186
187 assert result == expected
188 end
189
190 test "an announce activity" do
191 user = insert(:user)
192 other_user = insert(:user, %{nickname: "shp"})
193
194 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
195 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
196
197 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
198
199 activity = Repo.get(Activity, activity.id)
200
201 result = ActivityView.render("activity.json", activity: announce)
202
203 expected = %{
204 "activity_type" => "repeat",
205 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
206 "external_url" => announce.data["id"],
207 "id" => announce.id,
208 "is_local" => true,
209 "is_post_verb" => false,
210 "statusnet_html" => "shp retweeted a status.",
211 "text" => "shp retweeted a status.",
212 "uri" => "tag:#{announce.data["id"]}:objectType=note",
213 "user" => UserView.render("show.json", user: other_user),
214 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
215 "statusnet_conversation_id" => convo_id
216 }
217
218 assert result == expected
219 end
220
221 test "A follow activity" do
222 user = insert(:user)
223 other_user = insert(:user, %{nickname: "shp"})
224
225 {:ok, follower} = User.follow(user, other_user)
226 {:ok, follow} = ActivityPub.follow(follower, other_user)
227
228 result = ActivityView.render("activity.json", activity: follow)
229
230 expected = %{
231 "activity_type" => "follow",
232 "attentions" => [],
233 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
234 "external_url" => follow.data["id"],
235 "id" => follow.id,
236 "in_reply_to_status_id" => nil,
237 "is_local" => true,
238 "is_post_verb" => false,
239 "statusnet_html" => "#{user.nickname} started following shp",
240 "text" => "#{user.nickname} started following shp",
241 "user" => UserView.render("show.json", user: user)
242 }
243
244 assert result == expected
245 end
246
247 test "a delete activity" do
248 user = insert(:user)
249
250 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
251 {:ok, delete} = CommonAPI.delete(activity.id, user)
252
253 result = ActivityView.render("activity.json", activity: delete)
254
255 expected = %{
256 "activity_type" => "delete",
257 "attentions" => [],
258 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
259 "external_url" => delete.data["id"],
260 "id" => delete.id,
261 "in_reply_to_status_id" => nil,
262 "is_local" => true,
263 "is_post_verb" => false,
264 "statusnet_html" => "deleted notice {{tag",
265 "text" => "deleted notice {{tag",
266 "uri" => delete.data["object"],
267 "user" => UserView.render("show.json", user: user)
268 }
269
270 assert result == expected
271 end
272
273 test "a peertube video" do
274 {:ok, object} =
275 ActivityPub.fetch_object_from_id(
276 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
277 )
278
279 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
280
281 result = ActivityView.render("activity.json", activity: activity)
282
283 assert length(result["attachments"]) == 1
284 assert result["summary"] == "Friday Night"
285 end
286 end