From e1d25bad0c91f903ef6d8c7a2c5d7f2d63213d85 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Mon, 16 Nov 2020 21:45:37 +0300 Subject: [PATCH] fix tests --- lib/pleroma/emoji/pack.ex | 7 ++- .../emoji_pack_controller_test.exs | 48 +++++++++---------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/pleroma/emoji/pack.ex b/lib/pleroma/emoji/pack.ex index 4f4e84bfe..f768af19f 100644 --- a/lib/pleroma/emoji/pack.ex +++ b/lib/pleroma/emoji/pack.ex @@ -62,10 +62,9 @@ def show(opts) do @spec delete(String.t()) :: {:ok, [binary()]} | {:error, File.posix(), binary()} | {:error, :empty_values} def delete(name) do - with :ok <- validate_not_empty([name]) do - emoji_path() - |> Path.join(name) - |> File.rm_rf() + with :ok <- validate_not_empty([name]), + pack_path <- Path.join(emoji_path(), name) do + File.rm_rf(pack_path) end end diff --git a/test/pleroma/web/pleroma_api/controllers/emoji_pack_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/emoji_pack_controller_test.exs index 151f69cde..aa5348c6c 100644 --- a/test/pleroma/web/pleroma_api/controllers/emoji_pack_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/emoji_pack_controller_test.exs @@ -5,6 +5,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do use Pleroma.Web.ConnCase, async: false + import Mock import Tesla.Mock import Pleroma.Factory @@ -366,11 +367,9 @@ test "other error", %{admin_conn: admin_conn} do end test "returns error when file system not writable", %{admin_conn: conn} = ctx do - {:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path) - - try do - File.chmod!(@emoji_path, 0o400) - + with_mocks([ + {File, [:passthrough], [stat: fn _ -> {:error, :eacces} end]} + ]) do assert conn |> put_req_header("content-type", "multipart/form-data") |> patch( @@ -378,8 +377,6 @@ test "returns error when file system not writable", %{admin_conn: conn} = ctx do %{"metadata" => ctx[:new_data]} ) |> json_response_and_validate_schema(500) - after - File.chmod!(@emoji_path, mode) end end @@ -442,40 +439,43 @@ test "when the fallback source doesn't have all the files", ctx do end describe "POST/DELETE /api/pleroma/emoji/pack?name=:name" do - test "returns error when file system not writable", %{admin_conn: admin_conn} do - {:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path) - - try do - File.chmod!(@emoji_path, 0o400) + test "returns an error on creates pack when file system not writable", %{ + admin_conn: admin_conn + } do + path_pack = Path.join(@emoji_path, "test_pack") + with_mocks([ + {File, [:passthrough], [mkdir: fn ^path_pack -> {:error, :eacces} end]} + ]) do assert admin_conn |> post("/api/pleroma/emoji/pack?name=test_pack") |> json_response_and_validate_schema(500) == %{ "error" => "Unexpected error occurred while creating pack. (POSIX error: Permission denied)" } - after - File.chmod!(@emoji_path, mode) end end test "returns an error on deletes pack when the file system is not writable", %{ admin_conn: admin_conn } do - {:ok, _pack} = Pleroma.Emoji.Pack.create("test_pack2") - {:ok, %File.Stat{mode: mode}} = File.stat(@emoji_path) + path_pack = Path.join(@emoji_path, "test_emoji_pack") try do - File.chmod!(@emoji_path, 0o400) + {:ok, _pack} = Pleroma.Emoji.Pack.create("test_emoji_pack") - assert admin_conn - |> delete("/api/pleroma/emoji/pack?name=test_pack") - |> json_response_and_validate_schema(500) == %{ - "error" => "Couldn't delete the pack test_pack (POSIX error: Permission denied)" - } + with_mocks([ + {File, [:passthrough], [rm_rf: fn ^path_pack -> {:error, :eacces, path_pack} end]} + ]) do + assert admin_conn + |> delete("/api/pleroma/emoji/pack?name=test_emoji_pack") + |> json_response_and_validate_schema(500) == %{ + "error" => + "Couldn't delete the pack test_emoji_pack (POSIX error: Permission denied)" + } + end after - File.chmod!(@emoji_path, mode) - File.rm_rf!(Path.join([@emoji_path, "test_pack2"])) + File.rm_rf(path_pack) end end