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