IPFSTest: Fix configuration mocking

This commit is contained in:
Lain Soykaf 2024-05-27 17:22:18 +04:00
parent 825b4122a5
commit 3055c1598b
4 changed files with 55 additions and 25 deletions

View File

@ -153,6 +153,7 @@
config :pleroma, Pleroma.Upload, config_impl: Pleroma.UnstubbedConfigMock config :pleroma, Pleroma.Upload, config_impl: Pleroma.UnstubbedConfigMock
config :pleroma, Pleroma.ScheduledActivity, config_impl: Pleroma.UnstubbedConfigMock config :pleroma, Pleroma.ScheduledActivity, config_impl: Pleroma.UnstubbedConfigMock
config :pleroma, Pleroma.Web.RichMedia.Helpers, config_impl: Pleroma.StaticStubbedConfigMock config :pleroma, Pleroma.Web.RichMedia.Helpers, config_impl: Pleroma.StaticStubbedConfigMock
config :pleroma, Pleroma.Uploaders.IPFS, config_impl: Pleroma.UnstubbedConfigMock
peer_module = peer_module =
if String.to_integer(System.otp_release()) >= 25 do if String.to_integer(System.otp_release()) >= 25 do

View File

@ -282,7 +282,7 @@ def base_url do
end end
Pleroma.Uploaders.IPFS -> Pleroma.Uploaders.IPFS ->
Config.get([Pleroma.Uploaders.IPFS, :get_gateway_url]) @config_impl.get([Pleroma.Uploaders.IPFS, :get_gateway_url])
_ -> _ ->
public_endpoint || upload_base_url || Pleroma.Web.Endpoint.url() <> "/media/" public_endpoint || upload_base_url || Pleroma.Web.Endpoint.url() <> "/media/"

View File

@ -6,11 +6,12 @@ defmodule Pleroma.Uploaders.IPFS do
@behaviour Pleroma.Uploaders.Uploader @behaviour Pleroma.Uploaders.Uploader
require Logger require Logger
alias Pleroma.Config
alias Tesla.Multipart alias Tesla.Multipart
@config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config)
defp get_final_url(method) do defp get_final_url(method) do
config = Config.get([__MODULE__]) config = @config_impl.get([__MODULE__])
post_base_url = Keyword.get(config, :post_gateway_url) post_base_url = Keyword.get(config, :post_gateway_url)
Path.join([post_base_url, method]) Path.join([post_base_url, method])
@ -69,7 +70,7 @@ def put_file(%Pleroma.Upload{} = upload) do
@impl true @impl true
def delete_file(file) do def delete_file(file) do
case Pleroma.HTTP.post(delete_file_endpoint(), "", [], params: [arg: file]) do case Pleroma.HTTP.post(delete_file_endpoint(), "", [], params: [arg: file]) do
{:ok, %{status_code: 204}} -> :ok {:ok, %{status: 204}} -> :ok
error -> {:error, inspect(error)} error -> {:error, inspect(error)}
end end
end end

View File

@ -8,22 +8,22 @@ defmodule Pleroma.Uploaders.IPFSTest do
alias Pleroma.Uploaders.IPFS alias Pleroma.Uploaders.IPFS
alias Tesla.Multipart alias Tesla.Multipart
import Mock
import ExUnit.CaptureLog import ExUnit.CaptureLog
import Mock
import Mox
setup do alias Pleroma.UnstubbedConfigMock, as: Config
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.IPFS)
clear_config([Pleroma.Uploaders.IPFS])
clear_config(
[Pleroma.Uploaders.IPFS, :get_gateway_url],
"https://{CID}.ipfs.mydomain.com"
)
clear_config([Pleroma.Uploaders.IPFS, :post_gateway_url], "http://localhost:5001")
end
describe "get_final_url" do describe "get_final_url" do
setup do
Config
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
[post_gateway_url: "http://localhost:5001"]
end)
:ok
end
test "it returns the final url for put_file" do test "it returns the final url for put_file" do
assert IPFS.put_file_endpoint() == "http://localhost:5001/api/v0/add" assert IPFS.put_file_endpoint() == "http://localhost:5001/api/v0/add"
end end
@ -34,7 +34,21 @@ test "it returns the final url for delete_file" do
end end
describe "get_file/1" do describe "get_file/1" do
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
test "it returns path to ipfs file with cid as subdomain" do test "it returns path to ipfs file with cid as subdomain" do
Config
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :get_gateway_url] ->
"https://{CID}.ipfs.mydomain.com"
end)
assert IPFS.get_file("testcid") == { assert IPFS.get_file("testcid") == {
:ok, :ok,
{:url, "https://testcid.ipfs.mydomain.com"} {:url, "https://testcid.ipfs.mydomain.com"}
@ -42,10 +56,10 @@ test "it returns path to ipfs file with cid as subdomain" do
end end
test "it returns path to ipfs file with cid as path" do test "it returns path to ipfs file with cid as path" do
clear_config( Config
[Pleroma.Uploaders.IPFS, :get_gateway_url], |> expect(:get, fn [Pleroma.Uploaders.IPFS, :get_gateway_url] ->
"https://ipfs.mydomain.com/ipfs/{CID}" "https://ipfs.mydomain.com/ipfs/{CID}"
) end)
assert IPFS.get_file("testcid") == { assert IPFS.get_file("testcid") == {
:ok, :ok,
@ -56,6 +70,11 @@ test "it returns path to ipfs file with cid as path" do
describe "put_file/1" do describe "put_file/1" do
setup do setup do
Config
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
[post_gateway_url: "http://localhost:5001"]
end)
file_upload = %Pleroma.Upload{ file_upload = %Pleroma.Upload{
name: "image-tet.jpg", name: "image-tet.jpg",
content_type: "image/jpeg", content_type: "image/jpeg",
@ -73,7 +92,7 @@ test "it returns path to ipfs file with cid as path" do
test "save file", %{file_upload: file_upload} do test "save file", %{file_upload: file_upload} do
with_mock Pleroma.HTTP, with_mock Pleroma.HTTP,
post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] -> post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -88,7 +107,7 @@ test "save file", %{file_upload: file_upload} do
test "returns error", %{file_upload: file_upload} do test "returns error", %{file_upload: file_upload} do
with_mock Pleroma.HTTP, with_mock Pleroma.HTTP,
post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] -> post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
{:error, "IPFS Gateway upload failed"} {:error, "IPFS Gateway upload failed"}
end do end do
assert capture_log(fn -> assert capture_log(fn ->
@ -99,19 +118,19 @@ test "returns error", %{file_upload: file_upload} do
test "returns error if JSON decode fails", %{file_upload: file_upload} do test "returns error if JSON decode fails", %{file_upload: file_upload} do
with_mock Pleroma.HTTP, [], with_mock Pleroma.HTTP, [],
post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] -> post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
{:ok, %Tesla.Env{status: 200, body: "invalid"}} {:ok, %Tesla.Env{status: 200, body: "invalid"}}
end do end do
assert capture_log(fn -> assert capture_log(fn ->
assert IPFS.put_file(file_upload) == {:error, "JSON decode failed"} assert IPFS.put_file(file_upload) == {:error, "JSON decode failed"}
end) =~ end) =~
"Elixir.Pleroma.Uploaders.IPFS: {:error, %Jason.DecodeError{data: \"invalid\", position: 0, token: nil}}" "Elixir.Pleroma.Uploaders.IPFS: {:error, %Jason.DecodeError"
end end
end end
test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
with_mock Pleroma.HTTP, [], with_mock Pleroma.HTTP, [],
post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] -> post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
{:ok, %Tesla.Env{status: 200, body: "{\"key\": \"value\"}"}} {:ok, %Tesla.Env{status: 200, body: "{\"key\": \"value\"}"}}
end do end do
assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"} assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}
@ -120,9 +139,18 @@ test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_
end end
describe "delete_file/1" do describe "delete_file/1" do
setup do
Config
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
[post_gateway_url: "http://localhost:5001"]
end)
:ok
end
test_with_mock "deletes file", Pleroma.HTTP, test_with_mock "deletes file", Pleroma.HTTP,
post: fn "http://localhost:5001/api/v0/files/rm", "", [], params: [arg: "image.jpg"] -> post: fn "http://localhost:5001/api/v0/files/rm", "", [], params: [arg: "image.jpg"] ->
{:ok, %{status_code: 204}} {:ok, %{status: 204}}
end do end do
assert :ok = IPFS.delete_file("image.jpg") assert :ok = IPFS.delete_file("image.jpg")
end end