fix tests
[akkoma] / test / pleroma / web / plugs / frontend_static_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 "akkoma",
87 "objects",
88 "activities",
89 "notice",
90 "@:nickname",
91 ":nickname",
92 "users",
93 "tags",
94 "mailer",
95 "inbox",
96 "relay",
97 "internal",
98 ".well-known",
99 "nodeinfo",
100 "manifest.json",
101 "web",
102 "auth",
103 "embed",
104 "proxy",
105 "phoenix",
106 "test",
107 "user_exists",
108 "check_password"
109 ]
110
111 assert expected_routes == Pleroma.Web.Router.get_api_routes()
112 end
113 end