IPFS uploader: dialyzer fixes

lib/pleroma/uploaders/ipfs.ex:43:no_return
Function put_file/1 has no local return.
________________________________________________________________________________
lib/pleroma/uploaders/ipfs.ex:49:call
The function call will not succeed.

Pleroma.HTTP.post(
  binary(),
  _mp :: %Tesla.Multipart{
    :boundary => binary(),
    :content_type_params => [binary()],
    :parts => [
      %Tesla.Multipart.Part{
        :body => binary(),
        :dispositions => [any()],
        :headers => [any()]
      },
      ...
    ]
  },
  [],
  [{:params, [{:"cid-version", <<49>>}]}]
)

will never return since the success typing is:
(binary(), binary(), [{binary(), binary()}], Keyword.t()) ::
  {:error, _}
  | {:ok,
     %Tesla.Env{
       :__client__ => %Tesla.Client{
         :adapter => nil | {_, _} | {_, _, _},
         :fun => _,
         :post => [any()],
         :pre => [any()]
       },
       :__module__ => atom(),
       :body => _,
       :headers => [{_, _}],
       :method => :delete | :get | :head | :options | :patch | :post | :put | :trace,
       :opts => [{_, _}],
       :query => [{_, _}],
       :status => nil | integer(),
       :url => binary()
     }}

and the contract is
(Pleroma.HTTP.Request.url(), String.t(), Pleroma.HTTP.Request.headers(), :elixir.keyword()) ::
  {:ok, Tesla.Env.t()} | {:error, any()}
This commit is contained in:
Mark Felder 2024-05-28 13:14:34 -04:00
commit cfc8d7aade
5 changed files with 55 additions and 62 deletions

View file

@ -14,25 +14,6 @@ defmodule Pleroma.Uploaders.IPFSTest do
alias Pleroma.UnstubbedConfigMock, as: Config
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
assert IPFS.put_file_endpoint() == "http://localhost:5001/api/v0/add"
end
test "it returns the final url for delete_file" do
assert IPFS.delete_file_endpoint() == "http://localhost:5001/api/v0/files/rm"
end
end
describe "get_file/1" do
setup do
Config
@ -71,8 +52,8 @@ defmodule Pleroma.Uploaders.IPFSTest do
describe "put_file/1" do
setup do
Config
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
[post_gateway_url: "http://localhost:5001"]
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :post_gateway_url] ->
"http://localhost:5001"
end)
file_upload = %Pleroma.Upload{
@ -92,7 +73,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "save file", %{file_upload: file_upload} do
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"],
pool: :upload ->
{:ok,
%Tesla.Env{
status: 200,
@ -107,7 +92,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "returns error", %{file_upload: file_upload} do
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"],
pool: :upload ->
{:error, "IPFS Gateway upload failed"}
end do
assert capture_log(fn ->
@ -118,7 +107,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "returns error if JSON decode fails", %{file_upload: file_upload} do
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"],
pool: :upload ->
{:ok, %Tesla.Env{status: 200, body: "invalid"}}
end do
assert capture_log(fn ->
@ -130,7 +123,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
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"],
pool: :upload ->
{:ok, %Tesla.Env{status: 200, body: "{\"key\": \"value\"}"}}
end do
assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}
@ -141,8 +138,8 @@ defmodule Pleroma.Uploaders.IPFSTest do
describe "delete_file/1" do
setup do
Config
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
[post_gateway_url: "http://localhost:5001"]
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :post_gateway_url] ->
"http://localhost:5001"
end)
:ok