fix tests
authorMaksim Pechnikov <parallel588@gmail.com>
Mon, 16 Nov 2020 18:45:37 +0000 (21:45 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Mon, 16 Nov 2020 18:45:37 +0000 (21:45 +0300)
lib/pleroma/emoji/pack.ex
test/pleroma/web/pleroma_api/controllers/emoji_pack_controller_test.exs

index 4f4e84bfe57ffe681770411bd995f0b5a636e58a..f768af19f7891a8b5be8c9520cf832eb9a66338a 100644 (file)
@@ -62,10 +62,9 @@ defmodule Pleroma.Emoji.Pack 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
 
index 151f69cde7f7d4a65e29c5aa5a8ef429df3012b1..aa5348c6c5a9a69373c2cea8cd2465c95421d0d3 100644 (file)
@@ -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 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest 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 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
                  %{"metadata" => ctx[:new_data]}
                )
                |> json_response_and_validate_schema(500)
-      after
-        File.chmod!(@emoji_path, mode)
       end
     end
 
@@ -442,40 +439,43 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest 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)
-
-        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)"
-               }
+        {: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
+                 |> 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