200682ba92d6dde618c305b04a0aa87de3219a3a
[akkoma] / test / pleroma / web / admin_api / controllers / frontend_controller_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.AdminAPI.FrontendControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Config
11
12 @dir "test/frontend_static_test"
13
14 setup do
15 clear_config([:instance, :static_dir], @dir)
16 File.mkdir_p!(Pleroma.Frontend.dir())
17
18 on_exit(fn ->
19 File.rm_rf(@dir)
20 end)
21
22 admin = insert(:user, is_admin: true)
23 token = insert(:oauth_admin_token, user: admin)
24
25 conn =
26 build_conn()
27 |> assign(:user, admin)
28 |> assign(:token, token)
29
30 {:ok, %{admin: admin, token: token, conn: conn}}
31 end
32
33 describe "GET /api/pleroma/admin/frontends" do
34 test "it lists available frontends", %{conn: conn} do
35 response =
36 conn
37 |> get("/api/pleroma/admin/frontends")
38 |> json_response_and_validate_schema(:ok)
39
40 assert Enum.map(response, & &1["name"]) ==
41 Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
42
43 refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
44 end
45
46 test "it lists available frontends when no frontend folder was created yet", %{conn: conn} do
47 File.rm_rf(@dir)
48
49 response =
50 conn
51 |> get("/api/pleroma/admin/frontends")
52 |> json_response_and_validate_schema(:ok)
53
54 assert Enum.map(response, & &1["name"]) ==
55 Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
56
57 refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
58 end
59 end
60
61 describe "POST /api/pleroma/admin/frontends/install" do
62 test "from available frontends", %{conn: conn} do
63 clear_config([:frontends, :available], %{
64 "pleroma" => %{
65 "ref" => "fantasy",
66 "name" => "pleroma",
67 "build_url" => "http://gensokyo.2hu/builds/${ref}"
68 }
69 })
70
71 Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} ->
72 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")}
73 end)
74
75 conn
76 |> put_req_header("content-type", "application/json")
77 |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"})
78 |> json_response_and_validate_schema(:ok)
79
80 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
81
82 response =
83 conn
84 |> get("/api/pleroma/admin/frontends")
85 |> json_response_and_validate_schema(:ok)
86
87 assert response == [
88 %{
89 "build_url" => "http://gensokyo.2hu/builds/${ref}",
90 "git" => nil,
91 "installed" => true,
92 "name" => "pleroma",
93 "ref" => "fantasy"
94 }
95 ]
96 end
97
98 test "from a file", %{conn: conn} do
99 clear_config([:frontends, :available], %{
100 "pleroma" => %{
101 "ref" => "fantasy",
102 "name" => "pleroma",
103 "build_dir" => ""
104 }
105 })
106
107 conn
108 |> put_req_header("content-type", "application/json")
109 |> post("/api/pleroma/admin/frontends/install", %{
110 name: "pleroma",
111 file: "test/fixtures/tesla_mock/frontend.zip"
112 })
113 |> json_response_and_validate_schema(:ok)
114
115 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
116 end
117
118 test "from an URL", %{conn: conn} do
119 Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
120 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")}
121 end)
122
123 conn
124 |> put_req_header("content-type", "application/json")
125 |> post("/api/pleroma/admin/frontends/install", %{
126 name: "unknown",
127 ref: "baka",
128 build_url: "http://gensokyo.2hu/madeup.zip",
129 build_dir: ""
130 })
131 |> json_response_and_validate_schema(:ok)
132
133 assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
134 end
135
136 test "failing returns an error", %{conn: conn} do
137 Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
138 %Tesla.Env{status: 404, body: ""}
139 end)
140
141 result =
142 conn
143 |> put_req_header("content-type", "application/json")
144 |> post("/api/pleroma/admin/frontends/install", %{
145 name: "unknown",
146 ref: "baka",
147 build_url: "http://gensokyo.2hu/madeup.zip",
148 build_dir: ""
149 })
150 |> json_response_and_validate_schema(400)
151
152 assert result == %{"error" => "Could not download or unzip the frontend"}
153 end
154 end
155 end