2022-04-11 13:10:01 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Uploaders.IPFSTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
|
|
|
alias Pleroma.Uploaders.IPFS
|
2022-06-28 15:53:44 +00:00
|
|
|
alias Tesla.Multipart
|
2022-04-11 13:10:01 +00:00
|
|
|
|
|
|
|
import ExUnit.CaptureLog
|
2024-05-27 13:22:18 +00:00
|
|
|
import Mock
|
|
|
|
import Mox
|
2022-04-11 13:10:01 +00:00
|
|
|
|
2024-05-27 13:22:18 +00:00
|
|
|
alias Pleroma.UnstubbedConfigMock, as: Config
|
2022-04-11 13:10:01 +00:00
|
|
|
|
2024-05-27 13:22:18 +00:00
|
|
|
describe "get_final_url" do
|
|
|
|
setup do
|
|
|
|
Config
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
|
|
|
|
[post_gateway_url: "http://localhost:5001"]
|
|
|
|
end)
|
2022-04-11 13:10:01 +00:00
|
|
|
|
2024-05-27 13:22:18 +00:00
|
|
|
:ok
|
|
|
|
end
|
2022-04-11 13:10:01 +00:00
|
|
|
|
2022-05-09 10:15:40 +00:00
|
|
|
test "it returns the final url for put_file" do
|
2022-06-09 17:24:13 +00:00
|
|
|
assert IPFS.put_file_endpoint() == "http://localhost:5001/api/v0/add"
|
2022-05-09 10:15:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns the final url for delete_file" do
|
2022-06-09 17:24:13 +00:00
|
|
|
assert IPFS.delete_file_endpoint() == "http://localhost:5001/api/v0/files/rm"
|
2022-05-09 10:15:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-11 13:10:01 +00:00
|
|
|
describe "get_file/1" do
|
2024-05-27 13:22:18 +00:00
|
|
|
setup do
|
|
|
|
Config
|
|
|
|
|> expect(:get, fn [Pleroma.Upload, :uploader] -> Pleroma.Uploaders.IPFS end)
|
|
|
|
|> expect(:get, fn [Pleroma.Upload, :base_url] -> nil end)
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :public_endpoint] -> nil end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2022-04-11 13:10:01 +00:00
|
|
|
test "it returns path to ipfs file with cid as subdomain" do
|
2024-05-27 13:22:18 +00:00
|
|
|
Config
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :get_gateway_url] ->
|
|
|
|
"https://{CID}.ipfs.mydomain.com"
|
|
|
|
end)
|
|
|
|
|
2022-04-11 13:10:01 +00:00
|
|
|
assert IPFS.get_file("testcid") == {
|
|
|
|
:ok,
|
|
|
|
{:url, "https://testcid.ipfs.mydomain.com"}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns path to ipfs file with cid as path" do
|
2024-05-27 13:22:18 +00:00
|
|
|
Config
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :get_gateway_url] ->
|
2022-04-16 07:38:49 +00:00
|
|
|
"https://ipfs.mydomain.com/ipfs/{CID}"
|
2024-05-27 13:22:18 +00:00
|
|
|
end)
|
2022-04-11 13:10:01 +00:00
|
|
|
|
|
|
|
assert IPFS.get_file("testcid") == {
|
|
|
|
:ok,
|
|
|
|
{:url, "https://ipfs.mydomain.com/ipfs/testcid"}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "put_file/1" do
|
|
|
|
setup do
|
2024-05-27 13:22:18 +00:00
|
|
|
Config
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
|
|
|
|
[post_gateway_url: "http://localhost:5001"]
|
|
|
|
end)
|
|
|
|
|
2022-04-11 13:10:01 +00:00
|
|
|
file_upload = %Pleroma.Upload{
|
|
|
|
name: "image-tet.jpg",
|
|
|
|
content_type: "image/jpeg",
|
|
|
|
path: "test_folder/image-tet.jpg",
|
|
|
|
tempfile: Path.absname("test/instance_static/add/shortcode.png")
|
|
|
|
}
|
|
|
|
|
2022-06-28 15:53:44 +00:00
|
|
|
mp =
|
|
|
|
Multipart.new()
|
|
|
|
|> Multipart.add_content_type_param("charset=utf-8")
|
|
|
|
|> Multipart.add_file(file_upload.tempfile)
|
|
|
|
|
|
|
|
[file_upload: file_upload, mp: mp]
|
2022-04-11 13:10:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "save file", %{file_upload: file_upload} do
|
|
|
|
with_mock Pleroma.HTTP,
|
2024-05-27 13:22:18 +00:00
|
|
|
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
2022-04-11 13:10:01 +00:00
|
|
|
{:ok,
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
2022-05-09 10:15:40 +00:00
|
|
|
body:
|
|
|
|
"{\"Name\":\"image-tet.jpg\",\"Size\":\"5000\", \"Hash\":\"bafybeicrh7ltzx52yxcwrvxxckfmwhqdgsb6qym6dxqm2a4ymsakeshwoi\"}"
|
2022-04-11 13:10:01 +00:00
|
|
|
}}
|
|
|
|
end do
|
|
|
|
assert IPFS.put_file(file_upload) ==
|
|
|
|
{:ok, {:file, "bafybeicrh7ltzx52yxcwrvxxckfmwhqdgsb6qym6dxqm2a4ymsakeshwoi"}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns error", %{file_upload: file_upload} do
|
2022-06-28 15:53:44 +00:00
|
|
|
with_mock Pleroma.HTTP,
|
2024-05-27 13:22:18 +00:00
|
|
|
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
2022-06-28 15:53:44 +00:00
|
|
|
{:error, "IPFS Gateway upload failed"}
|
|
|
|
end do
|
2022-04-11 13:10:01 +00:00
|
|
|
assert capture_log(fn ->
|
|
|
|
assert IPFS.put_file(file_upload) == {:error, "IPFS Gateway upload failed"}
|
|
|
|
end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"IPFS Gateway upload failed\"}"
|
|
|
|
end
|
|
|
|
end
|
2022-06-09 17:24:13 +00:00
|
|
|
|
|
|
|
test "returns error if JSON decode fails", %{file_upload: file_upload} do
|
2022-06-28 15:53:44 +00:00
|
|
|
with_mock Pleroma.HTTP, [],
|
2024-05-27 13:22:18 +00:00
|
|
|
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
2022-07-08 08:06:46 +00:00
|
|
|
{:ok, %Tesla.Env{status: 200, body: "invalid"}}
|
2022-06-28 15:53:44 +00:00
|
|
|
end do
|
2022-06-09 17:24:13 +00:00
|
|
|
assert capture_log(fn ->
|
|
|
|
assert IPFS.put_file(file_upload) == {:error, "JSON decode failed"}
|
2022-06-28 15:53:44 +00:00
|
|
|
end) =~
|
2024-05-27 13:22:18 +00:00
|
|
|
"Elixir.Pleroma.Uploaders.IPFS: {:error, %Jason.DecodeError"
|
2022-06-09 17:24:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
|
2022-06-28 15:53:44 +00:00
|
|
|
with_mock Pleroma.HTTP, [],
|
2024-05-27 13:22:18 +00:00
|
|
|
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
2022-07-08 08:06:46 +00:00
|
|
|
{:ok, %Tesla.Env{status: 200, body: "{\"key\": \"value\"}"}}
|
2022-06-28 15:53:44 +00:00
|
|
|
end do
|
2022-06-09 17:24:13 +00:00
|
|
|
assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}
|
|
|
|
end
|
|
|
|
end
|
2022-04-11 13:10:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "delete_file/1" do
|
2024-05-27 13:22:18 +00:00
|
|
|
setup do
|
|
|
|
Config
|
|
|
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
|
|
|
|
[post_gateway_url: "http://localhost:5001"]
|
|
|
|
end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2022-04-11 13:10:01 +00:00
|
|
|
test_with_mock "deletes file", Pleroma.HTTP,
|
2022-06-28 15:53:44 +00:00
|
|
|
post: fn "http://localhost:5001/api/v0/files/rm", "", [], params: [arg: "image.jpg"] ->
|
2024-05-27 13:22:18 +00:00
|
|
|
{:ok, %{status: 204}}
|
2022-06-28 15:53:44 +00:00
|
|
|
end do
|
2022-04-11 13:10:01 +00:00
|
|
|
assert :ok = IPFS.delete_file("image.jpg")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|