ipfs: better tests with @ilja suggestions

This commit is contained in:
Claudio Maradonna 2022-06-28 17:53:44 +02:00
parent 254f2ea854
commit 5e097eb91d
No known key found for this signature in database
GPG Key ID: 0CBA58694C5680D9
1 changed files with 25 additions and 13 deletions

View File

@ -6,6 +6,7 @@ defmodule Pleroma.Uploaders.IPFSTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Uploaders.IPFS alias Pleroma.Uploaders.IPFS
alias Tesla.Multipart
import Mock import Mock
import ExUnit.CaptureLog import ExUnit.CaptureLog
@ -62,12 +63,17 @@ test "it returns path to ipfs file with cid as path" do
tempfile: Path.absname("test/instance_static/add/shortcode.png") tempfile: Path.absname("test/instance_static/add/shortcode.png")
} }
[file_upload: file_upload] mp =
Multipart.new()
|> Multipart.add_content_type_param("charset=utf-8")
|> Multipart.add_file(file_upload.tempfile)
[file_upload: file_upload, mp: mp]
end end
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 _, _, _, _ -> post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] ->
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -81,7 +87,10 @@ test "save file", %{file_upload: file_upload} do
end end
test "returns error", %{file_upload: file_upload} do test "returns error", %{file_upload: file_upload} do
with_mock Pleroma.HTTP, post: fn _, _, _, _ -> {:error, "IPFS Gateway upload failed"} end do with_mock Pleroma.HTTP,
post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] ->
{:error, "IPFS Gateway upload failed"}
end do
assert capture_log(fn -> assert capture_log(fn ->
assert IPFS.put_file(file_upload) == {:error, "IPFS Gateway upload failed"} assert IPFS.put_file(file_upload) == {:error, "IPFS Gateway upload failed"}
end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"IPFS Gateway upload failed\"}" end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"IPFS Gateway upload failed\"}"
@ -89,21 +98,22 @@ test "returns error", %{file_upload: file_upload} do
end end
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_mocks([ with_mock Pleroma.HTTP, [],
{Pleroma.HTTP, [], [post: fn _, _, _, _ -> {:ok, %Tesla.Env{status: 200, body: ''}} end]}, post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] ->
{Jason, [], [decode: fn _ -> {:error, "JSON decode failed"} end]} {:ok, %Tesla.Env{status: 200, body: 'invalid'}}
]) 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) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"JSON decode failed\"}" end) =~
"Elixir.Pleroma.Uploaders.IPFS: {:error, %Jason.DecodeError{data: \"invalid\", position: 0, token: nil}}"
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_mocks([ with_mock Pleroma.HTTP, [],
{Pleroma.HTTP, [], [post: fn _, _, _, _ -> {:ok, %Tesla.Env{status: 200, body: ''}} end]}, post: fn "http://localhost:5001/api/v0/add", mp, [], params: ["cid-version": "1"] ->
{Jason, [], [decode: fn _ -> {:ok, %{}} end]} {:ok, %Tesla.Env{status: 200, body: '{"key": "value"}'}}
]) 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"}
end end
end end
@ -111,7 +121,9 @@ test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_
describe "delete_file/1" do describe "delete_file/1" do
test_with_mock "deletes file", Pleroma.HTTP, test_with_mock "deletes file", Pleroma.HTTP,
post: fn _, _, _, _ -> {:ok, %{status_code: 204}} end do post: fn "http://localhost:5001/api/v0/files/rm", "", [], params: [arg: "image.jpg"] ->
{:ok, %{status_code: 204}}
end do
assert :ok = IPFS.delete_file("image.jpg") assert :ok = IPFS.delete_file("image.jpg")
end end
end end