ab31c5f228b46263751e28574cd55b4252ca2ea6
[anni] / test / pleroma / web / plugs / frontend_static_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.FrontendStaticPlugTest do
6   use Pleroma.Web.ConnCase
7   import Mock
8
9   @dir "test/tmp/instance_static"
10
11   setup do
12     File.mkdir_p!(@dir)
13     on_exit(fn -> File.rm_rf(@dir) end)
14   end
15
16   setup do: clear_config([:instance, :static_dir], @dir)
17
18   test "init will give a static plug config + the frontend type" do
19     opts =
20       [
21         at: "/admin",
22         frontend_type: :admin
23       ]
24       |> Pleroma.Web.Plugs.FrontendStatic.init()
25
26     assert opts[:at] == ["admin"]
27     assert opts[:frontend_type] == :admin
28   end
29
30   test "overrides existing static files", %{conn: conn} do
31     name = "pelmora"
32     ref = "uguu"
33
34     clear_config([:frontends, :primary], %{"name" => name, "ref" => ref})
35     path = "#{@dir}/frontends/#{name}/#{ref}"
36
37     File.mkdir_p!(path)
38     File.write!("#{path}/index.html", "from frontend plug")
39
40     index = get(conn, "/")
41     assert html_response(index, 200) == "from frontend plug"
42   end
43
44   test "overrides existing static files for the `pleroma/admin` path", %{conn: conn} do
45     name = "pelmora"
46     ref = "uguu"
47
48     clear_config([:frontends, :admin], %{"name" => name, "ref" => ref})
49     path = "#{@dir}/frontends/#{name}/#{ref}"
50
51     File.mkdir_p!(path)
52     File.write!("#{path}/index.html", "from frontend plug")
53
54     index = get(conn, "/pleroma/admin/")
55     assert html_response(index, 200) == "from frontend plug"
56   end
57
58   test "exclude invalid path", %{conn: conn} do
59     name = "pleroma-fe"
60     ref = "dist"
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}"
65
66     File.mkdir_p!("#{path}/proxy/rr/ss")
67     File.write!("#{path}/proxy/rr/ss/Ek7w8WPVcAApOvN.jpg:large", "FB image")
68
69     url =
70       Pleroma.Web.MediaProxy.encode_url("https://pbs.twimg.com/media/Ek7w8WPVcAApOvN.jpg:large")
71
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)
75     end
76   end
77
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
81     expected_routes = [
82       "api",
83       "main",
84       "ostatus_subscribe",
85       "oauth",
86       "objects",
87       "activities",
88       "notice",
89       "users",
90       "tags",
91       "mailer",
92       "inbox",
93       "relay",
94       "internal",
95       ".well-known",
96       "nodeinfo",
97       "manifest.json",
98       "auth",
99       "proxy",
100       "phoenix",
101       "test",
102       "user_exists",
103       "check_password"
104     ]
105
106     assert expected_routes == Pleroma.Web.Router.get_api_routes()
107   end
108 end