1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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",
57 "mediaType" => "image/jpeg",
60 "href" => "http://localhost:4001/media/post-process-file.jpg",
61 "mediaType" => "image/jpeg",
67 Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
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"}
75 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
78 setup do: [uploader: TestUploaderError]
79 setup [:ensure_local_uploader]
81 test "it returns error" do
82 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
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))
88 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploaderError) failed: \"Errors\""
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]
97 test "it returns error" do
98 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
100 assert capture_log(fn ->
101 assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
103 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploader) failed: \"Uploader callback timeout\""
107 describe "Storing a file with the Local uploader" do
108 setup [:ensure_local_uploader]
110 test "returns a media url" do
111 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
114 content_type: "image/jpg",
115 path: Path.absname("test/fixtures/image_tmp.jpg"),
116 filename: "image.jpg"
119 {:ok, data} = Upload.store(file)
121 assert %{"url" => [%{"href" => url}]} = data
123 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
126 test "copies the file to the configured folder with deduping" do
127 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
130 content_type: "image/jpg",
131 path: Path.absname("test/fixtures/image_tmp.jpg"),
132 filename: "an [image.jpg"
135 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
137 assert List.first(data["url"])["href"] ==
138 Pleroma.Web.base_url() <>
139 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
142 test "copies the file to the configured folder without deduping" do
143 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
146 content_type: "image/jpg",
147 path: Path.absname("test/fixtures/image_tmp.jpg"),
148 filename: "an [image.jpg"
151 {:ok, data} = Upload.store(file)
152 assert data["name"] == "an [image.jpg"
155 test "fixes incorrect content type" do
156 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
159 content_type: "application/octet-stream",
160 path: Path.absname("test/fixtures/image_tmp.jpg"),
161 filename: "an [image.jpg"
164 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
165 assert hd(data["url"])["mediaType"] == "image/jpeg"
168 test "adds missing extension" do
169 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
172 content_type: "image/jpg",
173 path: Path.absname("test/fixtures/image_tmp.jpg"),
174 filename: "an [image"
177 {:ok, data} = Upload.store(file)
178 assert data["name"] == "an [image.jpg"
181 test "fixes incorrect file extension" do
182 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
185 content_type: "image/jpg",
186 path: Path.absname("test/fixtures/image_tmp.jpg"),
187 filename: "an [image.blah"
190 {:ok, data} = Upload.store(file)
191 assert data["name"] == "an [image.jpg"
194 test "don't modify filename of an unknown type" do
195 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
198 content_type: "text/plain",
199 path: Path.absname("test/fixtures/test_tmp.txt"),
203 {:ok, data} = Upload.store(file)
204 assert data["name"] == "test.txt"
207 test "copies the file to the configured folder with anonymizing filename" do
208 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
211 content_type: "image/jpg",
212 path: Path.absname("test/fixtures/image_tmp.jpg"),
213 filename: "an [image.jpg"
216 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
218 refute data["name"] == "an [image.jpg"
221 test "escapes invalid characters in url" do
222 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
225 content_type: "image/jpg",
226 path: Path.absname("test/fixtures/image_tmp.jpg"),
227 filename: "an… image.jpg"
230 {:ok, data} = Upload.store(file)
231 [attachment_url | _] = data["url"]
233 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
236 test "escapes reserved uri characters" do
237 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
240 content_type: "image/jpg",
241 path: Path.absname("test/fixtures/image_tmp.jpg"),
242 filename: ":?#[]@!$&\\'()*+,;=.jpg"
245 {:ok, data} = Upload.store(file)
246 [attachment_url | _] = data["url"]
248 assert Path.basename(attachment_url["href"]) ==
249 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
253 describe "Setting a custom base_url for uploaded media" do
254 setup do: clear_config([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
256 test "returns a media url with configured base_url" do
257 base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
259 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
262 content_type: "image/jpg",
263 path: Path.absname("test/fixtures/image_tmp.jpg"),
264 filename: "image.jpg"
267 {:ok, data} = Upload.store(file, base_url: base_url)
269 assert %{"url" => [%{"href" => url}]} = data
271 refute String.starts_with?(url, base_url <> "/media/")