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
14 on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
17 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
18 clear_config([:media_proxy, :enabled], false)
22 resp_body: "Not Found"
23 } = get(conn, "/proxy/hhgfh/eeeee")
27 resp_body: "Not Found"
28 } = get(conn, "/proxy/hhgfh/eeee/fff")
33 clear_config([:media_proxy, :enabled], true)
34 clear_config([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
35 [url: MediaProxy.encode_url("https://google.fn/test.png")]
38 test "it returns 403 for invalid signature", %{conn: conn, url: url} do
39 Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
40 %{path: path} = URI.parse(url)
44 resp_body: "Forbidden"
49 resp_body: "Forbidden"
50 } = get(conn, "/proxy/hhgfh/eeee")
54 resp_body: "Forbidden"
55 } = get(conn, "/proxy/hhgfh/eeee/fff")
58 test "redirects on valid url when filename is invalidated", %{conn: conn, url: url} do
59 invalid_url = String.replace(url, "test.png", "test-file.png")
60 response = get(conn, invalid_url)
61 assert response.status == 302
62 assert redirected_to(response) == url
65 test "it performs ReverseProxy.call with valid signature", %{conn: conn, url: url} do
66 with_mock Pleroma.ReverseProxy,
67 call: fn _conn, _url, _opts -> %Conn{status: :success} end do
68 assert %Conn{status: :success} = get(conn, url)
72 test "it returns 404 when url is in banned_urls cache", %{conn: conn, url: url} do
73 MediaProxy.put_in_banned_urls("https://google.fn/test.png")
75 with_mock Pleroma.ReverseProxy,
76 call: fn _conn, _url, _opts -> %Conn{status: :success} end do
77 assert %Conn{status: 404, resp_body: "Not Found"} = get(conn, url)