Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / common_api / common_api_utils_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.CommonAPI.UtilsTest do
6 alias Pleroma.Builders.UserBuilder
7 alias Pleroma.Object
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.Endpoint
11 use Pleroma.DataCase
12
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
15
16 @public_address "https://www.w3.org/ns/activitystreams#Public"
17
18 test "it adds attachment links to a given text and attachment set" do
19 name =
20 "Sakura%20Mana%20%E2%80%93%20Turned%20on%20by%20a%20Senior%20OL%20with%20a%20Temptating%20Tight%20Skirt-s%20Full%20Hipline%20and%20Panty%20Shot-%20Beautiful%20Thick%20Thighs-%20and%20Erotic%20Ass-%20-2015-%20--%20Oppaitime%208-28-2017%206-50-33%20PM.png"
21
22 attachment = %{
23 "url" => [%{"href" => name}]
24 }
25
26 res = Utils.add_attachments("", [attachment])
27
28 assert res ==
29 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
30 end
31
32 describe "it confirms the password given is the current users password" do
33 test "incorrect password given" do
34 {:ok, user} = UserBuilder.insert()
35
36 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
37 end
38
39 test "correct password given" do
40 {:ok, user} = UserBuilder.insert()
41 assert Utils.confirm_current_password(user, "test") == {:ok, user}
42 end
43 end
44
45 test "parses emoji from name and bio" do
46 {:ok, user} = UserBuilder.insert(%{name: ":blank:", bio: ":firefox:"})
47
48 expected = [
49 %{
50 "type" => "Emoji",
51 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/emoji/Firefox.gif"},
52 "name" => ":firefox:"
53 },
54 %{
55 "type" => "Emoji",
56 "icon" => %{
57 "type" => "Image",
58 "url" => "#{Endpoint.url()}/emoji/blank.png"
59 },
60 "name" => ":blank:"
61 }
62 ]
63
64 assert expected == Utils.emoji_from_profile(user)
65 end
66
67 describe "format_input/3" do
68 test "works for bare text/plain" do
69 text = "hello world!"
70 expected = "hello world!"
71
72 {output, [], []} = Utils.format_input(text, "text/plain")
73
74 assert output == expected
75
76 text = "hello world!\n\nsecond paragraph!"
77 expected = "hello world!<br><br>second paragraph!"
78
79 {output, [], []} = Utils.format_input(text, "text/plain")
80
81 assert output == expected
82 end
83
84 test "works for bare text/html" do
85 text = "<p>hello world!</p>"
86 expected = "<p>hello world!</p>"
87
88 {output, [], []} = Utils.format_input(text, "text/html")
89
90 assert output == expected
91
92 text = "<p>hello world!</p>\n\n<p>second paragraph</p>"
93 expected = "<p>hello world!</p>\n\n<p>second paragraph</p>"
94
95 {output, [], []} = Utils.format_input(text, "text/html")
96
97 assert output == expected
98 end
99
100 test "works for bare text/markdown" do
101 text = "**hello world**"
102 expected = "<p><strong>hello world</strong></p>\n"
103
104 {output, [], []} = Utils.format_input(text, "text/markdown")
105
106 assert output == expected
107
108 text = "**hello world**\n\n*another paragraph*"
109 expected = "<p><strong>hello world</strong></p>\n<p><em>another paragraph</em></p>\n"
110
111 {output, [], []} = Utils.format_input(text, "text/markdown")
112
113 assert output == expected
114
115 text = """
116 > cool quote
117
118 by someone
119 """
120
121 expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n"
122
123 {output, [], []} = Utils.format_input(text, "text/markdown")
124
125 assert output == expected
126 end
127
128 test "works for bare text/bbcode" do
129 text = "[b]hello world[/b]"
130 expected = "<strong>hello world</strong>"
131
132 {output, [], []} = Utils.format_input(text, "text/bbcode")
133
134 assert output == expected
135
136 text = "[b]hello world![/b]\n\nsecond paragraph!"
137 expected = "<strong>hello world!</strong><br>\n<br>\nsecond paragraph!"
138
139 {output, [], []} = Utils.format_input(text, "text/bbcode")
140
141 assert output == expected
142
143 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
144
145 expected =
146 "<strong>hello world!</strong><br>\n<br>\n&lt;strong&gt;second paragraph!&lt;/strong&gt;"
147
148 {output, [], []} = Utils.format_input(text, "text/bbcode")
149
150 assert output == expected
151 end
152
153 test "works for text/markdown with mentions" do
154 {:ok, user} =
155 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
156
157 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
158
159 expected =
160 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
161 user.id
162 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> and <span class=\"h-card\"><a data-user=\"#{
163 user.id
164 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> <a href=\"http://google.com\">google.com</a> paragraph</em></p>\n"
165
166 {output, _, _} = Utils.format_input(text, "text/markdown")
167
168 assert output == expected
169 end
170 end
171
172 describe "context_to_conversation_id" do
173 test "creates a mapping object" do
174 conversation_id = Utils.context_to_conversation_id("random context")
175 object = Object.get_by_ap_id("random context")
176
177 assert conversation_id == object.id
178 end
179
180 test "returns an existing mapping for an existing object" do
181 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
182 conversation_id = Utils.context_to_conversation_id("random context")
183
184 assert conversation_id == object.id
185 end
186 end
187
188 describe "formats date to asctime" do
189 test "when date is in ISO 8601 format" do
190 date = DateTime.utc_now() |> DateTime.to_iso8601()
191
192 expected =
193 date
194 |> DateTime.from_iso8601()
195 |> elem(1)
196 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
197
198 assert Utils.date_to_asctime(date) == expected
199 end
200
201 test "when date is a binary in wrong format" do
202 date = DateTime.utc_now()
203
204 expected = ""
205
206 assert capture_log(fn ->
207 assert Utils.date_to_asctime(date) == expected
208 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
209 end
210
211 test "when date is a Unix timestamp" do
212 date = DateTime.utc_now() |> DateTime.to_unix()
213
214 expected = ""
215
216 assert capture_log(fn ->
217 assert Utils.date_to_asctime(date) == expected
218 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
219 end
220
221 test "when date is nil" do
222 expected = ""
223
224 assert capture_log(fn ->
225 assert Utils.date_to_asctime(nil) == expected
226 end) =~ "[warn] Date in wrong format, must be ISO 8601"
227 end
228
229 test "when date is a random string" do
230 assert capture_log(fn ->
231 assert Utils.date_to_asctime("foo") == ""
232 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
233 end
234 end
235
236 describe "get_to_and_cc" do
237 test "for public posts, not a reply" do
238 user = insert(:user)
239 mentioned_user = insert(:user)
240 mentions = [mentioned_user.ap_id]
241
242 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public")
243
244 assert length(to) == 2
245 assert length(cc) == 1
246
247 assert @public_address in to
248 assert mentioned_user.ap_id in to
249 assert user.follower_address in cc
250 end
251
252 test "for public posts, a reply" do
253 user = insert(:user)
254 mentioned_user = insert(:user)
255 third_user = insert(:user)
256 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
257 mentions = [mentioned_user.ap_id]
258
259 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public")
260
261 assert length(to) == 3
262 assert length(cc) == 1
263
264 assert @public_address in to
265 assert mentioned_user.ap_id in to
266 assert third_user.ap_id in to
267 assert user.follower_address in cc
268 end
269
270 test "for unlisted posts, not a reply" do
271 user = insert(:user)
272 mentioned_user = insert(:user)
273 mentions = [mentioned_user.ap_id]
274
275 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted")
276
277 assert length(to) == 2
278 assert length(cc) == 1
279
280 assert @public_address in cc
281 assert mentioned_user.ap_id in to
282 assert user.follower_address in to
283 end
284
285 test "for unlisted posts, a reply" do
286 user = insert(:user)
287 mentioned_user = insert(:user)
288 third_user = insert(:user)
289 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
290 mentions = [mentioned_user.ap_id]
291
292 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted")
293
294 assert length(to) == 3
295 assert length(cc) == 1
296
297 assert @public_address in cc
298 assert mentioned_user.ap_id in to
299 assert third_user.ap_id in to
300 assert user.follower_address in to
301 end
302
303 test "for private posts, not a reply" do
304 user = insert(:user)
305 mentioned_user = insert(:user)
306 mentions = [mentioned_user.ap_id]
307
308 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private")
309
310 assert length(to) == 2
311 assert length(cc) == 0
312
313 assert mentioned_user.ap_id in to
314 assert user.follower_address in to
315 end
316
317 test "for private posts, a reply" do
318 user = insert(:user)
319 mentioned_user = insert(:user)
320 third_user = insert(:user)
321 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
322 mentions = [mentioned_user.ap_id]
323
324 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private")
325
326 assert length(to) == 3
327 assert length(cc) == 0
328
329 assert mentioned_user.ap_id in to
330 assert third_user.ap_id in to
331 assert user.follower_address in to
332 end
333
334 test "for direct posts, not a reply" do
335 user = insert(:user)
336 mentioned_user = insert(:user)
337 mentions = [mentioned_user.ap_id]
338
339 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct")
340
341 assert length(to) == 1
342 assert length(cc) == 0
343
344 assert mentioned_user.ap_id in to
345 end
346
347 test "for direct posts, a reply" do
348 user = insert(:user)
349 mentioned_user = insert(:user)
350 third_user = insert(:user)
351 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
352 mentions = [mentioned_user.ap_id]
353
354 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct")
355
356 assert length(to) == 2
357 assert length(cc) == 0
358
359 assert mentioned_user.ap_id in to
360 assert third_user.ap_id in to
361 end
362 end
363 end