added test
authorMaksim Pechnikov <parallel588@gmail.com>
Sat, 14 Nov 2020 05:30:22 +0000 (08:30 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Sat, 14 Nov 2020 05:30:22 +0000 (08:30 +0300)
lib/pleroma/web/api_spec/operations/pleroma_emoji_pack_operation.ex
test/pleroma/web/pleroma_api/controllers/emoji_pack_controller_test.exs

index 79f52dcb3afdf43c2c282a8db91b2c945748ce7f..e576ccbad81f16e3eea5b787cd169398d89241d5 100644 (file)
@@ -169,7 +169,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
       responses: %{
         200 => ok_response(),
         400 => Operation.response("Bad Request", "application/json", ApiError),
-        404 => Operation.response("Not Found", "application/json", ApiError)
+        404 => Operation.response("Not Found", "application/json", ApiError),
+        500 => Operation.response("Error", "application/json", ApiError)
       }
     }
   end
@@ -184,7 +185,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
       parameters: [name_param()],
       responses: %{
         200 => Operation.response("Metadata", "application/json", metadata()),
-        400 => Operation.response("Bad Request", "application/json", ApiError)
+        400 => Operation.response("Bad Request", "application/json", ApiError),
+        500 => Operation.response("Error", "application/json", ApiError)
       }
     }
   end
index 3445f0ca043173879a9e7e9af218f1c1d24c213c..151f69cde7f7d4a65e29c5aa5a8ef429df3012b1 100644 (file)
@@ -3,7 +3,7 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
-  use Pleroma.Web.ConnCase
+  use Pleroma.Web.ConnCase, async: false
 
   import Tesla.Mock
   import Pleroma.Factory
@@ -346,7 +346,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
     end
   end
 
-  describe "PATCH /api/pleroma/emoji/pack?name=:name" do
+  describe "PATCH/update /api/pleroma/emoji/pack?name=:name" do
     setup do
       pack_file = "#{@emoji_path}/test_pack/pack.json"
       original_content = File.read!(pack_file)
@@ -365,6 +365,24 @@ 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)
+
+        assert conn
+               |> put_req_header("content-type", "multipart/form-data")
+               |> patch(
+                 "/api/pleroma/emoji/pack?name=test_pack",
+                 %{"metadata" => ctx[:new_data]}
+               )
+               |> json_response_and_validate_schema(500)
+      after
+        File.chmod!(@emoji_path, mode)
+      end
+    end
+
     test "for a pack without a fallback source", ctx do
       assert ctx[:admin_conn]
              |> put_req_header("content-type", "multipart/form-data")
@@ -424,6 +442,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)
+
+        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)
+
+      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)"
+               }
+      after
+        File.chmod!(@emoji_path, mode)
+        File.rm_rf!(Path.join([@emoji_path, "test_pack2"]))
+      end
+    end
+
     test "creating and deleting a pack", %{admin_conn: admin_conn} do
       assert admin_conn
              |> post("/api/pleroma/emoji/pack?name=test_created")