Handle new-style mastodon follow lists
[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 alias Pleroma.Upload
7 use Pleroma.DataCase
8
9 describe "Storing a file with the Local uploader" do
10 setup [:ensure_local_uploader]
11
12 test "returns a media url" do
13 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
14
15 file = %Plug.Upload{
16 content_type: "image/jpg",
17 path: Path.absname("test/fixtures/image_tmp.jpg"),
18 filename: "image.jpg"
19 }
20
21 {:ok, data} = Upload.store(file)
22
23 assert %{"url" => [%{"href" => url}]} = data
24
25 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
26 end
27
28 test "returns a media url with configured base_url" do
29 base_url = "https://cache.pleroma.social"
30
31 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
32
33 file = %Plug.Upload{
34 content_type: "image/jpg",
35 path: Path.absname("test/fixtures/image_tmp.jpg"),
36 filename: "image.jpg"
37 }
38
39 {:ok, data} = Upload.store(file, base_url: base_url)
40
41 assert %{"url" => [%{"href" => url}]} = data
42
43 assert String.starts_with?(url, base_url <> "/media/")
44 end
45
46 test "copies the file to the configured folder with deduping" do
47 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
48
49 file = %Plug.Upload{
50 content_type: "image/jpg",
51 path: Path.absname("test/fixtures/image_tmp.jpg"),
52 filename: "an [image.jpg"
53 }
54
55 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
56
57 assert List.first(data["url"])["href"] ==
58 Pleroma.Web.base_url() <>
59 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
60 end
61
62 test "copies the file to the configured folder without deduping" do
63 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
64
65 file = %Plug.Upload{
66 content_type: "image/jpg",
67 path: Path.absname("test/fixtures/image_tmp.jpg"),
68 filename: "an [image.jpg"
69 }
70
71 {:ok, data} = Upload.store(file)
72 assert data["name"] == "an [image.jpg"
73 end
74
75 test "fixes incorrect content type" do
76 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
77
78 file = %Plug.Upload{
79 content_type: "application/octet-stream",
80 path: Path.absname("test/fixtures/image_tmp.jpg"),
81 filename: "an [image.jpg"
82 }
83
84 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
85 assert hd(data["url"])["mediaType"] == "image/jpeg"
86 end
87
88 test "adds missing extension" do
89 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
90
91 file = %Plug.Upload{
92 content_type: "image/jpg",
93 path: Path.absname("test/fixtures/image_tmp.jpg"),
94 filename: "an [image"
95 }
96
97 {:ok, data} = Upload.store(file)
98 assert data["name"] == "an [image.jpg"
99 end
100
101 test "fixes incorrect file extension" do
102 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
103
104 file = %Plug.Upload{
105 content_type: "image/jpg",
106 path: Path.absname("test/fixtures/image_tmp.jpg"),
107 filename: "an [image.blah"
108 }
109
110 {:ok, data} = Upload.store(file)
111 assert data["name"] == "an [image.jpg"
112 end
113
114 test "don't modify filename of an unknown type" do
115 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
116
117 file = %Plug.Upload{
118 content_type: "text/plain",
119 path: Path.absname("test/fixtures/test_tmp.txt"),
120 filename: "test.txt"
121 }
122
123 {:ok, data} = Upload.store(file)
124 assert data["name"] == "test.txt"
125 end
126
127 test "copies the file to the configured folder with anonymizing filename" do
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: "an [image.jpg"
134 }
135
136 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
137
138 refute data["name"] == "an [image.jpg"
139 end
140
141 test "escapes invalid characters in url" 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 [attachment_url | _] = data["url"]
152
153 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
154 end
155
156 test "escapes reserved uri characters" do
157 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
158
159 file = %Plug.Upload{
160 content_type: "image/jpg",
161 path: Path.absname("test/fixtures/image_tmp.jpg"),
162 filename: ":?#[]@!$&\\'()*+,;=.jpg"
163 }
164
165 {:ok, data} = Upload.store(file)
166 [attachment_url | _] = data["url"]
167
168 assert Path.basename(attachment_url["href"]) ==
169 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
170 end
171 end
172 end