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
9 alias Pleroma.Uploaders.Uploader
11 @upload_file %Plug.Upload{
12 content_type: "image/jpg",
13 path: Path.absname("test/fixtures/image_tmp.jpg"),
17 defmodule TestUploaderBase do
18 def put_file(%{path: path} = _upload, module_name) do
24 |> :global.whereis_name()
25 |> send({Uploader, self(), {:test}, %{}})
27 assert_receive {Uploader, {:test}}, 4_000
30 Agent.start(fn -> task_pid end, name: module_name)
36 describe "Tried storing a file when http callback response success result" do
37 defmodule TestUploaderSuccess do
38 def http_callback(conn, _params),
39 do: {:ok, conn, {:file, "post-process-file.jpg"}}
41 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
44 setup do: [uploader: TestUploaderSuccess]
45 setup [:ensure_local_uploader]
47 test "it returns file" do
48 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
50 assert Upload.store(@upload_file) ==
53 "name" => "image.jpg",
57 "href" => "http://localhost:4001/media/post-process-file.jpg",
58 "mediaType" => "image/jpeg",
64 Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
68 describe "Tried storing a file when http callback response error" do
69 defmodule TestUploaderError do
70 def http_callback(conn, _params), do: {:error, conn, "Errors"}
72 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
75 setup do: [uploader: TestUploaderError]
76 setup [:ensure_local_uploader]
78 test "it returns error" do
79 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
80 assert Upload.store(@upload_file) == {:error, "Errors"}
81 Task.await(Agent.get(TestUploaderError, fn task_pid -> task_pid end))
85 describe "Tried storing a file when http callback doesn't response by timeout" do
86 defmodule(TestUploader, do: def(put_file(_upload), do: :wait_callback))
87 setup do: [uploader: TestUploader]
88 setup [:ensure_local_uploader]
90 test "it returns error" do
91 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
92 assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
96 describe "Storing a file with the Local uploader" do
97 setup [:ensure_local_uploader]
99 test "returns a media url" do
100 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
103 content_type: "image/jpg",
104 path: Path.absname("test/fixtures/image_tmp.jpg"),
105 filename: "image.jpg"
108 {:ok, data} = Upload.store(file)
110 assert %{"url" => [%{"href" => url}]} = data
112 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
115 test "returns a media url with configured base_url" do
116 base_url = "https://cache.pleroma.social"
118 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
121 content_type: "image/jpg",
122 path: Path.absname("test/fixtures/image_tmp.jpg"),
123 filename: "image.jpg"
126 {:ok, data} = Upload.store(file, base_url: base_url)
128 assert %{"url" => [%{"href" => url}]} = data
130 assert String.starts_with?(url, base_url <> "/media/")
133 test "copies the file to the configured folder with deduping" do
134 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
137 content_type: "image/jpg",
138 path: Path.absname("test/fixtures/image_tmp.jpg"),
139 filename: "an [image.jpg"
142 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
144 assert List.first(data["url"])["href"] ==
145 Pleroma.Web.base_url() <>
146 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
149 test "copies the file to the configured folder without deduping" do
150 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
153 content_type: "image/jpg",
154 path: Path.absname("test/fixtures/image_tmp.jpg"),
155 filename: "an [image.jpg"
158 {:ok, data} = Upload.store(file)
159 assert data["name"] == "an [image.jpg"
162 test "fixes incorrect content type" do
163 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
166 content_type: "application/octet-stream",
167 path: Path.absname("test/fixtures/image_tmp.jpg"),
168 filename: "an [image.jpg"
171 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
172 assert hd(data["url"])["mediaType"] == "image/jpeg"
175 test "adds missing extension" do
176 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
179 content_type: "image/jpg",
180 path: Path.absname("test/fixtures/image_tmp.jpg"),
181 filename: "an [image"
184 {:ok, data} = Upload.store(file)
185 assert data["name"] == "an [image.jpg"
188 test "fixes incorrect file extension" do
189 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
192 content_type: "image/jpg",
193 path: Path.absname("test/fixtures/image_tmp.jpg"),
194 filename: "an [image.blah"
197 {:ok, data} = Upload.store(file)
198 assert data["name"] == "an [image.jpg"
201 test "don't modify filename of an unknown type" do
202 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
205 content_type: "text/plain",
206 path: Path.absname("test/fixtures/test_tmp.txt"),
210 {:ok, data} = Upload.store(file)
211 assert data["name"] == "test.txt"
214 test "copies the file to the configured folder with anonymizing filename" do
215 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
218 content_type: "image/jpg",
219 path: Path.absname("test/fixtures/image_tmp.jpg"),
220 filename: "an [image.jpg"
223 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
225 refute data["name"] == "an [image.jpg"
228 test "escapes invalid characters in url" do
229 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
232 content_type: "image/jpg",
233 path: Path.absname("test/fixtures/image_tmp.jpg"),
234 filename: "an… image.jpg"
237 {:ok, data} = Upload.store(file)
238 [attachment_url | _] = data["url"]
240 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
243 test "escapes reserved uri characters" do
244 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
247 content_type: "image/jpg",
248 path: Path.absname("test/fixtures/image_tmp.jpg"),
249 filename: ":?#[]@!$&\\'()*+,;=.jpg"
252 {:ok, data} = Upload.store(file)
253 [attachment_url | _] = data["url"]
255 assert Path.basename(attachment_url["href"]) ==
256 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"