all tests passed
[akkoma] / test / upload_test.exs
1 defmodule Pleroma.UploadTest do
2 alias Pleroma.Upload
3 use Pleroma.DataCase
4
5 describe "Storing a file with the Local uploader" do
6 setup do
7 uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
8 filters = Pleroma.Config.get([Pleroma.Upload, :filters])
9
10 unless uploader == Pleroma.Uploaders.Local || filters != [] do
11 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
12 Pleroma.Config.put([Pleroma.Upload, :filters], [])
13
14 on_exit(fn ->
15 Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
16 Pleroma.Config.put([Pleroma.Upload, :filters], filters)
17 end)
18 end
19
20 :ok
21 end
22
23 test "returns a media url" do
24 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
25
26 file = %Plug.Upload{
27 content_type: "image/jpg",
28 path: Path.absname("test/fixtures/image_tmp.jpg"),
29 filename: "image.jpg"
30 }
31
32 {:ok, data} = Upload.store(file)
33
34 assert %{"url" => [%{"href" => url}]} = data
35
36 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
37 end
38
39 test "returns a media url with configured base_url" do
40 base_url = "https://cache.pleroma.social"
41
42 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
43
44 file = %Plug.Upload{
45 content_type: "image/jpg",
46 path: Path.absname("test/fixtures/image_tmp.jpg"),
47 filename: "image.jpg"
48 }
49
50 {:ok, data} = Upload.store(file, base_url: base_url)
51
52 assert %{"url" => [%{"href" => url}]} = data
53
54 assert String.starts_with?(url, base_url <> "/media/")
55 end
56
57 test "copies the file to the configured folder with deduping" do
58 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
59
60 file = %Plug.Upload{
61 content_type: "image/jpg",
62 path: Path.absname("test/fixtures/image_tmp.jpg"),
63 filename: "an [image.jpg"
64 }
65
66 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
67
68 assert List.first(data["url"])["href"] ==
69 Pleroma.Web.base_url() <>
70 "/media/e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpg"
71 end
72
73 test "copies the file to the configured folder without deduping" do
74 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
75
76 file = %Plug.Upload{
77 content_type: "image/jpg",
78 path: Path.absname("test/fixtures/image_tmp.jpg"),
79 filename: "an [image.jpg"
80 }
81
82 {:ok, data} = Upload.store(file)
83 assert data["name"] == "an [image.jpg"
84 end
85
86 test "fixes incorrect content type" do
87 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
88
89 file = %Plug.Upload{
90 content_type: "application/octet-stream",
91 path: Path.absname("test/fixtures/image_tmp.jpg"),
92 filename: "an [image.jpg"
93 }
94
95 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
96 assert hd(data["url"])["mediaType"] == "image/jpeg"
97 end
98
99 test "adds missing extension" 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: "an [image"
106 }
107
108 {:ok, data} = Upload.store(file)
109 assert data["name"] == "an [image.jpg"
110 end
111
112 test "fixes incorrect file extension" do
113 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
114
115 file = %Plug.Upload{
116 content_type: "image/jpg",
117 path: Path.absname("test/fixtures/image_tmp.jpg"),
118 filename: "an [image.blah"
119 }
120
121 {:ok, data} = Upload.store(file)
122 assert data["name"] == "an [image.jpg"
123 end
124
125 test "don't modify filename of an unknown type" do
126 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
127
128 file = %Plug.Upload{
129 content_type: "text/plain",
130 path: Path.absname("test/fixtures/test_tmp.txt"),
131 filename: "test.txt"
132 }
133
134 {:ok, data} = Upload.store(file)
135 assert data["name"] == "test.txt"
136 end
137
138 test "copies the file to the configured folder with anonymizing filename" do
139 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
140
141 file = %Plug.Upload{
142 content_type: "image/jpg",
143 path: Path.absname("test/fixtures/image_tmp.jpg"),
144 filename: "an [image.jpg"
145 }
146
147 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
148
149 refute data["name"] == "an [image.jpg"
150 end
151 end
152 end