Merge branch 'feature/signed-object-fetches' 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 alias Pleroma.Upload
9 alias Pleroma.Uploaders.Uploader
10
11 @upload_file %Plug.Upload{
12 content_type: "image/jpg",
13 path: Path.absname("test/fixtures/image_tmp.jpg"),
14 filename: "image.jpg"
15 }
16
17 defmodule TestUploaderBase do
18 def put_file(%{path: path} = _upload, module_name) do
19 task_pid =
20 Task.async(fn ->
21 :timer.sleep(10)
22
23 {Uploader, path}
24 |> :global.whereis_name()
25 |> send({Uploader, self(), {:test}, %{}})
26
27 assert_receive {Uploader, {:test}}, 4_000
28 end)
29
30 Agent.start(fn -> task_pid end, name: module_name)
31
32 :wait_callback
33 end
34 end
35
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"}}
40
41 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
42 end
43
44 setup do: [uploader: TestUploaderSuccess]
45 setup [:ensure_local_uploader]
46
47 test "it returns file" do
48 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
49
50 assert Upload.store(@upload_file) ==
51 {:ok,
52 %{
53 "name" => "image.jpg",
54 "type" => "Document",
55 "url" => [
56 %{
57 "href" => "http://localhost:4001/media/post-process-file.jpg",
58 "mediaType" => "image/jpeg",
59 "type" => "Link"
60 }
61 ]
62 }}
63
64 Task.await(Agent.get(TestUploaderSuccess, fn task_pid -> task_pid end))
65 end
66 end
67
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"}
71
72 def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
73 end
74
75 setup do: [uploader: TestUploaderError]
76 setup [:ensure_local_uploader]
77
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))
82 end
83 end
84
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]
89
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"}
93 end
94 end
95
96 describe "Storing a file with the Local uploader" do
97 setup [:ensure_local_uploader]
98
99 test "returns a media url" do
100 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
101
102 file = %Plug.Upload{
103 content_type: "image/jpg",
104 path: Path.absname("test/fixtures/image_tmp.jpg"),
105 filename: "image.jpg"
106 }
107
108 {:ok, data} = Upload.store(file)
109
110 assert %{"url" => [%{"href" => url}]} = data
111
112 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
113 end
114
115 test "returns a media url with configured base_url" do
116 base_url = "https://cache.pleroma.social"
117
118 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
119
120 file = %Plug.Upload{
121 content_type: "image/jpg",
122 path: Path.absname("test/fixtures/image_tmp.jpg"),
123 filename: "image.jpg"
124 }
125
126 {:ok, data} = Upload.store(file, base_url: base_url)
127
128 assert %{"url" => [%{"href" => url}]} = data
129
130 assert String.starts_with?(url, base_url <> "/media/")
131 end
132
133 test "copies the file to the configured folder with deduping" do
134 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
135
136 file = %Plug.Upload{
137 content_type: "image/jpg",
138 path: Path.absname("test/fixtures/image_tmp.jpg"),
139 filename: "an [image.jpg"
140 }
141
142 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
143
144 assert List.first(data["url"])["href"] ==
145 Pleroma.Web.base_url() <>
146 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
147 end
148
149 test "copies the file to the configured folder without deduping" do
150 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
151
152 file = %Plug.Upload{
153 content_type: "image/jpg",
154 path: Path.absname("test/fixtures/image_tmp.jpg"),
155 filename: "an [image.jpg"
156 }
157
158 {:ok, data} = Upload.store(file)
159 assert data["name"] == "an [image.jpg"
160 end
161
162 test "fixes incorrect content type" do
163 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
164
165 file = %Plug.Upload{
166 content_type: "application/octet-stream",
167 path: Path.absname("test/fixtures/image_tmp.jpg"),
168 filename: "an [image.jpg"
169 }
170
171 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
172 assert hd(data["url"])["mediaType"] == "image/jpeg"
173 end
174
175 test "adds missing extension" do
176 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
177
178 file = %Plug.Upload{
179 content_type: "image/jpg",
180 path: Path.absname("test/fixtures/image_tmp.jpg"),
181 filename: "an [image"
182 }
183
184 {:ok, data} = Upload.store(file)
185 assert data["name"] == "an [image.jpg"
186 end
187
188 test "fixes incorrect file extension" do
189 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
190
191 file = %Plug.Upload{
192 content_type: "image/jpg",
193 path: Path.absname("test/fixtures/image_tmp.jpg"),
194 filename: "an [image.blah"
195 }
196
197 {:ok, data} = Upload.store(file)
198 assert data["name"] == "an [image.jpg"
199 end
200
201 test "don't modify filename of an unknown type" do
202 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
203
204 file = %Plug.Upload{
205 content_type: "text/plain",
206 path: Path.absname("test/fixtures/test_tmp.txt"),
207 filename: "test.txt"
208 }
209
210 {:ok, data} = Upload.store(file)
211 assert data["name"] == "test.txt"
212 end
213
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")
216
217 file = %Plug.Upload{
218 content_type: "image/jpg",
219 path: Path.absname("test/fixtures/image_tmp.jpg"),
220 filename: "an [image.jpg"
221 }
222
223 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
224
225 refute data["name"] == "an [image.jpg"
226 end
227
228 test "escapes invalid characters in url" do
229 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
230
231 file = %Plug.Upload{
232 content_type: "image/jpg",
233 path: Path.absname("test/fixtures/image_tmp.jpg"),
234 filename: "an… image.jpg"
235 }
236
237 {:ok, data} = Upload.store(file)
238 [attachment_url | _] = data["url"]
239
240 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
241 end
242
243 test "escapes reserved uri characters" do
244 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
245
246 file = %Plug.Upload{
247 content_type: "image/jpg",
248 path: Path.absname("test/fixtures/image_tmp.jpg"),
249 filename: ":?#[]@!$&\\'()*+,;=.jpg"
250 }
251
252 {:ok, data} = Upload.store(file)
253 [attachment_url | _] = data["url"]
254
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"
257 end
258 end
259 end