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.Utils
9 alias Pleroma.Web.Endpoint
12 test "it adds attachment links to a given text and attachment set" do
14 "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"
17 "url" => [%{"href" => name}]
20 res = Utils.add_attachments("", [attachment])
23 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
26 describe "it confirms the password given is the current users password" do
27 test "incorrect password given" do
28 {:ok, user} = UserBuilder.insert()
30 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
33 test "correct password given" do
34 {:ok, user} = UserBuilder.insert()
35 assert Utils.confirm_current_password(user, "test") == {:ok, user}
39 test "parses emoji from name and bio" do
40 {:ok, user} = UserBuilder.insert(%{name: ":karjalanpiirakka:", bio: ":perkele:"})
45 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/finmoji/128px/perkele-128.png"},
52 "url" => "#{Endpoint.url()}/finmoji/128px/karjalanpiirakka-128.png"
54 "name" => ":karjalanpiirakka:"
58 assert expected == Utils.emoji_from_profile(user)
61 describe "format_input/3" do
62 test "works for bare text/plain" do
64 expected = "hello world!"
66 {output, [], []} = Utils.format_input(text, "text/plain")
68 assert output == expected
70 text = "hello world!\n\nsecond paragraph!"
71 expected = "hello world!<br><br>second paragraph!"
73 {output, [], []} = Utils.format_input(text, "text/plain")
75 assert output == expected
78 test "works for bare text/html" do
79 text = "<p>hello world!</p>"
80 expected = "<p>hello world!</p>"
82 {output, [], []} = Utils.format_input(text, "text/html")
84 assert output == expected
86 text = "<p>hello world!</p>\n\n<p>second paragraph</p>"
87 expected = "<p>hello world!</p>\n\n<p>second paragraph</p>"
89 {output, [], []} = Utils.format_input(text, "text/html")
91 assert output == expected
94 test "works for bare text/markdown" do
95 text = "**hello world**"
96 expected = "<p><strong>hello world</strong></p>\n"
98 {output, [], []} = Utils.format_input(text, "text/markdown")
100 assert output == expected
102 text = "**hello world**\n\n*another paragraph*"
103 expected = "<p><strong>hello world</strong></p>\n<p><em>another paragraph</em></p>\n"
105 {output, [], []} = Utils.format_input(text, "text/markdown")
107 assert output == expected
115 expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n"
117 {output, [], []} = Utils.format_input(text, "text/markdown")
119 assert output == expected
122 test "works for text/markdown with mentions" do
124 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
126 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
129 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
131 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> and <span class=\"h-card\"><a data-user=\"#{
133 }\" 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"
135 {output, _, _} = Utils.format_input(text, "text/markdown")
137 assert output == expected
141 describe "context_to_conversation_id" do
142 test "creates a mapping object" do
143 conversation_id = Utils.context_to_conversation_id("random context")
144 object = Object.get_by_ap_id("random context")
146 assert conversation_id == object.id
149 test "returns an existing mapping for an existing object" do
150 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
151 conversation_id = Utils.context_to_conversation_id("random context")
153 assert conversation_id == object.id
157 describe "formats date to asctime" do
158 test "when date is in ISO 8601 format" do
159 date = DateTime.utc_now() |> DateTime.to_iso8601()
163 |> DateTime.from_iso8601()
165 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
167 assert Utils.date_to_asctime(date) == expected
170 test "when date is a binary in wrong format" do
171 date = DateTime.utc_now()
175 assert Utils.date_to_asctime(date) == expected
178 test "when date is a Unix timestamp" do
179 date = DateTime.utc_now() |> DateTime.to_unix()
183 assert Utils.date_to_asctime(date) == expected
186 test "when date is nil" do
189 assert Utils.date_to_asctime(nil) == expected