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)
25 test "returns a media url" do
26 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
29 content_type: "image/jpg",
30 path: Path.absname("test/fixtures/image_tmp.jpg"),
34 {:ok, data} = Upload.store(file)
36 assert %{"url" => [%{"href" => url}]} = data
38 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
41 test "copies the file to the configured folder with deduping" do
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"),
47 filename: "an [image.jpg"
50 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
52 assert List.first(data["url"])["href"] ==
53 Pleroma.Web.base_url() <>
54 "/media/e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpg"
57 test "copies the file to the configured folder without 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)
67 assert data["name"] == "an [image.jpg"
70 test "fixes incorrect content type" do
71 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
74 content_type: "application/octet-stream",
75 path: Path.absname("test/fixtures/image_tmp.jpg"),
76 filename: "an [image.jpg"
79 {:ok, data} = Upload.store(file, filters: [Pleroma.Upload.Filter.Dedupe])
80 assert hd(data["url"])["mediaType"] == "image/jpeg"
83 test "adds missing extension" do
84 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
87 content_type: "image/jpg",
88 path: Path.absname("test/fixtures/image_tmp.jpg"),
92 {:ok, data} = Upload.store(file)
93 assert data["name"] == "an [image.jpg"
96 test "fixes incorrect file extension" do
97 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
100 content_type: "image/jpg",
101 path: Path.absname("test/fixtures/image_tmp.jpg"),
102 filename: "an [image.blah"
105 {:ok, data} = Upload.store(file)
106 assert data["name"] == "an [image.jpg"
109 test "don't modify filename of an unknown type" do
110 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
113 content_type: "text/plain",
114 path: Path.absname("test/fixtures/test_tmp.txt"),
118 {:ok, data} = Upload.store(file)
119 assert data["name"] == "test.txt"