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