Translated using Weblate (Italian)
[akkoma] / test / upload_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UploadTest do
6 use Pleroma.DataCase
7
8 import ExUnit.CaptureLog
9
10 alias Pleroma.Upload
11 alias Pleroma.Uploaders.Uploader
12
13 @upload_file %Plug.Upload{
14 content_type: "image/jpg",
15 path: Path.absname("test/fixtures/image_tmp.jpg"),
16 filename: "image.jpg"
17 }
18
19 defmodule TestUploaderBase do
20 def put_file(%{path: path} = _upload, module_name) do
21 task_pid =
22 Task.async(fn ->
23 :timer.sleep(10)
24
25 {Uploader, path}
26 |> :global.whereis_name()
27 |> send({Uploader, self(), {:test}, %{}})
28
29 assert_receive {Uploader, {:test}}, 4_000
30 end)
31
32 Agent.start(fn -> task_pid end, name: module_name)
33
34 :wait_callback
35 end
36 end
37
38 describe "Tried storing a file when http callback response success result" do
39 defmodule TestUploaderSuccess do
40 def http_callback(conn, _params),
41 do: {:ok, conn, {:file, "post-process-file.jpg"}}
42
43 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
44 end
45
46 setup do: [uploader: TestUploaderSuccess]
47 setup [:ensure_local_uploader]
48
49 test "it returns file" do
50 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
51
52 assert Upload.store(@upload_file) ==
53 {:ok,
54 %{
55 "name" => "image.jpg",
56 "type" => "Document",
57 "mediaType" => "image/jpeg",
58 "url" => [
59 %{
60 "href" => "http://localhost:4001/media/post-process-file.jpg",
61 "mediaType" => "image/jpeg",
62 "type" => "Link"
63 }
64 ]
65 }}
66
67 Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
68 end
69 end
70
71 describe "Tried storing a file when http callback response error" do
72 defmodule TestUploaderError do
73 def http_callback(conn, _params), do: {:error, conn, "Errors"}
74
75 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
76 end
77
78 setup do: [uploader: TestUploaderError]
79 setup [:ensure_local_uploader]
80
81 test "it returns error" do
82 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
83
84 assert capture_log(fn ->
85 assert Upload.store(@upload_file) == {:error, "Errors"}
86 Task.await(Agent.get(TestUploaderError, fn task_pid -> task_pid end))
87 end) =~
88 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploaderError) failed: \"Errors\""
89 end
90 end
91
92 describe "Tried storing a file when http callback doesn't response by timeout" do
93 defmodule(TestUploader, do: def(put_file(_upload), do: :wait_callback))
94 setup do: [uploader: TestUploader]
95 setup [:ensure_local_uploader]
96
97 test "it returns error" do
98 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
99
100 assert capture_log(fn ->
101 assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
102 end) =~
103 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploader) failed: \"Uploader callback timeout\""
104 end
105 end
106
107 describe "Storing a file with the Local uploader" do
108 setup [:ensure_local_uploader]
109
110 test "does not allow descriptions longer than the post limit" do
111 clear_config([:instance, :description_limit], 2)
112 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
113
114 file = %Plug.Upload{
115 content_type: "image/jpg",
116 path: Path.absname("test/fixtures/image_tmp.jpg"),
117 filename: "image.jpg"
118 }
119
120 {:error, :description_too_long} = Upload.store(file, description: "123")
121 end
122
123 test "returns a media url" do
124 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
125
126 file = %Plug.Upload{
127 content_type: "image/jpg",
128 path: Path.absname("test/fixtures/image_tmp.jpg"),
129 filename: "image.jpg"
130 }
131
132 {:ok, data} = Upload.store(file)
133
134 assert %{"url" => [%{"href" => url}]} = data
135
136 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
137 end
138
139 test "copies the file to the configured folder with deduping" do
140 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
141
142 file = %Plug.Upload{
143 content_type: "image/jpg",
144 path: Path.absname("test/fixtures/image_tmp.jpg"),
145 filename: "an [image.jpg"
146 }
147
148 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
149
150 assert List.first(data["url"])["href"] ==
151 Pleroma.Web.base_url() <>
152 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
153 end
154
155 test "copies the file to the configured folder without deduping" do
156 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
157
158 file = %Plug.Upload{
159 content_type: "image/jpg",
160 path: Path.absname("test/fixtures/image_tmp.jpg"),
161 filename: "an [image.jpg"
162 }
163
164 {:ok, data} = Upload.store(file)
165 assert data["name"] == "an [image.jpg"
166 end
167
168 test "fixes incorrect content type" do
169 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
170
171 file = %Plug.Upload{
172 content_type: "application/octet-stream",
173 path: Path.absname("test/fixtures/image_tmp.jpg"),
174 filename: "an [image.jpg"
175 }
176
177 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
178 assert hd(data["url"])["mediaType"] == "image/jpeg"
179 end
180
181 test "adds missing extension" do
182 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
183
184 file = %Plug.Upload{
185 content_type: "image/jpg",
186 path: Path.absname("test/fixtures/image_tmp.jpg"),
187 filename: "an [image"
188 }
189
190 {:ok, data} = Upload.store(file)
191 assert data["name"] == "an [image.jpg"
192 end
193
194 test "fixes incorrect file extension" do
195 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
196
197 file = %Plug.Upload{
198 content_type: "image/jpg",
199 path: Path.absname("test/fixtures/image_tmp.jpg"),
200 filename: "an [image.blah"
201 }
202
203 {:ok, data} = Upload.store(file)
204 assert data["name"] == "an [image.jpg"
205 end
206
207 test "don't modify filename of an unknown type" do
208 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
209
210 file = %Plug.Upload{
211 content_type: "text/plain",
212 path: Path.absname("test/fixtures/test_tmp.txt"),
213 filename: "test.txt"
214 }
215
216 {:ok, data} = Upload.store(file)
217 assert data["name"] == "test.txt"
218 end
219
220 test "copies the file to the configured folder with anonymizing filename" do
221 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
222
223 file = %Plug.Upload{
224 content_type: "image/jpg",
225 path: Path.absname("test/fixtures/image_tmp.jpg"),
226 filename: "an [image.jpg"
227 }
228
229 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
230
231 refute data["name"] == "an [image.jpg"
232 end
233
234 test "escapes invalid characters in url" do
235 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
236
237 file = %Plug.Upload{
238 content_type: "image/jpg",
239 path: Path.absname("test/fixtures/image_tmp.jpg"),
240 filename: "an… image.jpg"
241 }
242
243 {:ok, data} = Upload.store(file)
244 [attachment_url | _] = data["url"]
245
246 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
247 end
248
249 test "escapes reserved uri characters" do
250 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
251
252 file = %Plug.Upload{
253 content_type: "image/jpg",
254 path: Path.absname("test/fixtures/image_tmp.jpg"),
255 filename: ":?#[]@!$&\\'()*+,;=.jpg"
256 }
257
258 {:ok, data} = Upload.store(file)
259 [attachment_url | _] = data["url"]
260
261 assert Path.basename(attachment_url["href"]) ==
262 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
263 end
264 end
265
266 describe "Setting a custom base_url for uploaded media" do
267 setup do: clear_config([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
268
269 test "returns a media url with configured base_url" do
270 base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
271
272 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
273
274 file = %Plug.Upload{
275 content_type: "image/jpg",
276 path: Path.absname("test/fixtures/image_tmp.jpg"),
277 filename: "image.jpg"
278 }
279
280 {:ok, data} = Upload.store(file, base_url: base_url)
281
282 assert %{"url" => [%{"href" => url}]} = data
283
284 refute String.starts_with?(url, base_url <> "/media/")
285 end
286 end
287 end