2019-08-10 11:27:59 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-08-10 11:27:59 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Uploaders.S3Test do
|
2023-12-11 05:25:05 +00:00
|
|
|
use Pleroma.DataCase, async: true
|
2019-08-10 11:27:59 +00:00
|
|
|
|
2023-12-11 07:28:39 +00:00
|
|
|
alias Pleroma.UnstubbedConfigMock, as: ConfigMock
|
2019-08-10 11:27:59 +00:00
|
|
|
alias Pleroma.Uploaders.S3
|
2023-12-11 05:25:05 +00:00
|
|
|
alias Pleroma.Uploaders.S3.ExAwsMock
|
2019-08-10 11:27:59 +00:00
|
|
|
|
2023-12-11 05:25:05 +00:00
|
|
|
import Mox
|
2019-08-10 11:27:59 +00:00
|
|
|
import ExUnit.CaptureLog
|
|
|
|
|
|
|
|
describe "get_file/1" do
|
2023-12-11 05:25:05 +00:00
|
|
|
test "it returns url for files" do
|
|
|
|
ConfigMock
|
|
|
|
|> expect(:get, 6, fn key ->
|
|
|
|
[
|
|
|
|
{Pleroma.Upload,
|
|
|
|
[uploader: Pleroma.Uploaders.S3, base_url: "https://s3.amazonaws.com"]},
|
|
|
|
{Pleroma.Uploaders.S3, [bucket: "test_bucket"]}
|
|
|
|
]
|
|
|
|
|> get_in(key)
|
|
|
|
end)
|
|
|
|
|
2019-08-10 11:27:59 +00:00
|
|
|
assert S3.get_file("test_image.jpg") == {
|
|
|
|
:ok,
|
|
|
|
{:url, "https://s3.amazonaws.com/test_bucket/test_image.jpg"}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns path without bucket when truncated_namespace set to ''" do
|
2023-12-11 05:25:05 +00:00
|
|
|
ConfigMock
|
|
|
|
|> expect(:get, 6, fn key ->
|
|
|
|
[
|
|
|
|
{Pleroma.Upload,
|
|
|
|
[uploader: Pleroma.Uploaders.S3, base_url: "https://s3.amazonaws.com"]},
|
|
|
|
{Pleroma.Uploaders.S3,
|
|
|
|
[bucket: "test_bucket", truncated_namespace: "", bucket_namespace: "myaccount"]}
|
|
|
|
]
|
|
|
|
|> get_in(key)
|
|
|
|
end)
|
2021-01-12 22:35:10 +00:00
|
|
|
|
2019-08-10 11:27:59 +00:00
|
|
|
assert S3.get_file("test_image.jpg") == {
|
|
|
|
:ok,
|
|
|
|
{:url, "https://s3.amazonaws.com/test_image.jpg"}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns path with bucket namespace when namespace is set" do
|
2023-12-11 05:25:05 +00:00
|
|
|
ConfigMock
|
|
|
|
|> expect(:get, 6, fn key ->
|
|
|
|
[
|
|
|
|
{Pleroma.Upload,
|
|
|
|
[uploader: Pleroma.Uploaders.S3, base_url: "https://s3.amazonaws.com"]},
|
|
|
|
{Pleroma.Uploaders.S3, [bucket: "test_bucket", bucket_namespace: "family"]}
|
|
|
|
]
|
|
|
|
|> get_in(key)
|
|
|
|
end)
|
2019-08-10 11:27:59 +00:00
|
|
|
|
|
|
|
assert S3.get_file("test_image.jpg") == {
|
|
|
|
:ok,
|
|
|
|
{:url, "https://s3.amazonaws.com/family:test_bucket/test_image.jpg"}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "put_file/1" do
|
|
|
|
setup do
|
|
|
|
file_upload = %Pleroma.Upload{
|
|
|
|
name: "image-tet.jpg",
|
2020-10-13 15:37:24 +00:00
|
|
|
content_type: "image/jpeg",
|
2019-08-10 11:27:59 +00:00
|
|
|
path: "test_folder/image-tet.jpg",
|
2020-05-08 15:51:16 +00:00
|
|
|
tempfile: Path.absname("test/instance_static/add/shortcode.png")
|
2019-08-10 11:27:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 05:25:05 +00:00
|
|
|
ConfigMock
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.S3] ->
|
|
|
|
[
|
|
|
|
bucket: "test_bucket"
|
|
|
|
]
|
|
|
|
end)
|
|
|
|
|
2019-08-10 11:27:59 +00:00
|
|
|
[file_upload: file_upload]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "save file", %{file_upload: file_upload} do
|
2023-12-11 05:25:05 +00:00
|
|
|
ExAwsMock
|
|
|
|
|> expect(:request, fn _req -> {:ok, %{status_code: 200}} end)
|
|
|
|
|
|
|
|
assert S3.put_file(file_upload) == {:ok, {:file, "test_folder/image-tet.jpg"}}
|
2019-08-10 11:27:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns error", %{file_upload: file_upload} do
|
2023-12-11 05:25:05 +00:00
|
|
|
ExAwsMock
|
|
|
|
|> expect(:request, fn _req -> {:error, "S3 Upload failed"} end)
|
|
|
|
|
|
|
|
assert capture_log(fn ->
|
|
|
|
assert S3.put_file(file_upload) == {:error, "S3 Upload failed"}
|
|
|
|
end) =~ "Elixir.Pleroma.Uploaders.S3: {:error, \"S3 Upload failed\"}"
|
2019-08-10 11:27:59 +00:00
|
|
|
end
|
|
|
|
end
|
2020-01-12 18:48:58 +00:00
|
|
|
|
|
|
|
describe "delete_file/1" do
|
2023-12-11 05:25:05 +00:00
|
|
|
test "deletes file" do
|
|
|
|
ExAwsMock
|
|
|
|
|> expect(:request, fn _req -> {:ok, %{status_code: 204}} end)
|
|
|
|
|
|
|
|
ConfigMock
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.S3, :bucket] -> "test_bucket" end)
|
|
|
|
|
2020-01-12 18:48:58 +00:00
|
|
|
assert :ok = S3.delete_file("image.jpg")
|
|
|
|
end
|
|
|
|
end
|
2019-08-10 11:27:59 +00:00
|
|
|
end
|