998245b29da93de4fad2f7aa478025dc6f75854e
[akkoma] / test / upload_test.exs
1 defmodule Pleroma.UploadTest do
2 alias Pleroma.Upload
3 use Pleroma.DataCase
4
5 describe "Storing a file with the Local uploader" do
6 setup do
7 uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
8
9 unless uploader == Pleroma.Uploaders.Local do
10 on_exit(fn ->
11 Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
12 end)
13 end
14
15 :ok
16 end
17
18 test "returns a media url" do
19 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
20
21 file = %Plug.Upload{
22 content_type: "image/jpg",
23 path: Path.absname("test/fixtures/image_tmp.jpg"),
24 filename: "image.jpg"
25 }
26
27 {:ok, data} = Upload.store(file)
28
29 assert %{"url" => [%{"href" => url}]} = data
30
31 assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
32 end
33
34 test "copies the file to the configured folder with deduping" do
35 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
36
37 file = %Plug.Upload{
38 content_type: "image/jpg",
39 path: Path.absname("test/fixtures/image_tmp.jpg"),
40 filename: "an [image.jpg"
41 }
42
43 {:ok, data} = Upload.store(file, dedupe: true)
44
45 assert data["name"] ==
46 "e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpeg"
47 end
48
49 test "copies the file to the configured folder without deduping" do
50 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
51
52 file = %Plug.Upload{
53 content_type: "image/jpg",
54 path: Path.absname("test/fixtures/image_tmp.jpg"),
55 filename: "an [image.jpg"
56 }
57
58 {:ok, data} = Upload.store(file, dedupe: false)
59 assert data["name"] == "an [image.jpg"
60 end
61
62 test "fixes incorrect content type" do
63 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
64
65 file = %Plug.Upload{
66 content_type: "application/octet-stream",
67 path: Path.absname("test/fixtures/image_tmp.jpg"),
68 filename: "an [image.jpg"
69 }
70
71 {:ok, data} = Upload.store(file, dedupe: true)
72 assert hd(data["url"])["mediaType"] == "image/jpeg"
73 end
74
75 test "adds missing extension" do
76 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
77
78 file = %Plug.Upload{
79 content_type: "image/jpg",
80 path: Path.absname("test/fixtures/image_tmp.jpg"),
81 filename: "an [image"
82 }
83
84 {:ok, data} = Upload.store(file, dedupe: false)
85 assert data["name"] == "an [image.jpg"
86 end
87
88 test "fixes incorrect file extension" do
89 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
90
91 file = %Plug.Upload{
92 content_type: "image/jpg",
93 path: Path.absname("test/fixtures/image_tmp.jpg"),
94 filename: "an [image.blah"
95 }
96
97 {:ok, data} = Upload.store(file, dedupe: false)
98 assert data["name"] == "an [image.jpg"
99 end
100
101 test "don't modify filename of an unknown type" do
102 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
103
104 file = %Plug.Upload{
105 content_type: "text/plain",
106 path: Path.absname("test/fixtures/test_tmp.txt"),
107 filename: "test.txt"
108 }
109
110 {:ok, data} = Upload.store(file, dedupe: false)
111 assert data["name"] == "test.txt"
112 end
113 end
114 end