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 setup do: clear_config(:media_proxy)
11 setup do: clear_config([Pleroma.Web.Endpoint, :secret_key_base])
13 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
14 Config.put([:media_proxy, :enabled], false)
18 resp_body: "Not Found"
19 } = get(conn, "/proxy/hhgfh/eeeee")
23 resp_body: "Not Found"
24 } = get(conn, "/proxy/hhgfh/eeee/fff")
27 test "it returns 403 when signature invalidated", %{conn: conn} do
28 Config.put([:media_proxy, :enabled], true)
29 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
30 path = URI.parse(Pleroma.Web.MediaProxy.encode_url("https://google.fn")).path
31 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
35 resp_body: "Forbidden"
40 resp_body: "Forbidden"
41 } = get(conn, "/proxy/hhgfh/eeee")
45 resp_body: "Forbidden"
46 } = get(conn, "/proxy/hhgfh/eeee/fff")
49 test "redirects on valid url when filename invalidated", %{conn: conn} do
50 Config.put([:media_proxy, :enabled], true)
51 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
52 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
53 invalid_url = String.replace(url, "test.png", "test-file.png")
54 response = get(conn, invalid_url)
55 assert response.status == 302
56 assert redirected_to(response) == url
59 test "it performs ReverseProxy.call when signature valid", %{conn: conn} do
60 Config.put([:media_proxy, :enabled], true)
61 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
62 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
64 with_mock Pleroma.ReverseProxy,
65 call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
66 assert %Plug.Conn{status: :success} = get(conn, url)