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)
83 describe "filename_matches/3" do
84 test "preserves the encoded or decoded path" do
85 assert MediaProxyController.filename_matches(
86 %{"filename" => "/Hello world.jpg"},
88 "http://pleroma.social/Hello world.jpg"
91 assert MediaProxyController.filename_matches(
92 %{"filename" => "/Hello%20world.jpg"},
94 "http://pleroma.social/Hello%20world.jpg"
97 assert MediaProxyController.filename_matches(
98 %{"filename" => "/my%2Flong%2Furl%2F2019%2F07%2FS.jpg"},
99 "/my%2Flong%2Furl%2F2019%2F07%2FS.jpg",
100 "http://pleroma.social/my%2Flong%2Furl%2F2019%2F07%2FS.jpg"
103 assert MediaProxyController.filename_matches(
104 %{"filename" => "/my%2Flong%2Furl%2F2019%2F07%2FS.jp"},
105 "/my%2Flong%2Furl%2F2019%2F07%2FS.jp",
106 "http://pleroma.social/my%2Flong%2Furl%2F2019%2F07%2FS.jpg"
107 ) == {:wrong_filename, "my%2Flong%2Furl%2F2019%2F07%2FS.jpg"}
110 test "encoded url are tried to match for proxy as `conn.request_path` encodes the url" do
111 # conn.request_path will return encoded url
112 request_path = "/ANALYSE-DAI-_-LE-STABLECOIN-100-D%C3%89CENTRALIS%C3%89-BQ.jpg"
114 assert MediaProxyController.filename_matches(
117 "https://mydomain.com/uploads/2019/07/ANALYSE-DAI-_-LE-STABLECOIN-100-DÉCENTRALISÉ-BQ.jpg"