Lint and fix test to match new log message
[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(:banned_urls_cache) end)
15 end
16
17 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
18 Config.put([:media_proxy, :enabled], false)
19
20 assert %Plug.Conn{
21 status: 404,
22 resp_body: "Not Found"
23 } = get(conn, "/proxy/hhgfh/eeeee")
24
25 assert %Plug.Conn{
26 status: 404,
27 resp_body: "Not Found"
28 } = get(conn, "/proxy/hhgfh/eeee/fff")
29 end
30
31 test "it returns 403 when signature invalidated", %{conn: conn} do
32 Config.put([:media_proxy, :enabled], true)
33 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
34 path = URI.parse(Pleroma.Web.MediaProxy.encode_url("https://google.fn")).path
35 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
36
37 assert %Plug.Conn{
38 status: 403,
39 resp_body: "Forbidden"
40 } = get(conn, path)
41
42 assert %Plug.Conn{
43 status: 403,
44 resp_body: "Forbidden"
45 } = get(conn, "/proxy/hhgfh/eeee")
46
47 assert %Plug.Conn{
48 status: 403,
49 resp_body: "Forbidden"
50 } = get(conn, "/proxy/hhgfh/eeee/fff")
51 end
52
53 test "redirects on valid url when filename invalidated", %{conn: conn} do
54 Config.put([:media_proxy, :enabled], true)
55 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
56 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
57 invalid_url = String.replace(url, "test.png", "test-file.png")
58 response = get(conn, invalid_url)
59 assert response.status == 302
60 assert redirected_to(response) == url
61 end
62
63 test "it performs ReverseProxy.call when signature valid", %{conn: conn} do
64 Config.put([:media_proxy, :enabled], true)
65 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
66 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
67
68 with_mock Pleroma.ReverseProxy,
69 call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
70 assert %Plug.Conn{status: :success} = get(conn, url)
71 end
72 end
73
74 test "it returns 404 when url contains in banned_urls cache", %{conn: conn} do
75 Config.put([:media_proxy, :enabled], true)
76 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
77 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
78 Pleroma.Web.MediaProxy.put_in_banned_urls("https://google.fn/test.png")
79
80 with_mock Pleroma.ReverseProxy,
81 call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
82 assert %Plug.Conn{status: 404, resp_body: "Not Found"} = get(conn, url)
83 end
84 end
85 end