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