1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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/jpeg",
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 {:ok, result} = Upload.store(@upload_file)
57 "name" => "image.jpg",
59 "mediaType" => "image/jpeg",
62 "href" => "http://localhost:4001/media/post-process-file.jpg",
63 "mediaType" => "image/jpeg",
69 Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
73 describe "Tried storing a file when http callback response error" do
74 defmodule TestUploaderError do
75 def http_callback(conn, _params), do: {:error, conn, "Errors"}
77 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
80 setup do: [uploader: TestUploaderError]
81 setup [:ensure_local_uploader]
83 test "it returns error" do
84 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
86 assert capture_log(fn ->
87 assert Upload.store(@upload_file) == {:error, "Errors"}
88 Task.await(Agent.get(TestUploaderError, fn task_pid -> task_pid end))
90 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploaderError) failed: \"Errors\""
94 describe "Tried storing a file when http callback doesn't response by timeout" do
95 defmodule(TestUploader, do: def(put_file(_upload), do: :wait_callback))
96 setup do: [uploader: TestUploader]
97 setup [:ensure_local_uploader]
99 test "it returns error" do
100 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
102 assert capture_log(fn ->
103 assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
105 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploader) failed: \"Uploader callback timeout\""
109 describe "Storing a file with the Local uploader" do
110 setup [:ensure_local_uploader]
112 test "does not allow descriptions longer than the post limit" do
113 clear_config([:instance, :description_limit], 2)
114 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
117 content_type: "image/jpeg",
118 path: Path.absname("test/fixtures/image_tmp.jpg"),
119 filename: "image.jpg"
122 {:error, :description_too_long} = Upload.store(file, description: "123")
125 test "returns a media url" do
126 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
129 content_type: "image/jpeg",
130 path: Path.absname("test/fixtures/image_tmp.jpg"),
131 filename: "image.jpg"
134 {:ok, data} = Upload.store(file)
136 assert %{"url" => [%{"href" => url}]} = data
138 assert String.starts_with?(url, Pleroma.Upload.base_url())
141 test "copies the file to the configured folder with deduping" do
142 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
145 content_type: "image/jpeg",
146 path: Path.absname("test/fixtures/image_tmp.jpg"),
147 filename: "an [image.jpg"
150 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
152 assert List.first(data["url"])["href"] ==
153 Pleroma.Upload.base_url() <>
154 "e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
157 test "copies the file to the configured folder without deduping" do
158 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
161 content_type: "image/jpeg",
162 path: Path.absname("test/fixtures/image_tmp.jpg"),
163 filename: "an [image.jpg"
166 {:ok, data} = Upload.store(file)
167 assert data["name"] == "an [image.jpg"
170 test "fixes incorrect content type when base64 is given" do
172 img: "data:image/png;base64,#{Base.encode64(File.read!("test/fixtures/image.jpg"))}"
175 {:ok, data} = Upload.store(params)
176 assert hd(data["url"])["mediaType"] == "image/jpeg"
179 test "adds extension when base64 is given" do
180 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
183 img: "data:image/png;base64,#{Base.encode64(File.read!("test/fixtures/image.jpg"))}"
186 {:ok, data} = Upload.store(params)
187 assert String.ends_with?(data["name"], ".jpg")
190 test "copies the file to the configured folder with anonymizing filename" do
191 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
194 content_type: "image/jpeg",
195 path: Path.absname("test/fixtures/image_tmp.jpg"),
196 filename: "an [image.jpg"
199 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
201 refute data["name"] == "an [image.jpg"
204 test "escapes invalid characters in url" do
205 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
208 content_type: "image/jpeg",
209 path: Path.absname("test/fixtures/image_tmp.jpg"),
210 filename: "an… image.jpg"
213 {:ok, data} = Upload.store(file)
214 [attachment_url | _] = data["url"]
216 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
219 test "escapes reserved uri characters" do
220 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
223 content_type: "image/jpeg",
224 path: Path.absname("test/fixtures/image_tmp.jpg"),
225 filename: ":?#[]@!$&\\'()*+,;=.jpg"
228 {:ok, data} = Upload.store(file)
229 [attachment_url | _] = data["url"]
231 assert Path.basename(attachment_url["href"]) ==
232 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
236 describe "Setting a custom base_url for uploaded media" do
237 setup do: clear_config([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
239 test "returns a media url with configured base_url" do
240 base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
242 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
245 content_type: "image/jpeg",
246 path: Path.absname("test/fixtures/image_tmp.jpg"),
247 filename: "image.jpg"
250 {:ok, data} = Upload.store(file, base_url: base_url)
252 assert %{"url" => [%{"href" => url}]} = data
254 refute String.starts_with?(url, base_url <> "/media/")