1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Uploaders.S3Test do
9 alias Pleroma.Uploaders.S3
12 import ExUnit.CaptureLog
15 clear_config(Pleroma.Upload,
16 uploader: Pleroma.Uploaders.S3
19 clear_config(Pleroma.Upload,
20 base_url: "https://s3.amazonaws.com"
23 clear_config(Pleroma.Uploaders.S3,
28 describe "get_file/1" do
29 test "it returns path to local folder for files" do
30 assert S3.get_file("test_image.jpg") == {
32 {:url, "https://s3.amazonaws.com/test_bucket/test_image.jpg"}
36 test "it returns path without bucket when truncated_namespace set to ''" do
37 Config.put([Pleroma.Uploaders.S3],
38 bucket: "test_bucket",
39 truncated_namespace: ""
42 Config.put([Pleroma.Upload], base_url: "https://s3.amazonaws.com")
44 assert S3.get_file("test_image.jpg") == {
46 {:url, "https://s3.amazonaws.com/test_image.jpg"}
50 test "it returns path with bucket namespace when namespace is set" do
51 Config.put([Pleroma.Uploaders.S3],
52 bucket: "test_bucket",
53 bucket_namespace: "family"
56 Config.put([Pleroma.Upload], base_url: "https://s3.amazonaws.com")
58 assert S3.get_file("test_image.jpg") == {
60 {:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
65 describe "put_file/1" do
67 file_upload = %Pleroma.Upload{
68 name: "image-tet.jpg",
69 content_type: "image/jpeg",
70 path: "test_folder/image-tet.jpg",
71 tempfile: Path.absname("test/instance_static/add/shortcode.png")
74 [file_upload: file_upload]
77 test "save file", %{file_upload: file_upload} do
78 with_mock ExAws, request: fn _ -> {:ok, :ok} end do
79 assert S3.put_file(file_upload) == {:ok, {:file, "test_folder/image-tet.jpg"}}
83 test "returns error", %{file_upload: file_upload} do
84 with_mock ExAws, request: fn _ -> {:error, "S3 Upload failed"} end do
85 assert capture_log(fn ->
86 assert S3.put_file(file_upload) == {:error, "S3 Upload failed"}
87 end) =~ "Elixir.Pleroma.Uploaders.S3: {:error, \"S3 Upload failed\"}"
92 describe "delete_file/1" do
93 test_with_mock "deletes file", ExAws, request: fn _req -> {:ok, %{status_code: 204}} end do
94 assert :ok = S3.delete_file("image.jpg")
95 assert_called(ExAws.request(:_))