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