total rebase
[anni] / test / pleroma / web / admin_api / controllers / frontend_controller_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.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                  "installed_refs" => ["fantasy"],
93                  "name" => "pleroma",
94                  "ref" => "fantasy"
95                }
96              ]
97     end
98
99     test "from a file", %{conn: conn} do
100       clear_config([:frontends, :available], %{
101         "pleroma" => %{
102           "ref" => "fantasy",
103           "name" => "pleroma",
104           "build_dir" => ""
105         }
106       })
107
108       conn
109       |> put_req_header("content-type", "application/json")
110       |> post("/api/pleroma/admin/frontends/install", %{
111         name: "pleroma",
112         file: "test/fixtures/tesla_mock/frontend.zip"
113       })
114       |> json_response_and_validate_schema(:ok)
115
116       assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
117     end
118
119     test "from an URL", %{conn: conn} do
120       Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
121         %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")}
122       end)
123
124       conn
125       |> put_req_header("content-type", "application/json")
126       |> post("/api/pleroma/admin/frontends/install", %{
127         name: "unknown",
128         ref: "baka",
129         build_url: "http://gensokyo.2hu/madeup.zip",
130         build_dir: ""
131       })
132       |> json_response_and_validate_schema(:ok)
133
134       assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
135     end
136
137     test "failing returns an error", %{conn: conn} do
138       Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
139         %Tesla.Env{status: 404, body: ""}
140       end)
141
142       result =
143         conn
144         |> put_req_header("content-type", "application/json")
145         |> post("/api/pleroma/admin/frontends/install", %{
146           name: "unknown",
147           ref: "baka",
148           build_url: "http://gensokyo.2hu/madeup.zip",
149           build_dir: ""
150         })
151         |> json_response_and_validate_schema(400)
152
153       assert result == %{"error" => "Could not download or unzip the frontend"}
154     end
155   end
156 end