1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.MediaProxyCacheControllerTest do
6 use Pleroma.Web.ConnCase
11 alias Pleroma.Web.MediaProxy
13 setup do: clear_config([:media_proxy])
16 on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
25 |> assign(:user, admin)
26 |> assign(:token, token)
28 Config.put([:media_proxy, :enabled], true)
29 Config.put([:media_proxy, :invalidation, :enabled], true)
30 Config.put([:media_proxy, :invalidation, :provider], MediaProxy.Invalidation.Script)
32 {:ok, %{admin: admin, token: token, conn: conn}}
35 describe "GET /api/pleroma/admin/media_proxy_caches" do
36 test "shows banned MediaProxy URLs", %{conn: conn} do
37 MediaProxy.put_in_banned_urls([
38 "http://localhost:4001/media/a688346.jpg",
39 "http://localhost:4001/media/fb1f4d.jpg"
42 MediaProxy.put_in_banned_urls("http://localhost:4001/media/gb1f44.jpg")
43 MediaProxy.put_in_banned_urls("http://localhost:4001/media/tb13f47.jpg")
44 MediaProxy.put_in_banned_urls("http://localhost:4001/media/wb1f46.jpg")
48 |> get("/api/pleroma/admin/media_proxy_caches?page_size=2")
49 |> json_response_and_validate_schema(200)
51 assert response["urls"] == [
52 "http://localhost:4001/media/fb1f4d.jpg",
53 "http://localhost:4001/media/a688346.jpg"
58 |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=2")
59 |> json_response_and_validate_schema(200)
61 assert response["urls"] == [
62 "http://localhost:4001/media/gb1f44.jpg",
63 "http://localhost:4001/media/tb13f47.jpg"
68 |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=3")
69 |> json_response_and_validate_schema(200)
71 assert response["urls"] == ["http://localhost:4001/media/wb1f46.jpg"]
75 describe "POST /api/pleroma/admin/media_proxy_caches/delete" do
76 test "deleted MediaProxy URLs from banned", %{conn: conn} do
77 MediaProxy.put_in_banned_urls([
78 "http://localhost:4001/media/a688346.jpg",
79 "http://localhost:4001/media/fb1f4d.jpg"
84 |> put_req_header("content-type", "application/json")
85 |> post("/api/pleroma/admin/media_proxy_caches/delete", %{
86 urls: ["http://localhost:4001/media/a688346.jpg"]
88 |> json_response_and_validate_schema(200)
90 assert response["urls"] == ["http://localhost:4001/media/a688346.jpg"]
91 refute MediaProxy.in_banned_urls("http://localhost:4001/media/a688346.jpg")
92 assert MediaProxy.in_banned_urls("http://localhost:4001/media/fb1f4d.jpg")
96 describe "POST /api/pleroma/admin/media_proxy_caches/purge" do
97 test "perform invalidates cache of MediaProxy", %{conn: conn} do
99 "http://example.com/media/a688346.jpg",
100 "http://example.com/media/fb1f4d.jpg"
104 {MediaProxy.Invalidation.Script, [],
106 purge: fn _, _ -> {"ok", 0} end
111 |> put_req_header("content-type", "application/json")
112 |> post("/api/pleroma/admin/media_proxy_caches/purge", %{urls: urls, ban: false})
113 |> json_response_and_validate_schema(200)
115 assert response["urls"] == urls
117 refute MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
118 refute MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")
122 test "perform invalidates cache of MediaProxy and adds url to banned", %{conn: conn} do
124 "http://example.com/media/a688346.jpg",
125 "http://example.com/media/fb1f4d.jpg"
128 with_mocks [{MediaProxy.Invalidation.Script, [], [purge: fn _, _ -> {"ok", 0} end]}] do
131 |> put_req_header("content-type", "application/json")
132 |> post("/api/pleroma/admin/media_proxy_caches/purge", %{
136 |> json_response_and_validate_schema(200)
138 assert response["urls"] == urls
140 assert MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
141 assert MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")