summaryrefslogtreecommitdiff
path: root/test/pleroma/web/admin_api/controllers/media_proxy_cache_controller_test.exs
blob: 852334a571c6eed8952ec60b8ae27d5ae8692169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

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
    admin = insert(:user, is_admin: true)
    token = insert(:oauth_admin_token, user: admin)

    conn =
      build_conn()
      |> assign(:user, admin)
      |> assign(:token, token)

    clear_config([:media_proxy, :enabled], true)
    clear_config([:media_proxy, :invalidation, :enabled], true)
    clear_config([: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?page_size=2")
        |> json_response_and_validate_schema(200)

      assert response["page_size"] == 2
      assert response["count"] == 5

      results = response["urls"]

      response =
        conn
        |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=2")
        |> json_response_and_validate_schema(200)

      assert response["page_size"] == 2
      assert response["count"] == 5

      results = results ++ response["urls"]

      response =
        conn
        |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=3")
        |> json_response_and_validate_schema(200)

      results = results ++ response["urls"]

      assert results |> Enum.sort() ==
               [
                 "http://localhost:4001/media/wb1f46.jpg",
                 "http://localhost:4001/media/gb1f44.jpg",
                 "http://localhost:4001/media/tb13f47.jpg",
                 "http://localhost:4001/media/fb1f4d.jpg",
                 "http://localhost:4001/media/a688346.jpg"
               ]
               |> Enum.sort()
    end

    test "search banned MediaProxy URLs", %{conn: conn} do
      MediaProxy.put_in_banned_urls([
        "http://localhost:4001/media/a688346.jpg",
        "http://localhost:4001/media/ff44b1f4d.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?page_size=2&query=F44")
        |> json_response_and_validate_schema(200)

      assert response["urls"] |> Enum.sort() == [
               "http://localhost:4001/media/ff44b1f4d.jpg",
               "http://localhost:4001/media/gb1f44.jpg"
             ]

      assert response["page_size"] == 2
      assert response["count"] == 2
    end
  end

  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"
      ])

      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/pleroma/admin/media_proxy_caches/delete", %{
        urls: ["http://localhost:4001/media/a688346.jpg"]
      })
      |> json_response_and_validate_schema(200)

      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
        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)

        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
        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 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