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