Allow dashes in domain name search
[akkoma] / test / pleroma / uploaders / s3_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Uploaders.S3Test do
6 use Pleroma.DataCase
7
8 alias Pleroma.Uploaders.S3
9
10 import Mock
11 import ExUnit.CaptureLog
12
13 setup do
14 clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.S3)
15 clear_config([Pleroma.Upload, :base_url], "https://s3.amazonaws.com")
16 clear_config([Pleroma.Uploaders.S3])
17 clear_config([Pleroma.Uploaders.S3, :bucket], "test_bucket")
18 end
19
20 describe "get_file/1" do
21 test "it returns path to local folder for files" do
22 assert S3.get_file("test_image.jpg") == {
23 :ok,
24 {:url, "https://s3.amazonaws.com/test_bucket/test_image.jpg"}
25 }
26 end
27
28 test "it returns path without bucket when truncated_namespace set to ''" do
29 clear_config([Pleroma.Uploaders.S3],
30 bucket: "test_bucket",
31 bucket_namespace: "myaccount",
32 truncated_namespace: ""
33 )
34
35 clear_config([Pleroma.Upload, :base_url], "https://s3.amazonaws.com")
36
37 assert S3.get_file("test_image.jpg") == {
38 :ok,
39 {:url, "https://s3.amazonaws.com/test_image.jpg"}
40 }
41 end
42
43 test "it returns path with bucket namespace when namespace is set" do
44 clear_config([Pleroma.Uploaders.S3],
45 bucket: "test_bucket",
46 bucket_namespace: "family"
47 )
48
49 assert S3.get_file("test_image.jpg") == {
50 :ok,
51 {:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
52 }
53 end
54 end
55
56 describe "put_file/1" do
57 setup do
58 file_upload = %Pleroma.Upload{
59 name: "image-tet.jpg",
60 content_type: "image/jpeg",
61 path: "test_folder/image-tet.jpg",
62 tempfile: Path.absname("test/instance_static/add/shortcode.png")
63 }
64
65 [file_upload: file_upload]
66 end
67
68 test "save file", %{file_upload: file_upload} do
69 with_mock ExAws, request: fn _ -> {:ok, :ok} end do
70 assert S3.put_file(file_upload) == {:ok, {:file, "test_folder/image-tet.jpg"}}
71 end
72 end
73
74 test "returns error", %{file_upload: file_upload} do
75 with_mock ExAws, request: fn _ -> {:error, "S3 Upload failed"} end do
76 assert capture_log(fn ->
77 assert S3.put_file(file_upload) == {:error, "S3 Upload failed"}
78 end) =~ "Elixir.Pleroma.Uploaders.S3: {:error, \"S3 Upload failed\"}"
79 end
80 end
81 end
82
83 describe "delete_file/1" do
84 test_with_mock "deletes file", ExAws, request: fn _req -> {:ok, %{status_code: 204}} end do
85 assert :ok = S3.delete_file("image.jpg")
86 assert_called(ExAws.request(:_))
87 end
88 end
89 end