0cda1e0b089cf63c750462e48855e2f9d2ce1df6
[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 end