Move comments for email_notifications config to docs
[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.Utils
9 alias Pleroma.Web.Endpoint
10 use Pleroma.DataCase
11
12 test "it adds attachment links to a given text and attachment set" do
13 name =
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"
15
16 attachment = %{
17 "url" => [%{"href" => name}]
18 }
19
20 res = Utils.add_attachments("", [attachment])
21
22 assert res ==
23 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
24 end
25
26 describe "it confirms the password given is the current users password" do
27 test "incorrect password given" do
28 {:ok, user} = UserBuilder.insert()
29
30 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
31 end
32
33 test "correct password given" do
34 {:ok, user} = UserBuilder.insert()
35 assert Utils.confirm_current_password(user, "test") == {:ok, user}
36 end
37 end
38
39 test "parses emoji from name and bio" do
40 {:ok, user} = UserBuilder.insert(%{name: ":karjalanpiirakka:", bio: ":perkele:"})
41
42 expected = [
43 %{
44 "type" => "Emoji",
45 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/finmoji/128px/perkele-128.png"},
46 "name" => ":perkele:"
47 },
48 %{
49 "type" => "Emoji",
50 "icon" => %{
51 "type" => "Image",
52 "url" => "#{Endpoint.url()}/finmoji/128px/karjalanpiirakka-128.png"
53 },
54 "name" => ":karjalanpiirakka:"
55 }
56 ]
57
58 assert expected == Utils.emoji_from_profile(user)
59 end
60
61 describe "format_input/3" do
62 test "works for bare text/plain" do
63 text = "hello world!"
64 expected = "hello world!"
65
66 {output, [], []} = Utils.format_input(text, "text/plain")
67
68 assert output == expected
69
70 text = "hello world!\n\nsecond paragraph!"
71 expected = "hello world!<br><br>second paragraph!"
72
73 {output, [], []} = Utils.format_input(text, "text/plain")
74
75 assert output == expected
76 end
77
78 test "works for bare text/html" do
79 text = "<p>hello world!</p>"
80 expected = "<p>hello world!</p>"
81
82 {output, [], []} = Utils.format_input(text, "text/html")
83
84 assert output == expected
85
86 text = "<p>hello world!</p>\n\n<p>second paragraph</p>"
87 expected = "<p>hello world!</p>\n\n<p>second paragraph</p>"
88
89 {output, [], []} = Utils.format_input(text, "text/html")
90
91 assert output == expected
92 end
93
94 test "works for bare text/markdown" do
95 text = "**hello world**"
96 expected = "<p><strong>hello world</strong></p>\n"
97
98 {output, [], []} = Utils.format_input(text, "text/markdown")
99
100 assert output == expected
101
102 text = "**hello world**\n\n*another paragraph*"
103 expected = "<p><strong>hello world</strong></p>\n<p><em>another paragraph</em></p>\n"
104
105 {output, [], []} = Utils.format_input(text, "text/markdown")
106
107 assert output == expected
108
109 text = """
110 > cool quote
111
112 by someone
113 """
114
115 expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n"
116
117 {output, [], []} = Utils.format_input(text, "text/markdown")
118
119 assert output == expected
120 end
121
122 test "works for text/markdown with mentions" do
123 {:ok, user} =
124 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
125
126 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
127
128 expected =
129 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
130 user.id
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=\"#{
132 user.id
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"
134
135 {output, _, _} = Utils.format_input(text, "text/markdown")
136
137 assert output == expected
138 end
139 end
140
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")
145
146 assert conversation_id == object.id
147 end
148
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")
152
153 assert conversation_id == object.id
154 end
155 end
156
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()
160
161 expected =
162 date
163 |> DateTime.from_iso8601()
164 |> elem(1)
165 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
166
167 assert Utils.date_to_asctime(date) == expected
168 end
169
170 test "when date is a binary in wrong format" do
171 date = DateTime.utc_now()
172
173 expected = ""
174
175 assert Utils.date_to_asctime(date) == expected
176 end
177
178 test "when date is a Unix timestamp" do
179 date = DateTime.utc_now() |> DateTime.to_unix()
180
181 expected = ""
182
183 assert Utils.date_to_asctime(date) == expected
184 end
185
186 test "when date is nil" do
187 expected = ""
188
189 assert Utils.date_to_asctime(nil) == expected
190 end
191 end
192 end