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.MediaProxy.MediaProxyControllerTest do
6 use Pleroma.Web.ConnCase
10 alias Pleroma.Web.MediaProxy
11 alias Pleroma.Web.MediaProxy.MediaProxyController
15 on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
18 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
19 clear_config([:media_proxy, :enabled], false)
23 resp_body: "Not Found"
24 } = get(conn, "/proxy/hhgfh/eeeee")
28 resp_body: "Not Found"
29 } = get(conn, "/proxy/hhgfh/eeee/fff")
34 clear_config([:media_proxy, :enabled], true)
35 clear_config([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
36 [url: MediaProxy.encode_url("https://google.fn/test.png")]
39 test "it returns 403 for invalid signature", %{conn: conn, url: url} do
40 Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
41 %{path: path} = URI.parse(url)
45 resp_body: "Forbidden"
50 resp_body: "Forbidden"
51 } = get(conn, "/proxy/hhgfh/eeee")
55 resp_body: "Forbidden"
56 } = get(conn, "/proxy/hhgfh/eeee/fff")
59 test "redirects on valid url when filename is invalidated", %{conn: conn, url: url} do
60 invalid_url = String.replace(url, "test.png", "test-file.png")
61 response = get(conn, invalid_url)
62 assert response.status == 302
63 assert redirected_to(response) == url
66 test "it performs ReverseProxy.call with valid signature", %{conn: conn, url: url} do
67 with_mock Pleroma.ReverseProxy,
68 call: fn _conn, _url, _opts -> %Conn{status: :success} end do
69 assert %Conn{status: :success} = get(conn, url)
73 test "it returns 404 when url is in banned_urls cache", %{conn: conn, url: url} do
74 MediaProxy.put_in_banned_urls("https://google.fn/test.png")
76 with_mock Pleroma.ReverseProxy,
77 call: fn _conn, _url, _opts -> %Conn{status: :success} end do
78 assert %Conn{status: 404, resp_body: "Not Found"} = get(conn, url)