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