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.Uploaders.S3,
16 bucket: "test_bucket",
17 public_endpoint: "https://s3.amazonaws.com"
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 Config.put([Pleroma.Uploaders.S3],
30 bucket: "test_bucket",
31 public_endpoint: "https://s3.amazonaws.com",
32 truncated_namespace: ""
35 assert S3.get_file("test_image.jpg") == {
37 {:url, "https://s3.amazonaws.com/test_image.jpg"}
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"
48 assert S3.get_file("test_image.jpg") == {
50 {:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
55 describe "put_file/1" 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/fixtures/image_tmp.jpg")
64 [file_upload: file_upload]
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"}}
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\"}"
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(:_))