fix tests

This commit is contained in:
Maksim Pechnikov 2020-11-16 21:45:37 +03:00
parent 36ec604521
commit e1d25bad0c
2 changed files with 27 additions and 28 deletions

View file

@ -62,10 +62,9 @@ def show(opts) do
@spec delete(String.t()) :: @spec delete(String.t()) ::
{:ok, [binary()]} | {:error, File.posix(), binary()} | {:error, :empty_values} {:ok, [binary()]} | {:error, File.posix(), binary()} | {:error, :empty_values}
def delete(name) do def delete(name) do
with :ok <- validate_not_empty([name]) do with :ok <- validate_not_empty([name]),
emoji_path() pack_path <- Path.join(emoji_path(), name) do
|> Path.join(name) File.rm_rf(pack_path)
|> File.rm_rf()
end end
end end

View file

@ -5,6 +5,7 @@
defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
use Pleroma.Web.ConnCase, async: false use Pleroma.Web.ConnCase, async: false
import Mock
import Tesla.Mock import Tesla.Mock
import Pleroma.Factory import Pleroma.Factory
@ -366,11 +367,9 @@ test "other error", %{admin_conn: admin_conn} do
end end
test "returns error when file system not writable", %{admin_conn: conn} = ctx do test "returns error when file system not writable", %{admin_conn: conn} = ctx do
{:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path) with_mocks([
{File, [:passthrough], [stat: fn _ -> {:error, :eacces} end]}
try do ]) do
File.chmod!(@emoji_path, 0o400)
assert conn assert conn
|> put_req_header("content-type", "multipart/form-data") |> put_req_header("content-type", "multipart/form-data")
|> patch( |> patch(
@ -378,8 +377,6 @@ test "returns error when file system not writable", %{admin_conn: conn} = ctx do
%{"metadata" => ctx[:new_data]} %{"metadata" => ctx[:new_data]}
) )
|> json_response_and_validate_schema(500) |> json_response_and_validate_schema(500)
after
File.chmod!(@emoji_path, mode)
end end
end end
@ -442,40 +439,43 @@ test "when the fallback source doesn't have all the files", ctx do
end end
describe "POST/DELETE /api/pleroma/emoji/pack?name=:name" do describe "POST/DELETE /api/pleroma/emoji/pack?name=:name" do
test "returns error when file system not writable", %{admin_conn: admin_conn} do test "returns an error on creates pack when file system not writable", %{
{:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path) admin_conn: admin_conn
} do
try do path_pack = Path.join(@emoji_path, "test_pack")
File.chmod!(@emoji_path, 0o400)
with_mocks([
{File, [:passthrough], [mkdir: fn ^path_pack -> {:error, :eacces} end]}
]) do
assert admin_conn assert admin_conn
|> post("/api/pleroma/emoji/pack?name=test_pack") |> post("/api/pleroma/emoji/pack?name=test_pack")
|> json_response_and_validate_schema(500) == %{ |> json_response_and_validate_schema(500) == %{
"error" => "error" =>
"Unexpected error occurred while creating pack. (POSIX error: Permission denied)" "Unexpected error occurred while creating pack. (POSIX error: Permission denied)"
} }
after
File.chmod!(@emoji_path, mode)
end end
end end
test "returns an error on deletes pack when the file system is not writable", %{ test "returns an error on deletes pack when the file system is not writable", %{
admin_conn: admin_conn admin_conn: admin_conn
} do } do
{:ok, _pack} = Pleroma.Emoji.Pack.create("test_pack2") path_pack = Path.join(@emoji_path, "test_emoji_pack")
{:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path)
try do try do
File.chmod!(@emoji_path, 0o400) {:ok, _pack} = Pleroma.Emoji.Pack.create("test_emoji_pack")
with_mocks([
{File, [:passthrough], [rm_rf: fn ^path_pack -> {:error, :eacces, path_pack} end]}
]) do
assert admin_conn assert admin_conn
|> delete("/api/pleroma/emoji/pack?name=test_pack") |> delete("/api/pleroma/emoji/pack?name=test_emoji_pack")
|> json_response_and_validate_schema(500) == %{ |> json_response_and_validate_schema(500) == %{
"error" => "Couldn't delete the pack test_pack (POSIX error: Permission denied)" "error" =>
"Couldn't delete the pack test_emoji_pack (POSIX error: Permission denied)"
} }
end
after after
File.chmod!(@emoji_path, mode) File.rm_rf(path_pack)
File.rm_rf!(Path.join([@emoji_path, "test_pack2"]))
end end
end end