Merge branch 'develop' into 'feature/staff-discovery-api'
[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 Mock
16
17 test "a create activity with a note" do
18 user = insert(:user)
19 other_user = insert(:user, %{nickname: "shp"})
20
21 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
22
23 result = ActivityView.render("activity.json", activity: activity)
24
25 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
26
27 expected = %{
28 "activity_type" => "post",
29 "attachments" => [],
30 "attentions" => [
31 UserView.render("show.json", %{user: other_user})
32 ],
33 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
34 "external_url" => activity.data["object"]["id"],
35 "fave_num" => 0,
36 "favorited" => false,
37 "id" => activity.id,
38 "in_reply_to_status_id" => nil,
39 "is_local" => true,
40 "is_post_verb" => true,
41 "possibly_sensitive" => false,
42 "repeat_num" => 0,
43 "repeated" => false,
44 "statusnet_conversation_id" => convo_id,
45 "statusnet_html" =>
46 "Hey <span><a href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
47 "tags" => [],
48 "text" => "Hey @shp!",
49 "uri" => activity.data["object"]["id"],
50 "user" => UserView.render("show.json", %{user: user}),
51 "visibility" => "direct",
52 "summary" => nil
53 }
54
55 assert result == expected
56 end
57
58 test "a list of activities" do
59 user = insert(:user)
60 other_user = insert(:user, %{nickname: "shp"})
61 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
62
63 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
64
65 mocks = [
66 {
67 TwitterAPI,
68 [],
69 [context_to_conversation_id: fn _ -> false end]
70 },
71 {
72 User,
73 [:passthrough],
74 [get_cached_by_ap_id: fn _ -> nil end]
75 }
76 ]
77
78 with_mocks mocks do
79 [result] = ActivityView.render("index.json", activities: [activity])
80
81 assert result["statusnet_conversation_id"] == convo_id
82 assert result["user"]
83 refute called(TwitterAPI.context_to_conversation_id(:_))
84 refute called(User.get_cached_by_ap_id(user.ap_id))
85 refute called(User.get_cached_by_ap_id(other_user.ap_id))
86 end
87 end
88
89 test "an activity that is a reply" do
90 user = insert(:user)
91 other_user = insert(:user, %{nickname: "shp"})
92
93 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
94
95 {:ok, answer} =
96 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
97
98 result = ActivityView.render("activity.json", %{activity: answer})
99
100 assert result["in_reply_to_status_id"] == activity.id
101 end
102
103 test "a like activity" do
104 user = insert(:user)
105 other_user = insert(:user, %{nickname: "shp"})
106
107 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
108 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
109
110 result = ActivityView.render("activity.json", activity: like)
111
112 expected = %{
113 "activity_type" => "like",
114 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
115 "external_url" => like.data["id"],
116 "id" => like.id,
117 "in_reply_to_status_id" => activity.id,
118 "is_local" => true,
119 "is_post_verb" => false,
120 "statusnet_html" => "shp favorited a status.",
121 "text" => "shp favorited a status.",
122 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
123 "user" => UserView.render("show.json", user: other_user)
124 }
125
126 assert result == expected
127 end
128
129 test "a like activity for deleted post" do
130 user = insert(:user)
131 other_user = insert(:user, %{nickname: "shp"})
132
133 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
134 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
135 CommonAPI.delete(activity.id, user)
136
137 result = ActivityView.render("activity.json", activity: like)
138
139 expected = %{
140 "activity_type" => "like",
141 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
142 "external_url" => like.data["id"],
143 "id" => like.id,
144 "in_reply_to_status_id" => nil,
145 "is_local" => true,
146 "is_post_verb" => false,
147 "statusnet_html" => "shp favorited a status.",
148 "text" => "shp favorited a status.",
149 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
150 "user" => UserView.render("show.json", user: other_user)
151 }
152
153 assert result == expected
154 end
155
156 test "an announce activity" do
157 user = insert(:user)
158 other_user = insert(:user, %{nickname: "shp"})
159
160 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
161 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
162
163 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
164
165 activity = Repo.get(Activity, activity.id)
166
167 result = ActivityView.render("activity.json", activity: announce)
168
169 expected = %{
170 "activity_type" => "repeat",
171 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
172 "external_url" => announce.data["id"],
173 "id" => announce.id,
174 "is_local" => true,
175 "is_post_verb" => false,
176 "statusnet_html" => "shp retweeted a status.",
177 "text" => "shp retweeted a status.",
178 "uri" => "tag:#{announce.data["id"]}:objectType=note",
179 "user" => UserView.render("show.json", user: other_user),
180 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
181 "statusnet_conversation_id" => convo_id
182 }
183
184 assert result == expected
185 end
186
187 test "A follow activity" do
188 user = insert(:user)
189 other_user = insert(:user, %{nickname: "shp"})
190
191 {:ok, follower} = User.follow(user, other_user)
192 {:ok, follow} = ActivityPub.follow(follower, other_user)
193
194 result = ActivityView.render("activity.json", activity: follow)
195
196 expected = %{
197 "activity_type" => "follow",
198 "attentions" => [],
199 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
200 "external_url" => follow.data["id"],
201 "id" => follow.id,
202 "in_reply_to_status_id" => nil,
203 "is_local" => true,
204 "is_post_verb" => false,
205 "statusnet_html" => "#{user.nickname} started following shp",
206 "text" => "#{user.nickname} started following shp",
207 "user" => UserView.render("show.json", user: user)
208 }
209
210 assert result == expected
211 end
212
213 test "a delete activity" do
214 user = insert(:user)
215
216 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
217 {:ok, delete} = CommonAPI.delete(activity.id, user)
218
219 result = ActivityView.render("activity.json", activity: delete)
220
221 expected = %{
222 "activity_type" => "delete",
223 "attentions" => [],
224 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
225 "external_url" => delete.data["id"],
226 "id" => delete.id,
227 "in_reply_to_status_id" => nil,
228 "is_local" => true,
229 "is_post_verb" => false,
230 "statusnet_html" => "deleted notice {{tag",
231 "text" => "deleted notice {{tag",
232 "uri" => delete.data["object"],
233 "user" => UserView.render("show.json", user: user)
234 }
235
236 assert result == expected
237 end
238 end