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.UploadTest do
8 import ExUnit.CaptureLog
11 alias Pleroma.Uploaders.Uploader
13 @upload_file %Plug.Upload{
14 content_type: "image/jpg",
15 path: Path.absname("test/fixtures/image_tmp.jpg"),
19 defmodule TestUploaderBase do
20 def put_file(%{path: path} = _upload, module_name) do
26 |> :global.whereis_name()
27 |> send({Uploader, self(), {:test}, %{}})
29 assert_receive {Uploader, {:test}}, 4_000
32 Agent.start(fn -> task_pid end, name: module_name)
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"}}
43 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
46 setup do: [uploader: TestUploaderSuccess]
47 setup [:ensure_local_uploader]
49 test "it returns file" do
50 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
52 assert Upload.store(@upload_file) ==
55 "name" => "image.jpg",
59 "href" => "http://localhost:4001/media/post-process-file.jpg",
60 "mediaType" => "image/jpeg",
66 Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
70 describe "Tried storing a file when http callback response error" do
71 defmodule TestUploaderError do
72 def http_callback(conn, _params), do: {:error, conn, "Errors"}
74 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
77 setup do: [uploader: TestUploaderError]
78 setup [:ensure_local_uploader]
80 test "it returns error" do
81 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
83 assert capture_log(fn ->
84 assert Upload.store(@upload_file) == {:error, "Errors"}
85 Task.await(Agent.get(TestUploaderError, fn task_pid -> task_pid end))
87 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploaderError) failed: \"Errors\""
91 describe "Tried storing a file when http callback doesn't response by timeout" do
92 defmodule(TestUploader, do: def(put_file(_upload), do: :wait_callback))
93 setup do: [uploader: TestUploader]
94 setup [:ensure_local_uploader]
96 test "it returns error" do
97 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
99 assert capture_log(fn ->
100 assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
102 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploader) failed: \"Uploader callback timeout\""
106 describe "Storing a file with the Local uploader" do
107 setup [:ensure_local_uploader]
109 test "returns a media url" do
110 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
113 content_type: "image/jpg",
114 path: Path.absname("test/fixtures/image_tmp.jpg"),
115 filename: "image.jpg"
118 {:ok, data} = Upload.store(file)
120 assert %{"url" => [%{"href" => url}]} = data
122 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
125 test "copies the file to the configured folder with deduping" do
126 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
129 content_type: "image/jpg",
130 path: Path.absname("test/fixtures/image_tmp.jpg"),
131 filename: "an [image.jpg"
134 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
136 assert List.first(data["url"])["href"] ==
137 Pleroma.Web.base_url() <>
138 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
141 test "copies the file to the configured folder without deduping" do
142 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
145 content_type: "image/jpg",
146 path: Path.absname("test/fixtures/image_tmp.jpg"),
147 filename: "an [image.jpg"
150 {:ok, data} = Upload.store(file)
151 assert data["name"] == "an [image.jpg"
154 test "fixes incorrect content type" do
155 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
158 content_type: "application/octet-stream",
159 path: Path.absname("test/fixtures/image_tmp.jpg"),
160 filename: "an [image.jpg"
163 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
164 assert hd(data["url"])["mediaType"] == "image/jpeg"
167 test "adds missing extension" do
168 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
171 content_type: "image/jpg",
172 path: Path.absname("test/fixtures/image_tmp.jpg"),
173 filename: "an [image"
176 {:ok, data} = Upload.store(file)
177 assert data["name"] == "an [image.jpg"
180 test "fixes incorrect file extension" do
181 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
184 content_type: "image/jpg",
185 path: Path.absname("test/fixtures/image_tmp.jpg"),
186 filename: "an [image.blah"
189 {:ok, data} = Upload.store(file)
190 assert data["name"] == "an [image.jpg"
193 test "don't modify filename of an unknown type" do
194 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
197 content_type: "text/plain",
198 path: Path.absname("test/fixtures/test_tmp.txt"),
202 {:ok, data} = Upload.store(file)
203 assert data["name"] == "test.txt"
206 test "copies the file to the configured folder with anonymizing filename" do
207 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
210 content_type: "image/jpg",
211 path: Path.absname("test/fixtures/image_tmp.jpg"),
212 filename: "an [image.jpg"
215 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
217 refute data["name"] == "an [image.jpg"
220 test "escapes invalid characters in url" do
221 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
224 content_type: "image/jpg",
225 path: Path.absname("test/fixtures/image_tmp.jpg"),
226 filename: "an… image.jpg"
229 {:ok, data} = Upload.store(file)
230 [attachment_url | _] = data["url"]
232 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
235 test "escapes reserved uri characters" do
236 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
239 content_type: "image/jpg",
240 path: Path.absname("test/fixtures/image_tmp.jpg"),
241 filename: ":?#[]@!$&\\'()*+,;=.jpg"
244 {:ok, data} = Upload.store(file)
245 [attachment_url | _] = data["url"]
247 assert Path.basename(attachment_url["href"]) ==
248 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
252 describe "Setting a custom base_url for uploaded media" do
254 Pleroma.Config.put([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
257 Pleroma.Config.put([Pleroma.Upload, :base_url], nil)
261 test "returns a media url with configured base_url" do
262 base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
264 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
267 content_type: "image/jpg",
268 path: Path.absname("test/fixtures/image_tmp.jpg"),
269 filename: "image.jpg"
272 {:ok, data} = Upload.store(file, base_url: base_url)
274 assert %{"url" => [%{"href" => url}]} = data
276 refute String.starts_with?(url, base_url <> "/media/")