Merge branch 'fix/theora-detection-read-bytes' 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
116 expected = %{
117 "activity_type" => "like",
118 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
119 "external_url" => like.data["id"],
120 "id" => like.id,
121 "in_reply_to_status_id" => activity.id,
122 "is_local" => true,
123 "is_post_verb" => false,
124 "statusnet_html" => "shp favorited a status.",
125 "text" => "shp favorited a status.",
126 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
127 "user" => UserView.render("show.json", user: other_user)
128 }
129
130 assert result == expected
131 end
132
133 test "a like activity for deleted post" do
134 user = insert(:user)
135 other_user = insert(:user, %{nickname: "shp"})
136
137 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
138 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
139 CommonAPI.delete(activity.id, user)
140
141 result = ActivityView.render("activity.json", activity: like)
142
143 expected = %{
144 "activity_type" => "like",
145 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
146 "external_url" => like.data["id"],
147 "id" => like.id,
148 "in_reply_to_status_id" => nil,
149 "is_local" => true,
150 "is_post_verb" => false,
151 "statusnet_html" => "shp favorited a status.",
152 "text" => "shp favorited a status.",
153 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
154 "user" => UserView.render("show.json", user: other_user)
155 }
156
157 assert result == expected
158 end
159
160 test "an announce activity" do
161 user = insert(:user)
162 other_user = insert(:user, %{nickname: "shp"})
163
164 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
165 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
166
167 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
168
169 activity = Repo.get(Activity, activity.id)
170
171 result = ActivityView.render("activity.json", activity: announce)
172
173 expected = %{
174 "activity_type" => "repeat",
175 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
176 "external_url" => announce.data["id"],
177 "id" => announce.id,
178 "is_local" => true,
179 "is_post_verb" => false,
180 "statusnet_html" => "shp retweeted a status.",
181 "text" => "shp retweeted a status.",
182 "uri" => "tag:#{announce.data["id"]}:objectType=note",
183 "user" => UserView.render("show.json", user: other_user),
184 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
185 "statusnet_conversation_id" => convo_id
186 }
187
188 assert result == expected
189 end
190
191 test "A follow activity" do
192 user = insert(:user)
193 other_user = insert(:user, %{nickname: "shp"})
194
195 {:ok, follower} = User.follow(user, other_user)
196 {:ok, follow} = ActivityPub.follow(follower, other_user)
197
198 result = ActivityView.render("activity.json", activity: follow)
199
200 expected = %{
201 "activity_type" => "follow",
202 "attentions" => [],
203 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
204 "external_url" => follow.data["id"],
205 "id" => follow.id,
206 "in_reply_to_status_id" => nil,
207 "is_local" => true,
208 "is_post_verb" => false,
209 "statusnet_html" => "#{user.nickname} started following shp",
210 "text" => "#{user.nickname} started following shp",
211 "user" => UserView.render("show.json", user: user)
212 }
213
214 assert result == expected
215 end
216
217 test "a delete activity" do
218 user = insert(:user)
219
220 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
221 {:ok, delete} = CommonAPI.delete(activity.id, user)
222
223 result = ActivityView.render("activity.json", activity: delete)
224
225 expected = %{
226 "activity_type" => "delete",
227 "attentions" => [],
228 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
229 "external_url" => delete.data["id"],
230 "id" => delete.id,
231 "in_reply_to_status_id" => nil,
232 "is_local" => true,
233 "is_post_verb" => false,
234 "statusnet_html" => "deleted notice {{tag",
235 "text" => "deleted notice {{tag",
236 "uri" => delete.data["object"],
237 "user" => UserView.render("show.json", user: user)
238 }
239
240 assert result == expected
241 end
242 end