Merge branch 'fix/redirect-on-user-fetch' into 'develop'
[akkoma] / test / upload_test.exs
1 defmodule Pleroma.UploadTest do
2 alias Pleroma.Upload
3 use Pleroma.DataCase
4
5 describe "Storing a file" do
6 test "copies the file to the configured folder with deduping" do
7 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
8
9 file = %Plug.Upload{
10 content_type: "image/jpg",
11 path: Path.absname("test/fixtures/image_tmp.jpg"),
12 filename: "an [image.jpg"
13 }
14
15 data = Upload.store(file, true)
16
17 assert data["name"] ==
18 "e7a6d0cf595bff76f14c9a98b6c199539559e8b844e02e51e5efcfd1f614a2df.jpeg"
19 end
20
21 test "copies the file to the configured folder without deduping" do
22 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
23
24 file = %Plug.Upload{
25 content_type: "image/jpg",
26 path: Path.absname("test/fixtures/image_tmp.jpg"),
27 filename: "an [image.jpg"
28 }
29
30 data = Upload.store(file, false)
31 assert data["name"] == "an [image.jpg"
32 end
33
34 test "fixes incorrect content type" do
35 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
36
37 file = %Plug.Upload{
38 content_type: "application/octet-stream",
39 path: Path.absname("test/fixtures/image_tmp.jpg"),
40 filename: "an [image.jpg"
41 }
42
43 data = Upload.store(file, true)
44 assert hd(data["url"])["mediaType"] == "image/jpeg"
45 end
46
47 test "adds missing extension" do
48 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
49
50 file = %Plug.Upload{
51 content_type: "image/jpg",
52 path: Path.absname("test/fixtures/image_tmp.jpg"),
53 filename: "an [image"
54 }
55
56 data = Upload.store(file, false)
57 assert data["name"] == "an [image.jpg"
58 end
59
60 test "fixes incorrect file extension" do
61 File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
62
63 file = %Plug.Upload{
64 content_type: "image/jpg",
65 path: Path.absname("test/fixtures/image_tmp.jpg"),
66 filename: "an [image.blah"
67 }
68
69 data = Upload.store(file, false)
70 assert data["name"] == "an [image.jpg"
71 end
72
73 test "don't modify filename of an unknown type" do
74 File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
75
76 file = %Plug.Upload{
77 content_type: "text/plain",
78 path: Path.absname("test/fixtures/test_tmp.txt"),
79 filename: "test.txt"
80 }
81
82 data = Upload.store(file, false)
83 assert data["name"] == "test.txt"
84 end
85 end
86 end