Merge branch 'fix/admin-api-user-deletion' into 'develop'
[akkoma] / test / upload_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UploadTest do
6 use Pleroma.DataCase
7
8 import ExUnit.CaptureLog
9
10 alias Pleroma.Upload
11 alias Pleroma.Uploaders.Uploader
12
13 @upload_file %Plug.Upload{
14 content_type: "image/jpg",
15 path: Path.absname("test/fixtures/image_tmp.jpg"),
16 filename: "image.jpg"
17 }
18
19 defmodule TestUploaderBase do
20 def put_file(%{path: path} = _upload, module_name) do
21 task_pid =
22 Task.async(fn ->
23 :timer.sleep(10)
24
25 {Uploader, path}
26 |> :global.whereis_name()
27 |> send({Uploader, self(), {:test}, %{}})
28
29 assert_receive {Uploader, {:test}}, 4_000
30 end)
31
32 Agent.start(fn -> task_pid end, name: module_name)
33
34 :wait_callback
35 end
36 end
37
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"}}
42
43 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
44 end
45
46 setup do: [uploader: TestUploaderSuccess]
47 setup [:ensure_local_uploader]
48
49 test "it returns file" do
50 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
51
52 assert Upload.store(@upload_file) ==
53 {:ok,
54 %{
55 "name" => "image.jpg",
56 "type" => "Document",
57 "url" => [
58 %{
59 "href" => "http://localhost:4001/media/post-process-file.jpg",
60 "mediaType" => "image/jpeg",
61 "type" => "Link"
62 }
63 ]
64 }}
65
66 Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
67 end
68 end
69
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"}
73
74 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
75 end
76
77 setup do: [uploader: TestUploaderError]
78 setup [:ensure_local_uploader]
79
80 test "it returns error" do
81 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
82
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))
86 end) =~
87 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploaderError) failed: \"Errors\""
88 end
89 end
90
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]
95
96 test "it returns error" do
97 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
98
99 assert capture_log(fn ->
100 assert Upload.store(@upload_file) == {:error, "Uploader callback timeout"}
101 end) =~
102 "[error] Elixir.Pleroma.Upload store (using Pleroma.UploadTest.TestUploader) failed: \"Uploader callback timeout\""
103 end
104 end
105
106 describe "Storing a file with the Local uploader" do
107 setup [:ensure_local_uploader]
108
109 test "returns a media url" do
110 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
111
112 file = %Plug.Upload{
113 content_type: "image/jpg",
114 path: Path.absname("test/fixtures/image_tmp.jpg"),
115 filename: "image.jpg"
116 }
117
118 {:ok, data} = Upload.store(file)
119
120 assert %{"url" => [%{"href" => url}]} = data
121
122 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
123 end
124
125 test "copies the file to the configured folder with deduping" do
126 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
127
128 file = %Plug.Upload{
129 content_type: "image/jpg",
130 path: Path.absname("test/fixtures/image_tmp.jpg"),
131 filename: "an [image.jpg"
132 }
133
134 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
135
136 assert List.first(data["url"])["href"] ==
137 Pleroma.Web.base_url() <>
138 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
139 end
140
141 test "copies the file to the configured folder without deduping" do
142 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
143
144 file = %Plug.Upload{
145 content_type: "image/jpg",
146 path: Path.absname("test/fixtures/image_tmp.jpg"),
147 filename: "an [image.jpg"
148 }
149
150 {:ok, data} = Upload.store(file)
151 assert data["name"] == "an [image.jpg"
152 end
153
154 test "fixes incorrect content type" do
155 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
156
157 file = %Plug.Upload{
158 content_type: "application/octet-stream",
159 path: Path.absname("test/fixtures/image_tmp.jpg"),
160 filename: "an [image.jpg"
161 }
162
163 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
164 assert hd(data["url"])["mediaType"] == "image/jpeg"
165 end
166
167 test "adds missing extension" do
168 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
169
170 file = %Plug.Upload{
171 content_type: "image/jpg",
172 path: Path.absname("test/fixtures/image_tmp.jpg"),
173 filename: "an [image"
174 }
175
176 {:ok, data} = Upload.store(file)
177 assert data["name"] == "an [image.jpg"
178 end
179
180 test "fixes incorrect file extension" do
181 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
182
183 file = %Plug.Upload{
184 content_type: "image/jpg",
185 path: Path.absname("test/fixtures/image_tmp.jpg"),
186 filename: "an [image.blah"
187 }
188
189 {:ok, data} = Upload.store(file)
190 assert data["name"] == "an [image.jpg"
191 end
192
193 test "don't modify filename of an unknown type" do
194 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
195
196 file = %Plug.Upload{
197 content_type: "text/plain",
198 path: Path.absname("test/fixtures/test_tmp.txt"),
199 filename: "test.txt"
200 }
201
202 {:ok, data} = Upload.store(file)
203 assert data["name"] == "test.txt"
204 end
205
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")
208
209 file = %Plug.Upload{
210 content_type: "image/jpg",
211 path: Path.absname("test/fixtures/image_tmp.jpg"),
212 filename: "an [image.jpg"
213 }
214
215 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
216
217 refute data["name"] == "an [image.jpg"
218 end
219
220 test "escapes invalid characters in url" do
221 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
222
223 file = %Plug.Upload{
224 content_type: "image/jpg",
225 path: Path.absname("test/fixtures/image_tmp.jpg"),
226 filename: "an… image.jpg"
227 }
228
229 {:ok, data} = Upload.store(file)
230 [attachment_url | _] = data["url"]
231
232 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
233 end
234
235 test "escapes reserved uri characters" do
236 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
237
238 file = %Plug.Upload{
239 content_type: "image/jpg",
240 path: Path.absname("test/fixtures/image_tmp.jpg"),
241 filename: ":?#[]@!$&\\'()*+,;=.jpg"
242 }
243
244 {:ok, data} = Upload.store(file)
245 [attachment_url | _] = data["url"]
246
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"
249 end
250 end
251
252 describe "Setting a custom base_url for uploaded media" do
253 clear_config([Pleroma.Upload, :base_url]) do
254 Pleroma.Config.put([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
255 end
256
257 test "returns a media url with configured base_url" do
258 base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
259
260 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
261
262 file = %Plug.Upload{
263 content_type: "image/jpg",
264 path: Path.absname("test/fixtures/image_tmp.jpg"),
265 filename: "image.jpg"
266 }
267
268 {:ok, data} = Upload.store(file, base_url: base_url)
269
270 assert %{"url" => [%{"href" => url}]} = data
271
272 refute String.starts_with?(url, base_url <> "/media/")
273 end
274 end
275 end