Merge branch 'feature/add-roles-to-users-admin-api' 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.Web.CommonAPI.Utils
7 alias Pleroma.Web.Endpoint
8 alias Pleroma.Builders.UserBuilder
9 use Pleroma.DataCase
10
11 test "it adds attachment links to a given text and attachment set" do
12 name =
13 "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"
14
15 attachment = %{
16 "url" => [%{"href" => name}]
17 }
18
19 res = Utils.add_attachments("", [attachment])
20
21 assert res ==
22 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
23 end
24
25 describe "it confirms the password given is the current users password" do
26 test "incorrect password given" do
27 {:ok, user} = UserBuilder.insert()
28
29 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
30 end
31
32 test "correct password given" do
33 {:ok, user} = UserBuilder.insert()
34 assert Utils.confirm_current_password(user, "test") == {:ok, user}
35 end
36 end
37
38 test "parses emoji from name and bio" do
39 {:ok, user} = UserBuilder.insert(%{name: ":karjalanpiirakka:", bio: ":perkele:"})
40
41 expected = [
42 %{
43 "type" => "Emoji",
44 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/finmoji/128px/perkele-128.png"},
45 "name" => ":perkele:"
46 },
47 %{
48 "type" => "Emoji",
49 "icon" => %{
50 "type" => "Image",
51 "url" => "#{Endpoint.url()}/finmoji/128px/karjalanpiirakka-128.png"
52 },
53 "name" => ":karjalanpiirakka:"
54 }
55 ]
56
57 assert expected == Utils.emoji_from_profile(user)
58 end
59
60 describe "format_input/3" do
61 test "works for bare text/plain" do
62 text = "hello world!"
63 expected = "hello world!"
64
65 {output, [], []} = Utils.format_input(text, "text/plain")
66
67 assert output == expected
68
69 text = "hello world!\n\nsecond paragraph!"
70 expected = "hello world!<br><br>second paragraph!"
71
72 {output, [], []} = Utils.format_input(text, "text/plain")
73
74 assert output == expected
75 end
76
77 test "works for bare text/html" do
78 text = "<p>hello world!</p>"
79 expected = "<p>hello world!</p>"
80
81 {output, [], []} = Utils.format_input(text, "text/html")
82
83 assert output == expected
84
85 text = "<p>hello world!</p>\n\n<p>second paragraph</p>"
86 expected = "<p>hello world!</p>\n\n<p>second paragraph</p>"
87
88 {output, [], []} = Utils.format_input(text, "text/html")
89
90 assert output == expected
91 end
92
93 test "works for bare text/markdown" do
94 text = "**hello world**"
95 expected = "<p><strong>hello world</strong></p>\n"
96
97 {output, [], []} = Utils.format_input(text, "text/markdown")
98
99 assert output == expected
100
101 text = "**hello world**\n\n*another paragraph*"
102 expected = "<p><strong>hello world</strong></p>\n<p><em>another paragraph</em></p>\n"
103
104 {output, [], []} = Utils.format_input(text, "text/markdown")
105
106 assert output == expected
107
108 text = """
109 > cool quote
110
111 by someone
112 """
113
114 expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n"
115
116 {output, [], []} = Utils.format_input(text, "text/markdown")
117
118 assert output == expected
119 end
120
121 test "works for text/markdown with mentions" do
122 {:ok, user} =
123 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
124
125 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
126
127 expected =
128 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
129 user.id
130 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> and <span class=\"h-card\"><a data-user=\"#{
131 user.id
132 }\" 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"
133
134 {output, _, _} = Utils.format_input(text, "text/markdown")
135
136 assert output == expected
137 end
138 end
139 end