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 "copies the file to the configured folder with deduping" do
40 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
43 content_type: "image/jpg",
44 path: Path.absname("test/fixtures/image_tmp.jpg"),
45 filename: "an [image.jpg"
48 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
50 assert List.first(data["url"])["href"] ==
51 Pleroma.Web.base_url() <>
52 "/media/e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpg"
55 test "copies the file to the configured folder without deduping" do
56 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
59 content_type: "image/jpg",
60 path: Path.absname("test/fixtures/image_tmp.jpg"),
61 filename: "an [image.jpg"
64 {:ok, data} = Upload.store(file)
65 assert data["name"] == "an [image.jpg"
68 test "fixes incorrect content type" do
69 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
72 content_type: "application/octet-stream",
73 path: Path.absname("test/fixtures/image_tmp.jpg"),
74 filename: "an [image.jpg"
77 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
78 assert hd(data["url"])["mediaType"] == "image/jpeg"
81 test "adds missing extension" do
82 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
85 content_type: "image/jpg",
86 path: Path.absname("test/fixtures/image_tmp.jpg"),
90 {:ok, data} = Upload.store(file)
91 assert data["name"] == "an [image.jpg"
94 test "fixes incorrect file extension" do
95 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
98 content_type: "image/jpg",
99 path: Path.absname("test/fixtures/image_tmp.jpg"),
100 filename: "an [image.blah"
103 {:ok, data} = Upload.store(file)
104 assert data["name"] == "an [image.jpg"
107 test "don't modify filename of an unknown type" do
108 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
111 content_type: "text/plain",
112 path: Path.absname("test/fixtures/test_tmp.txt"),
116 {:ok, data} = Upload.store(file)
117 assert data["name"] == "test.txt"