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