Merge remote-tracking branch 'upstream/develop' into feature/incoming-remote-unfollow
[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 }
53
54 assert result == expected
55 end
56
57 test "a list of activities" do
58 user = insert(:user)
59 other_user = insert(:user, %{nickname: "shp"})
60 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
61
62 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
63
64 mocks = [
65 {
66 TwitterAPI,
67 [],
68 [context_to_conversation_id: fn _ -> false end]
69 },
70 {
71 User,
72 [:passthrough],
73 [get_cached_by_ap_id: fn _ -> nil end]
74 }
75 ]
76
77 with_mocks mocks do
78 [result] = ActivityView.render("index.json", activities: [activity])
79
80 assert result["statusnet_conversation_id"] == convo_id
81 assert result["user"]
82 refute called(TwitterAPI.context_to_conversation_id(:_))
83 refute called(User.get_cached_by_ap_id(user.ap_id))
84 refute called(User.get_cached_by_ap_id(other_user.ap_id))
85 end
86 end
87
88 test "an activity that is a reply" do
89 user = insert(:user)
90 other_user = insert(:user, %{nickname: "shp"})
91
92 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
93
94 {:ok, answer} =
95 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
96
97 result = ActivityView.render("activity.json", %{activity: answer})
98
99 assert result["in_reply_to_status_id"] == activity.id
100 end
101
102 test "a like activity" do
103 user = insert(:user)
104 other_user = insert(:user, %{nickname: "shp"})
105
106 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
107 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
108
109 result = ActivityView.render("activity.json", activity: like)
110
111 expected = %{
112 "activity_type" => "like",
113 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
114 "external_url" => like.data["id"],
115 "id" => like.id,
116 "in_reply_to_status_id" => activity.id,
117 "is_local" => true,
118 "is_post_verb" => false,
119 "statusnet_html" => "shp favorited a status.",
120 "text" => "shp favorited a status.",
121 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
122 "user" => UserView.render("show.json", user: other_user)
123 }
124
125 assert result == expected
126 end
127
128 test "an announce activity" do
129 user = insert(:user)
130 other_user = insert(:user, %{nickname: "shp"})
131
132 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
133 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
134
135 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
136
137 activity = Repo.get(Activity, activity.id)
138
139 result = ActivityView.render("activity.json", activity: announce)
140
141 expected = %{
142 "activity_type" => "repeat",
143 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
144 "external_url" => announce.data["id"],
145 "id" => announce.id,
146 "is_local" => true,
147 "is_post_verb" => false,
148 "statusnet_html" => "shp retweeted a status.",
149 "text" => "shp retweeted a status.",
150 "uri" => "tag:#{announce.data["id"]}:objectType=note",
151 "user" => UserView.render("show.json", user: other_user),
152 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
153 "statusnet_conversation_id" => convo_id
154 }
155
156 assert result == expected
157 end
158
159 test "A follow activity" do
160 user = insert(:user)
161 other_user = insert(:user, %{nickname: "shp"})
162
163 {:ok, follower} = User.follow(user, other_user)
164 {:ok, follow} = ActivityPub.follow(follower, other_user)
165
166 result = ActivityView.render("activity.json", activity: follow)
167
168 expected = %{
169 "activity_type" => "follow",
170 "attentions" => [],
171 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
172 "external_url" => follow.data["id"],
173 "id" => follow.id,
174 "in_reply_to_status_id" => nil,
175 "is_local" => true,
176 "is_post_verb" => false,
177 "statusnet_html" => "#{user.nickname} started following shp",
178 "text" => "#{user.nickname} started following shp",
179 "user" => UserView.render("show.json", user: user)
180 }
181
182 assert result == expected
183 end
184
185 test "a delete activity" do
186 user = insert(:user)
187
188 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
189 {:ok, delete} = CommonAPI.delete(activity.id, user)
190
191 result = ActivityView.render("activity.json", activity: delete)
192
193 expected = %{
194 "activity_type" => "delete",
195 "attentions" => [],
196 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
197 "external_url" => delete.data["id"],
198 "id" => delete.id,
199 "in_reply_to_status_id" => nil,
200 "is_local" => true,
201 "is_post_verb" => false,
202 "statusnet_html" => "deleted notice {{tag",
203 "text" => "deleted notice {{tag",
204 "uri" => delete.data["object"],
205 "user" => UserView.render("show.json", user: user)
206 }
207
208 assert result == expected
209 end
210 end