a5b07c4469f89d8fc2938bf7829bc2428a3b19f2
[akkoma] / test / web / common_api / common_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.CommonAPITest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Object
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11
12 import Pleroma.Factory
13
14 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
15 har = insert(:user)
16 jafnhar = insert(:user)
17 tridi = insert(:user)
18 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
19 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
20
21 {:ok, activity} =
22 CommonAPI.post(har, %{
23 "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
24 "visibility" => "direct"
25 })
26
27 refute tridi.ap_id in activity.recipients
28 assert jafnhar.ap_id in activity.recipients
29 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
30 end
31
32 test "it de-duplicates tags" do
33 user = insert(:user)
34 {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"})
35
36 object = Object.normalize(activity.data["object"])
37
38 assert object.data["tag"] == ["2hu"]
39 end
40
41 test "it adds emoji in the object" do
42 user = insert(:user)
43 {:ok, activity} = CommonAPI.post(user, %{"status" => ":firefox:"})
44
45 assert Object.normalize(activity).data["emoji"]["firefox"]
46 end
47
48 test "it adds emoji when updating profiles" do
49 user = insert(:user, %{name: ":firefox:"})
50
51 CommonAPI.update(user)
52 user = User.get_cached_by_ap_id(user.ap_id)
53 [firefox] = user.info.source_data["tag"]
54
55 assert firefox["name"] == ":firefox:"
56 end
57
58 describe "posting" do
59 test "it filters out obviously bad tags when accepting a post as HTML" do
60 user = insert(:user)
61
62 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
63
64 {:ok, activity} =
65 CommonAPI.post(user, %{
66 "status" => post,
67 "content_type" => "text/html"
68 })
69
70 object = Object.normalize(activity.data["object"])
71
72 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
73 end
74
75 test "it filters out obviously bad tags when accepting a post as Markdown" do
76 user = insert(:user)
77
78 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
79
80 {:ok, activity} =
81 CommonAPI.post(user, %{
82 "status" => post,
83 "content_type" => "text/markdown"
84 })
85
86 object = Object.normalize(activity.data["object"])
87
88 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
89 end
90 end
91
92 describe "reactions" do
93 test "repeating a status" do
94 user = insert(:user)
95 other_user = insert(:user)
96
97 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
98
99 {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user)
100 end
101
102 test "favoriting a status" do
103 user = insert(:user)
104 other_user = insert(:user)
105
106 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
107
108 {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
109 end
110
111 test "retweeting a status twice returns an error" do
112 user = insert(:user)
113 other_user = insert(:user)
114
115 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
116 {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
117 {:error, _} = CommonAPI.repeat(activity.id, user)
118 end
119
120 test "favoriting a status twice returns an error" do
121 user = insert(:user)
122 other_user = insert(:user)
123
124 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
125 {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
126 {:error, _} = CommonAPI.favorite(activity.id, user)
127 end
128 end
129
130 describe "pinned statuses" do
131 setup do
132 Pleroma.Config.put([:instance, :max_pinned_statuses], 1)
133
134 user = insert(:user)
135 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"})
136
137 [user: user, activity: activity]
138 end
139
140 test "pin status", %{user: user, activity: activity} do
141 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
142
143 id = activity.id
144 user = refresh_record(user)
145
146 assert %User{info: %{pinned_activities: [^id]}} = user
147 end
148
149 test "only self-authored can be pinned", %{activity: activity} do
150 user = insert(:user)
151
152 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
153 end
154
155 test "max pinned statuses", %{user: user, activity: activity_one} do
156 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
157
158 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
159
160 user = refresh_record(user)
161
162 assert {:error, "You have already pinned the maximum number of statuses"} =
163 CommonAPI.pin(activity_two.id, user)
164 end
165
166 test "unpin status", %{user: user, activity: activity} do
167 {:ok, activity} = CommonAPI.pin(activity.id, user)
168
169 user = refresh_record(user)
170
171 assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user)
172
173 user = refresh_record(user)
174
175 assert %User{info: %{pinned_activities: []}} = user
176 end
177
178 test "should unpin when deleting a status", %{user: user, activity: activity} do
179 {:ok, activity} = CommonAPI.pin(activity.id, user)
180
181 user = refresh_record(user)
182
183 assert {:ok, _} = CommonAPI.delete(activity.id, user)
184
185 user = refresh_record(user)
186
187 assert %User{info: %{pinned_activities: []}} = user
188 end
189 end
190
191 describe "mute tests" do
192 setup do
193 user = insert(:user)
194
195 activity = insert(:note_activity)
196
197 [user: user, activity: activity]
198 end
199
200 test "add mute", %{user: user, activity: activity} do
201 {:ok, _} = CommonAPI.add_mute(user, activity)
202 assert CommonAPI.thread_muted?(user, activity)
203 end
204
205 test "remove mute", %{user: user, activity: activity} do
206 CommonAPI.add_mute(user, activity)
207 {:ok, _} = CommonAPI.remove_mute(user, activity)
208 refute CommonAPI.thread_muted?(user, activity)
209 end
210
211 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
212 CommonAPI.add_mute(user, activity)
213 {:error, _} = CommonAPI.add_mute(user, activity)
214 end
215 end
216
217 describe "reports" do
218 test "creates a report" do
219 reporter = insert(:user)
220 target_user = insert(:user)
221
222 {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"})
223
224 reporter_ap_id = reporter.ap_id
225 target_ap_id = target_user.ap_id
226 activity_ap_id = activity.data["id"]
227 comment = "foobar"
228
229 report_data = %{
230 "account_id" => target_user.id,
231 "comment" => comment,
232 "status_ids" => [activity.id]
233 }
234
235 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
236
237 assert %Activity{
238 actor: ^reporter_ap_id,
239 data: %{
240 "type" => "Flag",
241 "content" => ^comment,
242 "object" => [^target_ap_id, ^activity_ap_id]
243 }
244 } = flag_activity
245 end
246 end
247
248 describe "reblog muting" do
249 setup do
250 muter = insert(:user)
251
252 muted = insert(:user)
253
254 [muter: muter, muted: muted]
255 end
256
257 test "add a reblog mute", %{muter: muter, muted: muted} do
258 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
259
260 assert Pleroma.User.showing_reblogs?(muter, muted) == false
261 end
262
263 test "remove a reblog mute", %{muter: muter, muted: muted} do
264 {:ok, muter} = CommonAPI.hide_reblogs(muter, muted)
265 {:ok, muter} = CommonAPI.show_reblogs(muter, muted)
266
267 assert Pleroma.User.showing_reblogs?(muter, muted) == true
268 end
269 end
270 end