1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UploadTest do
9 describe "Storing a file with the Local uploader" do
10 setup [:ensure_local_uploader]
12 test "returns a media url" do
13 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
16 content_type: "image/jpg",
17 path: Path.absname("test/fixtures/image_tmp.jpg"),
21 {:ok, data} = Upload.store(file)
23 assert %{"url" => [%{"href" => url}]} = data
25 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
28 test "returns a media url with configured base_url" do
29 base_url = "https://cache.pleroma.social"
31 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
34 content_type: "image/jpg",
35 path: Path.absname("test/fixtures/image_tmp.jpg"),
39 {:ok, data} = Upload.store(file, base_url: base_url)
41 assert %{"url" => [%{"href" => url}]} = data
43 assert String.starts_with?(url, base_url <> "/media/")
46 test "copies the file to the configured folder with deduping" do
47 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
50 content_type: "image/jpg",
51 path: Path.absname("test/fixtures/image_tmp.jpg"),
52 filename: "an [image.jpg"
55 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
57 assert List.first(data["url"])["href"] ==
58 Pleroma.Web.base_url() <>
59 "/media/e30397b58d226d6583ab5b8b3c5defb0c682bda5c31ef07a9f57c1c4986e3781.jpg"
62 test "copies the file to the configured folder without deduping" do
63 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
66 content_type: "image/jpg",
67 path: Path.absname("test/fixtures/image_tmp.jpg"),
68 filename: "an [image.jpg"
71 {:ok, data} = Upload.store(file)
72 assert data["name"] == "an [image.jpg"
75 test "fixes incorrect content type" do
76 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
79 content_type: "application/octet-stream",
80 path: Path.absname("test/fixtures/image_tmp.jpg"),
81 filename: "an [image.jpg"
84 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
85 assert hd(data["url"])["mediaType"] == "image/jpeg"
88 test "adds missing extension" do
89 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
92 content_type: "image/jpg",
93 path: Path.absname("test/fixtures/image_tmp.jpg"),
97 {:ok, data} = Upload.store(file)
98 assert data["name"] == "an [image.jpg"
101 test "fixes incorrect file extension" do
102 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
105 content_type: "image/jpg",
106 path: Path.absname("test/fixtures/image_tmp.jpg"),
107 filename: "an [image.blah"
110 {:ok, data} = Upload.store(file)
111 assert data["name"] == "an [image.jpg"
114 test "don't modify filename of an unknown type" do
115 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
118 content_type: "text/plain",
119 path: Path.absname("test/fixtures/test_tmp.txt"),
123 {:ok, data} = Upload.store(file)
124 assert data["name"] == "test.txt"
127 test "copies the file to the configured folder with anonymizing filename" do
128 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
131 content_type: "image/jpg",
132 path: Path.absname("test/fixtures/image_tmp.jpg"),
133 filename: "an [image.jpg"
136 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.AnonymizeFilename])
138 refute data["name"] == "an [image.jpg"
141 test "escapes invalid characters in url" do
142 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
145 content_type: "image/jpg",
146 path: Path.absname("test/fixtures/image_tmp.jpg"),
147 filename: "an… image.jpg"
150 {:ok, data} = Upload.store(file)
151 [attachment_url | _] = data["url"]
153 assert Path.basename(attachment_url["href"]) == "an%E2%80%A6%20image.jpg"
156 test "escapes reserved uri characters" do
157 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
160 content_type: "image/jpg",
161 path: Path.absname("test/fixtures/image_tmp.jpg"),
162 filename: ":?#[]@!$&\\'()*+,;=.jpg"
165 {:ok, data} = Upload.store(file)
166 [attachment_url | _] = data["url"]
168 assert Path.basename(attachment_url["href"]) ==
169 "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"