1 defmodule Pleroma.UploadTest do
5 describe "Storing a file with the Local uploader" do
6 setup [:ensure_local_uploader]
8 test "returns a media url" do
9 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
12 content_type: "image/jpg",
13 path: Path.absname("test/fixtures/image_tmp.jpg"),
17 {:ok, data} = Upload.store(file)
19 assert %{"url" => [%{"href" => url}]} = data
21 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
24 test "returns a media url with configured base_url" do
25 base_url = "https://cache.pleroma.social"
27 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
30 content_type: "image/jpg",
31 path: Path.absname("test/fixtures/image_tmp.jpg"),
35 {:ok, data} = Upload.store(file, base_url: base_url)
37 assert %{"url" => [%{"href" => url}]} = data
39 assert String.starts_with?(url, base_url <> "/media/")
42 test "copies the file to the configured folder with deduping" do
43 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
46 content_type: "image/jpg",
47 path: Path.absname("test/fixtures/image_tmp.jpg"),
48 filename: "an [image.jpg"
51 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
53 assert List.first(data["url"])["href"] ==
54 Pleroma.Web.base_url() <>
55 "/media/e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpg"
58 test "copies the file to the configured folder without deduping" do
59 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
62 content_type: "image/jpg",
63 path: Path.absname("test/fixtures/image_tmp.jpg"),
64 filename: "an [image.jpg"
67 {:ok, data} = Upload.store(file)
68 assert data["name"] == "an [image.jpg"
71 test "fixes incorrect content type" do
72 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
75 content_type: "application/octet-stream",
76 path: Path.absname("test/fixtures/image_tmp.jpg"),
77 filename: "an [image.jpg"
80 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
81 assert hd(data["url"])["mediaType"] == "image/jpeg"
84 test "adds missing extension" do
85 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
88 content_type: "image/jpg",
89 path: Path.absname("test/fixtures/image_tmp.jpg"),
93 {:ok, data} = Upload.store(file)
94 assert data["name"] == "an [image.jpg"
97 test "fixes incorrect file extension" do
98 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
101 content_type: "image/jpg",
102 path: Path.absname("test/fixtures/image_tmp.jpg"),
103 filename: "an [image.blah"
106 {:ok, data} = Upload.store(file)
107 assert data["name"] == "an [image.jpg"
110 test "don't modify filename of an unknown type" do
111 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
114 content_type: "text/plain",
115 path: Path.absname("test/fixtures/test_tmp.txt"),
119 {:ok, data} = Upload.store(file)
120 assert data["name"] == "test.txt"
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")
127 content_type: "image/jpg",
128 path: Path.absname("test/fixtures/image_tmp.jpg"),
129 filename: "an [image.jpg"
132 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
134 refute data["name"] == "an [image.jpg"