1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI.UtilsTest do
6 alias Pleroma.Builders.UserBuilder
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.Endpoint
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
16 @public_address "https://www.w3.org/ns/activitystreams#Public"
18 test "it adds attachment links to a given text and attachment set" do
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"
23 "url" => [%{"href" => name}]
26 res = Utils.add_attachments("", [attachment])
29 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
32 describe "it confirms the password given is the current users password" do
33 test "incorrect password given" do
34 {:ok, user} = UserBuilder.insert()
36 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
39 test "correct password given" do
40 {:ok, user} = UserBuilder.insert()
41 assert Utils.confirm_current_password(user, "test") == {:ok, user}
45 test "parses emoji from name and bio" do
46 {:ok, user} = UserBuilder.insert(%{name: ":blank:", bio: ":firefox:"})
51 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/emoji/Firefox.gif"},
58 "url" => "#{Endpoint.url()}/emoji/blank.png"
64 assert expected == Utils.emoji_from_profile(user)
67 describe "format_input/3" do
68 test "works for bare text/plain" do
70 expected = "hello world!"
72 {output, [], []} = Utils.format_input(text, "text/plain")
74 assert output == expected
76 text = "hello world!\n\nsecond paragraph!"
77 expected = "hello world!<br><br>second paragraph!"
79 {output, [], []} = Utils.format_input(text, "text/plain")
81 assert output == expected
84 test "works for bare text/html" do
85 text = "<p>hello world!</p>"
86 expected = "<p>hello world!</p>"
88 {output, [], []} = Utils.format_input(text, "text/html")
90 assert output == expected
92 text = "<p>hello world!</p>\n\n<p>second paragraph</p>"
93 expected = "<p>hello world!</p>\n\n<p>second paragraph</p>"
95 {output, [], []} = Utils.format_input(text, "text/html")
97 assert output == expected
100 test "works for bare text/markdown" do
101 text = "**hello world**"
102 expected = "<p><strong>hello world</strong></p>\n"
104 {output, [], []} = Utils.format_input(text, "text/markdown")
106 assert output == expected
108 text = "**hello world**\n\n*another paragraph*"
109 expected = "<p><strong>hello world</strong></p>\n<p><em>another paragraph</em></p>\n"
111 {output, [], []} = Utils.format_input(text, "text/markdown")
113 assert output == expected
121 expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n"
123 {output, [], []} = Utils.format_input(text, "text/markdown")
125 assert output == expected
128 test "works for bare text/bbcode" do
129 text = "[b]hello world[/b]"
130 expected = "<strong>hello world</strong>"
132 {output, [], []} = Utils.format_input(text, "text/bbcode")
134 assert output == expected
136 text = "[b]hello world![/b]\n\nsecond paragraph!"
137 expected = "<strong>hello world!</strong><br>\n<br>\nsecond paragraph!"
139 {output, [], []} = Utils.format_input(text, "text/bbcode")
141 assert output == expected
143 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
146 "<strong>hello world!</strong><br>\n<br>\n<strong>second paragraph!</strong>"
148 {output, [], []} = Utils.format_input(text, "text/bbcode")
150 assert output == expected
153 test "works for text/markdown with mentions" do
155 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
157 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
160 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
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=\"#{
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"
166 {output, _, _} = Utils.format_input(text, "text/markdown")
168 assert output == expected
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")
177 assert conversation_id == object.id
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")
184 assert conversation_id == object.id
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()
194 |> DateTime.from_iso8601()
196 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
198 assert Utils.date_to_asctime(date) == expected
201 test "when date is a binary in wrong format" do
202 date = DateTime.utc_now()
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"
211 test "when date is a Unix timestamp" do
212 date = DateTime.utc_now() |> DateTime.to_unix()
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"
221 test "when date is nil" do
224 assert capture_log(fn ->
225 assert Utils.date_to_asctime(nil) == expected
226 end) =~ "[warn] Date in wrong format, must be ISO 8601"
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"
236 describe "get_to_and_cc" do
237 test "for public posts, not a reply" do
239 mentioned_user = insert(:user)
240 mentions = [mentioned_user.ap_id]
242 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public")
244 assert length(to) == 2
245 assert length(cc) == 1
247 assert @public_address in to
248 assert mentioned_user.ap_id in to
249 assert user.follower_address in cc
252 test "for public posts, a reply" do
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]
259 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public")
261 assert length(to) == 3
262 assert length(cc) == 1
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
270 test "for unlisted posts, not a reply" do
272 mentioned_user = insert(:user)
273 mentions = [mentioned_user.ap_id]
275 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted")
277 assert length(to) == 2
278 assert length(cc) == 1
280 assert @public_address in cc
281 assert mentioned_user.ap_id in to
282 assert user.follower_address in to
285 test "for unlisted posts, a reply" do
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]
292 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted")
294 assert length(to) == 3
295 assert length(cc) == 1
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
303 test "for private posts, not a reply" do
305 mentioned_user = insert(:user)
306 mentions = [mentioned_user.ap_id]
308 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private")
310 assert length(to) == 2
311 assert length(cc) == 0
313 assert mentioned_user.ap_id in to
314 assert user.follower_address in to
317 test "for private posts, a reply" do
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]
324 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private")
326 assert length(to) == 3
327 assert length(cc) == 0
329 assert mentioned_user.ap_id in to
330 assert third_user.ap_id in to
331 assert user.follower_address in to
334 test "for direct posts, not a reply" do
336 mentioned_user = insert(:user)
337 mentions = [mentioned_user.ap_id]
339 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct")
341 assert length(to) == 1
342 assert length(cc) == 0
344 assert mentioned_user.ap_id in to
347 test "for direct posts, a reply" do
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]
354 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct")
356 assert length(to) == 2
357 assert length(cc) == 0
359 assert mentioned_user.ap_id in to
360 assert third_user.ap_id in to