added test
[akkoma] / test / web / admin_api / controllers / media_proxy_cache_controller_test.exs
index 1b1d6bc36f295c8067c0f71e920f6995fac6a3c0..5ab6cb78a0255ba4a844476770c4bd8c7463de1e 100644 (file)
@@ -6,6 +6,15 @@ defmodule Pleroma.Web.AdminAPI.MediaProxyCacheControllerTest do
   use Pleroma.Web.ConnCase
 
   import Pleroma.Factory
+  import Mock
+
+  alias Pleroma.Web.MediaProxy
+
+  setup do: clear_config([:media_proxy])
+
+  setup do
+    on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
+  end
 
   setup do
     admin = insert(:user, is_admin: true)
@@ -16,51 +25,121 @@ defmodule Pleroma.Web.AdminAPI.MediaProxyCacheControllerTest do
       |> assign(:user, admin)
       |> assign(:token, token)
 
+    Config.put([:media_proxy, :enabled], true)
+    Config.put([:media_proxy, :invalidation, :enabled], true)
+    Config.put([:media_proxy, :invalidation, :provider], MediaProxy.Invalidation.Script)
+
     {:ok, %{admin: admin, token: token, conn: conn}}
   end
 
   describe "GET /api/pleroma/admin/media_proxy_caches" do
     test "shows banned MediaProxy URLs", %{conn: conn} do
+      MediaProxy.put_in_banned_urls([
+        "http://localhost:4001/media/a688346.jpg",
+        "http://localhost:4001/media/fb1f4d.jpg"
+      ])
+
+      MediaProxy.put_in_banned_urls("http://localhost:4001/media/gb1f44.jpg")
+      MediaProxy.put_in_banned_urls("http://localhost:4001/media/tb13f47.jpg")
+      MediaProxy.put_in_banned_urls("http://localhost:4001/media/wb1f46.jpg")
+
       response =
         conn
-        |> get("/api/pleroma/admin/media_proxy_caches")
+        |> get("/api/pleroma/admin/media_proxy_caches?page_size=2")
         |> json_response_and_validate_schema(200)
 
-      assert response["urls"] == []
-    end
-  end
+      assert response["urls"] == [
+               "http://localhost:4001/media/fb1f4d.jpg",
+               "http://localhost:4001/media/a688346.jpg"
+             ]
 
-  describe "DELETE /api/pleroma/admin/media_proxy_caches/delete" do
-    test "deleted MediaProxy URLs from banned", %{conn: conn} do
       response =
         conn
-        |> put_req_header("content-type", "application/json")
-        |> post("/api/pleroma/admin/media_proxy_caches/delete", %{
-          urls: ["http://example.com/media/a688346.jpg", "http://example.com/media/fb1f4d.jpg"]
-        })
+        |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=2")
         |> json_response_and_validate_schema(200)
 
       assert response["urls"] == [
-               "http://example.com/media/a688346.jpg",
-               "http://example.com/media/fb1f4d.jpg"
+               "http://localhost:4001/media/gb1f44.jpg",
+               "http://localhost:4001/media/tb13f47.jpg"
              ]
+
+      response =
+        conn
+        |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=3")
+        |> json_response_and_validate_schema(200)
+
+      assert response["urls"] == ["http://localhost:4001/media/wb1f46.jpg"]
     end
   end
 
-  describe "PURGE /api/pleroma/admin/media_proxy_caches/purge" do
-    test "perform invalidates cache of MediaProxy", %{conn: conn} do
+  describe "POST /api/pleroma/admin/media_proxy_caches/delete" do
+    test "deleted MediaProxy URLs from banned", %{conn: conn} do
+      MediaProxy.put_in_banned_urls([
+        "http://localhost:4001/media/a688346.jpg",
+        "http://localhost:4001/media/fb1f4d.jpg"
+      ])
+
       response =
         conn
         |> put_req_header("content-type", "application/json")
-        |> post("/api/pleroma/admin/media_proxy_caches/purge", %{
-          urls: ["http://example.com/media/a688346.jpg", "http://example.com/media/fb1f4d.jpg"]
+        |> post("/api/pleroma/admin/media_proxy_caches/delete", %{
+          urls: ["http://localhost:4001/media/a688346.jpg"]
         })
         |> json_response_and_validate_schema(200)
 
-      assert response["urls"] == [
-               "http://example.com/media/a688346.jpg",
-               "http://example.com/media/fb1f4d.jpg"
-             ]
+      assert response["urls"] == ["http://localhost:4001/media/a688346.jpg"]
+      refute MediaProxy.in_banned_urls("http://localhost:4001/media/a688346.jpg")
+      assert MediaProxy.in_banned_urls("http://localhost:4001/media/fb1f4d.jpg")
+    end
+  end
+
+  describe "POST /api/pleroma/admin/media_proxy_caches/purge" do
+    test "perform invalidates cache of MediaProxy", %{conn: conn} do
+      urls = [
+        "http://example.com/media/a688346.jpg",
+        "http://example.com/media/fb1f4d.jpg"
+      ]
+
+      with_mocks [
+        {MediaProxy.Invalidation.Script, [],
+         [
+           purge: fn _, _ -> {"ok", 0} end
+         ]}
+      ] do
+        response =
+          conn
+          |> put_req_header("content-type", "application/json")
+          |> post("/api/pleroma/admin/media_proxy_caches/purge", %{urls: urls, ban: false})
+          |> json_response_and_validate_schema(200)
+
+        assert response["urls"] == urls
+
+        refute MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
+        refute MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")
+      end
+    end
+
+    test "perform invalidates cache of MediaProxy and adds url to banned", %{conn: conn} do
+      urls = [
+        "http://example.com/media/a688346.jpg",
+        "http://example.com/media/fb1f4d.jpg"
+      ]
+
+      with_mocks [{MediaProxy.Invalidation.Script, [], [purge: fn _, _ -> {"ok", 0} end]}] do
+        response =
+          conn
+          |> put_req_header("content-type", "application/json")
+          |> post("/api/pleroma/admin/media_proxy_caches/purge", %{
+            urls: urls,
+            ban: true
+          })
+          |> json_response_and_validate_schema(200)
+
+        assert response["urls"] == urls
+
+        assert MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
+        assert MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")
+      end
     end
   end
 end