1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
11 media_proxy_config = Config.get([:media_proxy]) || []
12 on_exit(fn -> Config.put([:media_proxy], media_proxy_config) end)
16 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
17 Config.put([:media_proxy, :enabled], false)
21 resp_body: "Not Found"
22 } = get(conn, "/proxy/hhgfh/eeeee")
26 resp_body: "Not Found"
27 } = get(conn, "/proxy/hhgfh/eeee/fff")
30 test "it returns 403 when signature invalidated", %{conn: conn} do
31 Config.put([:media_proxy, :enabled], true)
32 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
33 path = URI.parse(Pleroma.Web.MediaProxy.encode_url("https://google.fn")).path
34 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
38 resp_body: "Forbidden"
43 resp_body: "Forbidden"
44 } = get(conn, "/proxy/hhgfh/eeee")
48 resp_body: "Forbidden"
49 } = get(conn, "/proxy/hhgfh/eeee/fff")
52 test "redirects on valid url when filename invalidated", %{conn: conn} do
53 Config.put([:media_proxy, :enabled], true)
54 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
55 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
56 invalid_url = String.replace(url, "test.png", "test-file.png")
57 response = get(conn, invalid_url)
58 html = "<html><body>You are being <a href=\"#{url}\">redirected</a>.</body></html>"
59 assert response.status == 302
60 assert response.resp_body == html
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")
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)