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