minor: Fix version dot
[akkoma] / test / web / media_proxy / media_proxy_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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
11 media_proxy_config = Config.get([:media_proxy]) || []
12 on_exit(fn -> Config.put([:media_proxy], media_proxy_config) end)
13 :ok
14 end
15
16 test "it returns 404 when MediaProxy disabled", %{conn: conn} do
17 Config.put([:media_proxy, :enabled], false)
18
19 assert %Plug.Conn{
20 status: 404,
21 resp_body: "Not Found"
22 } = get(conn, "/proxy/hhgfh/eeeee")
23
24 assert %Plug.Conn{
25 status: 404,
26 resp_body: "Not Found"
27 } = get(conn, "/proxy/hhgfh/eeee/fff")
28 end
29
30 test "it returns 403 when signature invalidated", %{conn: conn} do
31 Config.put([:media_proxy, :enabled], true)
32 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
33 path = URI.parse(Pleroma.Web.MediaProxy.encode_url("https://google.fn")).path
34 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000")
35
36 assert %Plug.Conn{
37 status: 403,
38 resp_body: "Forbidden"
39 } = get(conn, path)
40
41 assert %Plug.Conn{
42 status: 403,
43 resp_body: "Forbidden"
44 } = get(conn, "/proxy/hhgfh/eeee")
45
46 assert %Plug.Conn{
47 status: 403,
48 resp_body: "Forbidden"
49 } = get(conn, "/proxy/hhgfh/eeee/fff")
50 end
51
52 test "redirects on valid url when filename invalidated", %{conn: conn} do
53 Config.put([:media_proxy, :enabled], true)
54 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
55 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
56 invalid_url = String.replace(url, "test.png", "test-file.png")
57 response = get(conn, invalid_url)
58 html = "<html><body>You are being <a href=\"#{url}\">redirected</a>.</body></html>"
59 assert response.status == 302
60 assert response.resp_body == html
61 end
62
63 test "it performs ReverseProxy.call when signature valid", %{conn: conn} do
64 Config.put([:media_proxy, :enabled], true)
65 Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
66 url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
67
68 with_mock Pleroma.ReverseProxy,
69 call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
70 assert %Plug.Conn{status: :success} = get(conn, url)
71 end
72 end
73 end