Removed unused alias.
[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 Plug.Conn
12
13 setup do
14 on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
15 end
16
17 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
18 clear_config([:media_proxy, :enabled], false)
19
20 assert %Conn{
21 status: 404,
22 resp_body: "Not Found"
23 } = get(conn, "/proxy/hhgfh/eeeee")
24
25 assert %Conn{
26 status: 404,
27 resp_body: "Not Found"
28 } = get(conn, "/proxy/hhgfh/eeee/fff")
29 end
30
31 describe "" do
32 setup do
33 clear_config([:media_proxy, :enabled], true)
34 clear_config([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
35 [url: MediaProxy.encode_url("https://google.fn/test.png")]
36 end
37
38 test "it returns 403 for invalid signature", %{conn: conn, url: url} do
39 Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
40 %{path: path} = URI.parse(url)
41
42 assert %Conn{
43 status: 403,
44 resp_body: "Forbidden"
45 } = get(conn, path)
46
47 assert %Conn{
48 status: 403,
49 resp_body: "Forbidden"
50 } = get(conn, "/proxy/hhgfh/eeee")
51
52 assert %Conn{
53 status: 403,
54 resp_body: "Forbidden"
55 } = get(conn, "/proxy/hhgfh/eeee/fff")
56 end
57
58 test "redirects on valid url when filename is invalidated", %{conn: conn, url: url} do
59 invalid_url = String.replace(url, "test.png", "test-file.png")
60 response = get(conn, invalid_url)
61 assert response.status == 302
62 assert redirected_to(response) == url
63 end
64
65 test "it performs ReverseProxy.call with valid signature", %{conn: conn, url: url} do
66 with_mock Pleroma.ReverseProxy,
67 call: fn _conn, _url, _opts -> %Conn{status: :success} end do
68 assert %Conn{status: :success} = get(conn, url)
69 end
70 end
71
72 test "it returns 404 when url is in banned_urls cache", %{conn: conn, url: url} do
73 MediaProxy.put_in_banned_urls("https://google.fn/test.png")
74
75 with_mock Pleroma.ReverseProxy,
76 call: fn _conn, _url, _opts -> %Conn{status: :success} end do
77 assert %Conn{status: 404, resp_body: "Not Found"} = get(conn, url)
78 end
79 end
80 end
81 end