Apply suggestion to test/web/admin_api/controllers/media_proxy_cache_controller_test.exs
[akkoma] / test / web / media_proxy / media_proxy_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
6 use Pleroma.Web.ConnCase
7 import Mock
8 alias Pleroma.Config
9
10 setup do: clear_config(:media_proxy)
11 setup do: clear_config([Pleroma.Web.Endpoint, :secret_key_base])
12
13 setup do
14 on_exit(fn -> Cachex.clear(:deleted_urls_cache) end)
15 :ok
16 end
17
18 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
19 Config.put([:media_proxy, :enabled], false)
20
21 assert %Plug.Conn{
22 status: 404,
23 resp_body: "Not Found"
24 } = get(conn, "/proxy/hhgfh/eeeee")
25
26 assert %Plug.Conn{
27 status: 404,
28 resp_body: "Not Found"
29 } = get(conn, "/proxy/hhgfh/eeee/fff")
30 end
31
32 test "it returns 403 when signature invalidated", %{conn: conn} do
33 Config.put([:media_proxy, :enabled], true)
34 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
35 path = URI.parse(Pleroma.Web.MediaProxy.encode_url("https://google.fn")).path
36 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
37
38 assert %Plug.Conn{
39 status: 403,
40 resp_body: "Forbidden"
41 } = get(conn, path)
42
43 assert %Plug.Conn{
44 status: 403,
45 resp_body: "Forbidden"
46 } = get(conn, "/proxy/hhgfh/eeee")
47
48 assert %Plug.Conn{
49 status: 403,
50 resp_body: "Forbidden"
51 } = get(conn, "/proxy/hhgfh/eeee/fff")
52 end
53
54 test "redirects on valid url when filename invalidated", %{conn: conn} do
55 Config.put([:media_proxy, :enabled], true)
56 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
57 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
58 invalid_url = String.replace(url, "test.png", "test-file.png")
59 response = get(conn, invalid_url)
60 assert response.status == 302
61 assert redirected_to(response) == url
62 end
63
64 test "it performs ReverseProxy.call when signature valid", %{conn: conn} do
65 Config.put([:media_proxy, :enabled], true)
66 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
67 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
68
69 with_mock Pleroma.ReverseProxy,
70 call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
71 assert %Plug.Conn{status: :success} = get(conn, url)
72 end
73 end
74
75 test "it returns 404 when url contains in deleted_urls cache", %{conn: conn} do
76 Config.put([:media_proxy, :enabled], true)
77 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
78 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
79 Pleroma.Web.MediaProxy.put_in_deleted_urls("https://google.fn/test.png")
80
81 with_mock Pleroma.ReverseProxy,
82 call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
83 assert %Plug.Conn{status: 404, resp_body: "Not Found"} = get(conn, url)
84 end
85 end
86 end