branch
[akkoma] / test / uploaders / s3_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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]) do
15 Config.put([Pleroma.Uploaders.S3],
16 bucket: "test_bucket",
17 public_endpoint: "https://s3.amazonaws.com"
18 )
19 end
20
21 describe "get_file/1" do
22 test "it returns path to local folder for files" do
23 assert S3.get_file("test_image.jpg") == {
24 :ok,
25 {:url, "https://s3.amazonaws.com/test_bucket/test_image.jpg"}
26 }
27 end
28
29 test "it returns path without bucket when truncated_namespace set to ''" do
30 Config.put([Pleroma.Uploaders.S3],
31 bucket: "test_bucket",
32 public_endpoint: "https://s3.amazonaws.com",
33 truncated_namespace: ""
34 )
35
36 assert S3.get_file("test_image.jpg") == {
37 :ok,
38 {:url, "https://s3.amazonaws.com/test_image.jpg"}
39 }
40 end
41
42 test "it returns path with bucket namespace when namespace is set" do
43 Config.put([Pleroma.Uploaders.S3],
44 bucket: "test_bucket",
45 public_endpoint: "https://s3.amazonaws.com",
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/jpg",
61 path: "test_folder/image-tet.jpg",
62 tempfile: Path.absname("test/fixtures/image_tmp.jpg")
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 end