Merge branch 'length-limit-bio' into 'develop'
[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 setup do
15 config = Config.get([Pleroma.Uploaders.S3])
16
17 Config.put([Pleroma.Uploaders.S3],
18 bucket: "test_bucket",
19 public_endpoint: "https://s3.amazonaws.com"
20 )
21
22 on_exit(fn ->
23 Config.put([Pleroma.Uploaders.S3], config)
24 end)
25
26 :ok
27 end
28
29 describe "get_file/1" do
30 test "it returns path to local folder for files" do
31 assert S3.get_file("test_image.jpg") == {
32 :ok,
33 {:url, "https://s3.amazonaws.com/test_bucket/test_image.jpg"}
34 }
35 end
36
37 test "it returns path without bucket when truncated_namespace set to ''" do
38 Config.put([Pleroma.Uploaders.S3],
39 bucket: "test_bucket",
40 public_endpoint: "https://s3.amazonaws.com",
41 truncated_namespace: ""
42 )
43
44 assert S3.get_file("test_image.jpg") == {
45 :ok,
46 {:url, "https://s3.amazonaws.com/test_image.jpg"}
47 }
48 end
49
50 test "it returns path with bucket namespace when namespace is set" do
51 Config.put([Pleroma.Uploaders.S3],
52 bucket: "test_bucket",
53 public_endpoint: "https://s3.amazonaws.com",
54 bucket_namespace: "family"
55 )
56
57 assert S3.get_file("test_image.jpg") == {
58 :ok,
59 {:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
60 }
61 end
62 end
63
64 describe "put_file/1" do
65 setup do
66 file_upload = %Pleroma.Upload{
67 name: "image-tet.jpg",
68 content_type: "image/jpg",
69 path: "test_folder/image-tet.jpg",
70 tempfile: Path.absname("test/fixtures/image_tmp.jpg")
71 }
72
73 [file_upload: file_upload]
74 end
75
76 test "save file", %{file_upload: file_upload} do
77 with_mock ExAws, request: fn _ -> {:ok, :ok} end do
78 assert S3.put_file(file_upload) == {:ok, {:file, "test_folder/image-tet.jpg"}}
79 end
80 end
81
82 test "returns error", %{file_upload: file_upload} do
83 with_mock ExAws, request: fn _ -> {:error, "S3 Upload failed"} end do
84 assert capture_log(fn ->
85 assert S3.put_file(file_upload) == {:error, "S3 Upload failed"}
86 end) =~ "Elixir.Pleroma.Uploaders.S3: {:error, \"S3 Upload failed\"}"
87 end
88 end
89 end
90 end