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