1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Uploaders.S3Test do
8 alias Pleroma.Uploaders.S3
11 import ExUnit.CaptureLog
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")
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") == {
24 {:url, "https://s3.amazonaws.com/test_bucket/test_image.jpg"}
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: ""
35 clear_config([Pleroma.Upload, :base_url], "https://s3.amazonaws.com")
37 assert S3.get_file("test_image.jpg") == {
39 {:url, "https://s3.amazonaws.com/test_image.jpg"}
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"
49 assert S3.get_file("test_image.jpg") == {
51 {:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
56 describe "put_file/1" 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")
65 [file_upload: file_upload]
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"}}
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\"}"
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(:_))