Merge branch 'develop' into feature/gen-magic
[akkoma] / test / web / media_proxy / media_proxy_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Mock
9
10 alias Pleroma.Web.MediaProxy
11 alias Pleroma.Web.MediaProxy.MediaProxyController
12 alias Plug.Conn
13
14 setup do
15 on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
16 end
17
18 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
19 clear_config([:media_proxy, :enabled], false)
20
21 assert %Conn{
22 status: 404,
23 resp_body: "Not Found"
24 } = get(conn, "/proxy/hhgfh/eeeee")
25
26 assert %Conn{
27 status: 404,
28 resp_body: "Not Found"
29 } = get(conn, "/proxy/hhgfh/eeee/fff")
30 end
31
32 describe "" do
33 setup do
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")]
37 end
38
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)
42
43 assert %Conn{
44 status: 403,
45 resp_body: "Forbidden"
46 } = get(conn, path)
47
48 assert %Conn{
49 status: 403,
50 resp_body: "Forbidden"
51 } = get(conn, "/proxy/hhgfh/eeee")
52
53 assert %Conn{
54 status: 403,
55 resp_body: "Forbidden"
56 } = get(conn, "/proxy/hhgfh/eeee/fff")
57 end
58
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
64 end
65
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)
70 end
71 end
72
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")
75
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)
79 end
80 end
81 end
82
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"},
87 "/Hello world.jpg",
88 "http://pleroma.social/Hello world.jpg"
89 ) == :ok
90
91 assert MediaProxyController.filename_matches(
92 %{"filename" => "/Hello%20world.jpg"},
93 "/Hello%20world.jpg",
94 "http://pleroma.social/Hello%20world.jpg"
95 ) == :ok
96
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"
101 ) == :ok
102
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"}
108 end
109
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"
113
114 assert MediaProxyController.filename_matches(
115 true,
116 request_path,
117 "https://mydomain.com/uploads/2019/07/ANALYSE-DAI-_-LE-STABLECOIN-100-DÉCENTRALISÉ-BQ.jpg"
118 ) == :ok
119 end
120 end
121 end