1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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)
47 describe "POST /api/pleroma/admin/frontends/install" do
48 test "from available frontends", %{conn: conn} do
49 clear_config([:frontends, :available], %{
53 "build_url" => "http://gensokyo.2hu/builds/${ref}"
57 Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} ->
58 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")}
62 |> put_req_header("content-type", "application/json")
63 |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"})
64 |> json_response_and_validate_schema(:ok)
66 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
70 |> get("/api/pleroma/admin/frontends")
71 |> json_response_and_validate_schema(:ok)
75 "build_url" => "http://gensokyo.2hu/builds/${ref}",
84 test "from a file", %{conn: conn} do
85 clear_config([:frontends, :available], %{
94 |> put_req_header("content-type", "application/json")
95 |> post("/api/pleroma/admin/frontends/install", %{
97 file: "test/fixtures/tesla_mock/frontend.zip"
99 |> json_response_and_validate_schema(:ok)
101 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
104 test "from an URL", %{conn: conn} do
105 Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
106 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")}
110 |> put_req_header("content-type", "application/json")
111 |> post("/api/pleroma/admin/frontends/install", %{
114 build_url: "http://gensokyo.2hu/madeup.zip",
117 |> json_response_and_validate_schema(:ok)
119 assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
122 test "failing returns an error", %{conn: conn} do
123 Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
124 %Tesla.Env{status: 404, body: ""}
129 |> put_req_header("content-type", "application/json")
130 |> post("/api/pleroma/admin/frontends/install", %{
133 build_url: "http://gensokyo.2hu/madeup.zip",
136 |> json_response_and_validate_schema(400)
138 assert result == %{"error" => "Could not download or unzip the frontend"}