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