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 "returns a media url with configured base_url" do
126 base_url = "https://cache.pleroma.social"
128 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
131 content_type: "image/jpg",
132 path: Path.absname("test/fixtures/image_tmp.jpg"),
133 filename: "image.jpg"
136 {:ok, data} = Upload.store(file, base_url: base_url)
138 assert %{"url" => [%{"href" => url}]} = data
140 assert String.starts_with?(url, base_url <> "/media/")
143 test "copies the file to the configured folder with deduping" do
144 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
147 content_type: "image/jpg",
148 path: Path.absname("test/fixtures/image_tmp.jpg"),
149 filename: "an [image.jpg"
152 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
154 assert List.first(data["url"])["href"] ==
155 Pleroma.Web.base_url() <>
156 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
159 test "copies the file to the configured folder without deduping" do
160 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
163 content_type: "image/jpg",
164 path: Path.absname("test/fixtures/image_tmp.jpg"),
165 filename: "an [image.jpg"
168 {:ok, data} = Upload.store(file)
169 assert data["name"] == "an [image.jpg"
172 test "fixes incorrect content type" do
173 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
176 content_type: "application/octet-stream",
177 path: Path.absname("test/fixtures/image_tmp.jpg"),
178 filename: "an [image.jpg"
181 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
182 assert hd(data["url"])["mediaType"] == "image/jpeg"
185 test "adds missing extension" do
186 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
189 content_type: "image/jpg",
190 path: Path.absname("test/fixtures/image_tmp.jpg"),
191 filename: "an [image"
194 {:ok, data} = Upload.store(file)
195 assert data["name"] == "an [image.jpg"
198 test "fixes incorrect file extension" do
199 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
202 content_type: "image/jpg",
203 path: Path.absname("test/fixtures/image_tmp.jpg"),
204 filename: "an [image.blah"
207 {:ok, data} = Upload.store(file)
208 assert data["name"] == "an [image.jpg"
211 test "don't modify filename of an unknown type" do
212 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
215 content_type: "text/plain",
216 path: Path.absname("test/fixtures/test_tmp.txt"),
220 {:ok, data} = Upload.store(file)
221 assert data["name"] == "test.txt"
224 test "copies the file to the configured folder with anonymizing filename" do
225 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
228 content_type: "image/jpg",
229 path: Path.absname("test/fixtures/image_tmp.jpg"),
230 filename: "an [image.jpg"
233 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
235 refute data["name"] == "an [image.jpg"
238 test "escapes invalid characters in url" do
239 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
242 content_type: "image/jpg",
243 path: Path.absname("test/fixtures/image_tmp.jpg"),
244 filename: "an… image.jpg"
247 {:ok, data} = Upload.store(file)
248 [attachment_url | _] = data["url"]
250 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
253 test "escapes reserved uri characters" do
254 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
257 content_type: "image/jpg",
258 path: Path.absname("test/fixtures/image_tmp.jpg"),
259 filename: ":?#[]@!$&\\'()*+,;=.jpg"
262 {:ok, data} = Upload.store(file)
263 [attachment_url | _] = data["url"]
265 assert Path.basename(attachment_url["href"]) ==
266 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"