1 defmodule Pleroma.UploadTest do
5 describe "Storing a file with the Local uploader" do
7 uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
8 filters = Pleroma.Config.get([Pleroma.Upload, :filters])
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], [])
15 Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
16 Pleroma.Config.put([Pleroma.Upload, :filters], filters)
23 test "returns a media url" do
24 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
27 content_type: "image/jpg",
28 path: Path.absname("test/fixtures/image_tmp.jpg"),
32 {:ok, data} = Upload.store(file)
34 assert %{"url" => [%{"href" => url}]} = data
36 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
39 test "returns a media url with configured base_url" do
40 base_url = "https://cache.pleroma.social"
42 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
45 content_type: "image/jpg",
46 path: Path.absname("test/fixtures/image_tmp.jpg"),
50 {:ok, data} = Upload.store(file, base_url: base_url)
52 assert %{"url" => [%{"href" => url}]} = data
54 assert String.starts_with?(url, base_url <> "/media/")
57 test "copies the file to the configured folder with deduping" do
58 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
61 content_type: "image/jpg",
62 path: Path.absname("test/fixtures/image_tmp.jpg"),
63 filename: "an [image.jpg"
66 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
68 assert List.first(data["url"])["href"] ==
69 Pleroma.Web.base_url() <>
70 "/media/e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpg"
73 test "copies the file to the configured folder without deduping" do
74 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
77 content_type: "image/jpg",
78 path: Path.absname("test/fixtures/image_tmp.jpg"),
79 filename: "an [image.jpg"
82 {:ok, data} = Upload.store(file)
83 assert data["name"] == "an [image.jpg"
86 test "fixes incorrect content type" do
87 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
90 content_type: "application/octet-stream",
91 path: Path.absname("test/fixtures/image_tmp.jpg"),
92 filename: "an [image.jpg"
95 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
96 assert hd(data["url"])["mediaType"] == "image/jpeg"
99 test "adds missing extension" do
100 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
103 content_type: "image/jpg",
104 path: Path.absname("test/fixtures/image_tmp.jpg"),
105 filename: "an [image"
108 {:ok, data} = Upload.store(file)
109 assert data["name"] == "an [image.jpg"
112 test "fixes incorrect file extension" do
113 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
116 content_type: "image/jpg",
117 path: Path.absname("test/fixtures/image_tmp.jpg"),
118 filename: "an [image.blah"
121 {:ok, data} = Upload.store(file)
122 assert data["name"] == "an [image.jpg"
125 test "don't modify filename of an unknown type" do
126 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
129 content_type: "text/plain",
130 path: Path.absname("test/fixtures/test_tmp.txt"),
134 {:ok, data} = Upload.store(file)
135 assert data["name"] == "test.txt"
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")
142 content_type: "image/jpg",
143 path: Path.absname("test/fixtures/image_tmp.jpg"),
144 filename: "an [image.jpg"
147 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
149 refute data["name"] == "an [image.jpg"