Merge branch '1112-user-insert-conflict-id-preservation' 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 "returns a media url with configured base_url" do
126 base_url = "https://cache.pleroma.social"
127
128 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
129
130 file = %Plug.Upload{
131 content_type: "image/jpg",
132 path: Path.absname("test/fixtures/image_tmp.jpg"),
133 filename: "image.jpg"
134 }
135
136 {:ok, data} = Upload.store(file, base_url: base_url)
137
138 assert %{"url" => [%{"href" => url}]} = data
139
140 assert String.starts_with?(url, base_url <> "/media/")
141 end
142
143 test "copies the file to the configured folder with deduping" do
144 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
145
146 file = %Plug.Upload{
147 content_type: "image/jpg",
148 path: Path.absname("test/fixtures/image_tmp.jpg"),
149 filename: "an [image.jpg"
150 }
151
152 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
153
154 assert List.first(data["url"])["href"] ==
155 Pleroma.Web.base_url() <>
156 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
157 end
158
159 test "copies the file to the configured folder without deduping" do
160 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
161
162 file = %Plug.Upload{
163 content_type: "image/jpg",
164 path: Path.absname("test/fixtures/image_tmp.jpg"),
165 filename: "an [image.jpg"
166 }
167
168 {:ok, data} = Upload.store(file)
169 assert data["name"] == "an [image.jpg"
170 end
171
172 test "fixes incorrect content type" do
173 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
174
175 file = %Plug.Upload{
176 content_type: "application/octet-stream",
177 path: Path.absname("test/fixtures/image_tmp.jpg"),
178 filename: "an [image.jpg"
179 }
180
181 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
182 assert hd(data["url"])["mediaType"] == "image/jpeg"
183 end
184
185 test "adds missing extension" do
186 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
187
188 file = %Plug.Upload{
189 content_type: "image/jpg",
190 path: Path.absname("test/fixtures/image_tmp.jpg"),
191 filename: "an [image"
192 }
193
194 {:ok, data} = Upload.store(file)
195 assert data["name"] == "an [image.jpg"
196 end
197
198 test "fixes incorrect file extension" do
199 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
200
201 file = %Plug.Upload{
202 content_type: "image/jpg",
203 path: Path.absname("test/fixtures/image_tmp.jpg"),
204 filename: "an [image.blah"
205 }
206
207 {:ok, data} = Upload.store(file)
208 assert data["name"] == "an [image.jpg"
209 end
210
211 test "don't modify filename of an unknown type" do
212 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
213
214 file = %Plug.Upload{
215 content_type: "text/plain",
216 path: Path.absname("test/fixtures/test_tmp.txt"),
217 filename: "test.txt"
218 }
219
220 {:ok, data} = Upload.store(file)
221 assert data["name"] == "test.txt"
222 end
223
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")
226
227 file = %Plug.Upload{
228 content_type: "image/jpg",
229 path: Path.absname("test/fixtures/image_tmp.jpg"),
230 filename: "an [image.jpg"
231 }
232
233 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
234
235 refute data["name"] == "an [image.jpg"
236 end
237
238 test "escapes invalid characters in url" do
239 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
240
241 file = %Plug.Upload{
242 content_type: "image/jpg",
243 path: Path.absname("test/fixtures/image_tmp.jpg"),
244 filename: "an… image.jpg"
245 }
246
247 {:ok, data} = Upload.store(file)
248 [attachment_url | _] = data["url"]
249
250 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
251 end
252
253 test "escapes reserved uri characters" do
254 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
255
256 file = %Plug.Upload{
257 content_type: "image/jpg",
258 path: Path.absname("test/fixtures/image_tmp.jpg"),
259 filename: ":?#[]@!$&\\'()*+,;=.jpg"
260 }
261
262 {:ok, data} = Upload.store(file)
263 [attachment_url | _] = data["url"]
264
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"
267 end
268 end
269 end