1 defmodule Pleroma.UploadTest do
5 describe "Storing a file with the Local uploader" do
7 uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
9 unless uploader == Pleroma.Uploaders.Local do
11 Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
18 test "returns a media url" do
19 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
22 content_type: "image/jpg",
23 path: Path.absname("test/fixtures/image_tmp.jpg"),
27 {:ok, data} = Upload.store(file)
29 assert %{"url" => [%{"href" => url}]} = data
31 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
34 test "copies the file to the configured folder with deduping" do
35 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
38 content_type: "image/jpg",
39 path: Path.absname("test/fixtures/image_tmp.jpg"),
40 filename: "an [image.jpg"
43 {:ok, data} = Upload.store(file, dedupe: true)
45 assert data["name"] ==
46 "e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpeg"
49 test "copies the file to the configured folder without deduping" do
50 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
53 content_type: "image/jpg",
54 path: Path.absname("test/fixtures/image_tmp.jpg"),
55 filename: "an [image.jpg"
58 {:ok, data} = Upload.store(file, dedupe: false)
59 assert data["name"] == "an [image.jpg"
62 test "fixes incorrect content type" do
63 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
66 content_type: "application/octet-stream",
67 path: Path.absname("test/fixtures/image_tmp.jpg"),
68 filename: "an [image.jpg"
71 {:ok, data} = Upload.store(file, dedupe: true)
72 assert hd(data["url"])["mediaType"] == "image/jpeg"
75 test "adds missing extension" do
76 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
79 content_type: "image/jpg",
80 path: Path.absname("test/fixtures/image_tmp.jpg"),
84 {:ok, data} = Upload.store(file, dedupe: false)
85 assert data["name"] == "an [image.jpg"
88 test "fixes incorrect file 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"),
94 filename: "an [image.blah"
97 {:ok, data} = Upload.store(file, dedupe: false)
98 assert data["name"] == "an [image.jpg"
101 test "don't modify filename of an unknown type" do
102 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
105 content_type: "text/plain",
106 path: Path.absname("test/fixtures/test_tmp.txt"),
110 {:ok, data} = Upload.store(file, dedupe: false)
111 assert data["name"] == "test.txt"