ability to set and reset avatar, profile banner and backgroud in Mastodon API
[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: ":blank:", bio: ":firefox:"})
41
42 expected = [
43 %{
44 "type" => "Emoji",
45 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/emoji/Firefox.gif"},
46 "name" => ":firefox:"
47 },
48 %{
49 "type" => "Emoji",
50 "icon" => %{
51 "type" => "Image",
52 "url" => "#{Endpoint.url()}/emoji/blank.png"
53 },
54 "name" => ":blank:"
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 bare text/bbcode" do
123 text = "[b]hello world[/b]"
124 expected = "<strong>hello world</strong>"
125
126 {output, [], []} = Utils.format_input(text, "text/bbcode")
127
128 assert output == expected
129
130 text = "[b]hello world![/b]\n\nsecond paragraph!"
131 expected = "<strong>hello world!</strong><br>\n<br>\nsecond paragraph!"
132
133 {output, [], []} = Utils.format_input(text, "text/bbcode")
134
135 assert output == expected
136
137 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
138
139 expected =
140 "<strong>hello world!</strong><br>\n<br>\n&lt;strong&gt;second paragraph!&lt;/strong&gt;"
141
142 {output, [], []} = Utils.format_input(text, "text/bbcode")
143
144 assert output == expected
145 end
146
147 test "works for text/markdown with mentions" do
148 {:ok, user} =
149 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
150
151 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
152
153 expected =
154 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
155 user.id
156 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> and <span class=\"h-card\"><a data-user=\"#{
157 user.id
158 }\" 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"
159
160 {output, _, _} = Utils.format_input(text, "text/markdown")
161
162 assert output == expected
163 end
164 end
165
166 describe "context_to_conversation_id" do
167 test "creates a mapping object" do
168 conversation_id = Utils.context_to_conversation_id("random context")
169 object = Object.get_by_ap_id("random context")
170
171 assert conversation_id == object.id
172 end
173
174 test "returns an existing mapping for an existing object" do
175 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
176 conversation_id = Utils.context_to_conversation_id("random context")
177
178 assert conversation_id == object.id
179 end
180 end
181
182 describe "formats date to asctime" do
183 test "when date is in ISO 8601 format" do
184 date = DateTime.utc_now() |> DateTime.to_iso8601()
185
186 expected =
187 date
188 |> DateTime.from_iso8601()
189 |> elem(1)
190 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
191
192 assert Utils.date_to_asctime(date) == expected
193 end
194
195 test "when date is a binary in wrong format" do
196 date = DateTime.utc_now()
197
198 expected = ""
199
200 assert Utils.date_to_asctime(date) == expected
201 end
202
203 test "when date is a Unix timestamp" do
204 date = DateTime.utc_now() |> DateTime.to_unix()
205
206 expected = ""
207
208 assert Utils.date_to_asctime(date) == expected
209 end
210
211 test "when date is nil" do
212 expected = ""
213
214 assert Utils.date_to_asctime(nil) == expected
215 end
216 end
217 end