1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.FrontendStaticPlugTest do
6 use Pleroma.Web.ConnCase
9 @dir "test/tmp/instance_static"
13 on_exit(fn -> File.rm_rf(@dir) end)
16 setup do: clear_config([:instance, :static_dir], @dir)
18 test "init will give a static plug config + the frontend type" do
24 |> Pleroma.Web.Plugs.FrontendStatic.init()
26 assert opts[:at] == ["admin"]
27 assert opts[:frontend_type] == :admin
30 test "overrides existing static files", %{conn: conn} do
34 clear_config([:frontends, :primary], %{"name" => name, "ref" => ref})
35 path = "#{@dir}/frontends/#{name}/#{ref}"
38 File.write!("#{path}/index.html", "from frontend plug")
40 index = get(conn, "/")
41 assert html_response(index, 200) == "from frontend plug"
44 test "overrides existing static files for the `pleroma/admin` path", %{conn: conn} do
48 clear_config([:frontends, :admin], %{"name" => name, "ref" => ref})
49 path = "#{@dir}/frontends/#{name}/#{ref}"
52 File.write!("#{path}/index.html", "from frontend plug")
54 index = get(conn, "/pleroma/admin/")
55 assert html_response(index, 200) == "from frontend plug"
58 test "exclude invalid path", %{conn: conn} do
61 clear_config([:media_proxy, :enabled], true)
62 clear_config([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
63 clear_config([:frontends, :primary], %{"name" => name, "ref" => ref})
64 path = "#{@dir}/frontends/#{name}/#{ref}"
66 File.mkdir_p!("#{path}/proxy/rr/ss")
67 File.write!("#{path}/proxy/rr/ss/Ek7w8WPVcAApOvN.jpg:large", "FB image")
70 Pleroma.Web.MediaProxy.encode_url("https://pbs.twimg.com/media/Ek7w8WPVcAApOvN.jpg:large")
72 with_mock Pleroma.ReverseProxy,
73 call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
74 assert %Plug.Conn{status: :success} = get(conn, url)
78 test "api routes are detected correctly" do
79 # If this test fails we have probably added something
80 # new that should be in /api/ instead
106 assert expected_routes == Pleroma.Web.get_api_routes()