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