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