Merge branch 'fix/digest-task-otp' into 'develop'
[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 setup do:
15 clear_config(Pleroma.Uploaders.S3,
16 bucket: "test_bucket",
17 public_endpoint: "https://s3.amazonaws.com"
18 )
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 Config.put([Pleroma.Uploaders.S3],
30 bucket: "test_bucket",
31 public_endpoint: "https://s3.amazonaws.com",
32 truncated_namespace: ""
33 )
34
35 assert S3.get_file("test_image.jpg") == {
36 :ok,
37 {:url, "https://s3.amazonaws.com/test_image.jpg"}
38 }
39 end
40
41 test "it returns path with bucket namespace when namespace is set" do
42 Config.put([Pleroma.Uploaders.S3],
43 bucket: "test_bucket",
44 public_endpoint: "https://s3.amazonaws.com",
45 bucket_namespace: "family"
46 )
47
48 assert S3.get_file("test_image.jpg") == {
49 :ok,
50 {:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
51 }
52 end
53 end
54
55 describe "put_file/1" do
56 setup do
57 file_upload = %Pleroma.Upload{
58 name: "image-tet.jpg",
59 content_type: "image/jpg",
60 path: "test_folder/image-tet.jpg",
61 tempfile: Path.absname("test/instance_static/add/shortcode.png")
62 }
63
64 [file_upload: file_upload]
65 end
66
67 test "save file", %{file_upload: file_upload} do
68 with_mock ExAws, request: fn _ -> {:ok, :ok} end do
69 assert S3.put_file(file_upload) == {:ok, {:file, "test_folder/image-tet.jpg"}}
70 end
71 end
72
73 test "returns error", %{file_upload: file_upload} do
74 with_mock ExAws, request: fn _ -> {:error, "S3 Upload failed"} end do
75 assert capture_log(fn ->
76 assert S3.put_file(file_upload) == {:error, "S3 Upload failed"}
77 end) =~ "Elixir.Pleroma.Uploaders.S3: {:error, \"S3 Upload failed\"}"
78 end
79 end
80 end
81
82 describe "delete_file/1" do
83 test_with_mock "deletes file", ExAws, request: fn _req -> {:ok, %{status_code: 204}} end do
84 assert :ok = S3.delete_file("image.jpg")
85 assert_called(ExAws.request(:_))
86 end
87 end
88 end