1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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}",
98 test "from a file", %{conn: conn} do
99 clear_config([:frontends, :available], %{
108 |> put_req_header("content-type", "application/json")
109 |> post("/api/pleroma/admin/frontends/install", %{
111 file: "test/fixtures/tesla_mock/frontend.zip"
113 |> json_response_and_validate_schema(:ok)
115 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
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")}
124 |> put_req_header("content-type", "application/json")
125 |> post("/api/pleroma/admin/frontends/install", %{
128 build_url: "http://gensokyo.2hu/madeup.zip",
131 |> json_response_and_validate_schema(:ok)
133 assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
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: ""}
143 |> put_req_header("content-type", "application/json")
144 |> post("/api/pleroma/admin/frontends/install", %{
147 build_url: "http://gensokyo.2hu/madeup.zip",
150 |> json_response_and_validate_schema(400)
152 assert result == %{"error" => "Could not download or unzip the frontend"}