1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do
6 use Pleroma.Web.ConnCase
12 @dir "test/frontend_static_test"
15 clear_config([:instance, :static_dir], @dir)
16 File.mkdir_p!(Pleroma.Frontend.dir())
22 admin = insert(:user, is_admin: true)
23 token = insert(:oauth_admin_token, user: admin)
27 |> assign(:user, admin)
28 |> assign(:token, token)
30 {:ok, %{admin: admin, token: token, conn: conn}}
33 describe "GET /api/pleroma/admin/frontends" do
34 test "it lists available frontends", %{conn: conn} do
37 |> get("/api/pleroma/admin/frontends")
38 |> json_response_and_validate_schema(:ok)
40 assert Enum.map(response, & &1["name"]) ==
41 Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
43 refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
46 test "it lists available frontends when no frontend folder was created yet", %{conn: conn} do
51 |> get("/api/pleroma/admin/frontends")
52 |> json_response_and_validate_schema(:ok)
54 assert Enum.map(response, & &1["name"]) ==
55 Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
57 refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
61 describe "POST /api/pleroma/admin/frontends/install" do
62 test "from available frontends", %{conn: conn} do
63 clear_config([:frontends, :available], %{
67 "build_url" => "http://gensokyo.2hu/builds/${ref}"
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")}
76 |> put_req_header("content-type", "application/json")
77 |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"})
78 |> json_response_and_validate_schema(:ok)
80 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
84 |> get("/api/pleroma/admin/frontends")
85 |> json_response_and_validate_schema(:ok)
89 "build_url" => "http://gensokyo.2hu/builds/${ref}",
92 "installed_refs" => ["fantasy"],
99 test "from a file", %{conn: conn} do
100 clear_config([:frontends, :available], %{
109 |> put_req_header("content-type", "application/json")
110 |> post("/api/pleroma/admin/frontends/install", %{
112 file: "test/fixtures/tesla_mock/frontend.zip"
114 |> json_response_and_validate_schema(:ok)
116 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
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")}
125 |> put_req_header("content-type", "application/json")
126 |> post("/api/pleroma/admin/frontends/install", %{
129 build_url: "http://gensokyo.2hu/madeup.zip",
132 |> json_response_and_validate_schema(:ok)
134 assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
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: ""}
144 |> put_req_header("content-type", "application/json")
145 |> post("/api/pleroma/admin/frontends/install", %{
148 build_url: "http://gensokyo.2hu/madeup.zip",
151 |> json_response_and_validate_schema(400)
153 assert result == %{"error" => "Could not download or unzip the frontend"}