X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fpleroma%2Fweb%2Fadmin_api%2Fcontrollers%2Ffrontend_controller_test.exs;h=200682ba92d6dde618c305b04a0aa87de3219a3a;hb=838da53ea72cba73fb4643194a102d55a90ea535;hp=461d6e5c93d99c694a4516140064acefcbdd6963;hpb=03e306785b2013fe6fd47b59d4e578c6ed586b08;p=akkoma diff --git a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs index 461d6e5c9..200682ba9 100644 --- a/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs @@ -1,16 +1,13 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors +# Copyright © 2017-2021 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do use Pleroma.Web.ConnCase - use Oban.Testing, repo: Pleroma.Repo import Pleroma.Factory alias Pleroma.Config - alias Pleroma.Tests.ObanHelpers - alias Pleroma.Workers.FrontendInstallerWorker @dir "test/frontend_static_test" @@ -45,10 +42,24 @@ defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do refute Enum.any?(response, fn frontend -> frontend["installed"] == true end) end + + test "it lists available frontends when no frontend folder was created yet", %{conn: conn} do + File.rm_rf(@dir) + + response = + conn + |> get("/api/pleroma/admin/frontends") + |> json_response_and_validate_schema(:ok) + + assert Enum.map(response, & &1["name"]) == + Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end) + + refute Enum.any?(response, fn frontend -> frontend["installed"] == true end) + end end - describe "POST /api/pleroma/admin/frontends" do - test "it installs a frontend", %{conn: conn} do + describe "POST /api/pleroma/admin/frontends/install" do + test "from available frontends", %{conn: conn} do clear_config([:frontends, :available], %{ "pleroma" => %{ "ref" => "fantasy", @@ -63,16 +74,9 @@ defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do conn |> put_req_header("content-type", "application/json") - |> post("/api/pleroma/admin/frontends", %{name: "pleroma"}) + |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"}) |> json_response_and_validate_schema(:ok) - assert_enqueued( - worker: FrontendInstallerWorker, - args: %{"name" => "pleroma", "opts" => %{}} - ) - - ObanHelpers.perform(all_enqueued(worker: FrontendInstallerWorker)) - assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) response = @@ -90,5 +94,62 @@ defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do } ] end + + test "from a file", %{conn: conn} do + clear_config([:frontends, :available], %{ + "pleroma" => %{ + "ref" => "fantasy", + "name" => "pleroma", + "build_dir" => "" + } + }) + + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/frontends/install", %{ + name: "pleroma", + file: "test/fixtures/tesla_mock/frontend.zip" + }) + |> json_response_and_validate_schema(:ok) + + assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"])) + end + + test "from an URL", %{conn: conn} do + Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} -> + %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")} + end) + + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/frontends/install", %{ + name: "unknown", + ref: "baka", + build_url: "http://gensokyo.2hu/madeup.zip", + build_dir: "" + }) + |> json_response_and_validate_schema(:ok) + + assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"])) + end + + test "failing returns an error", %{conn: conn} do + Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} -> + %Tesla.Env{status: 404, body: ""} + end) + + result = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/pleroma/admin/frontends/install", %{ + name: "unknown", + ref: "baka", + build_url: "http://gensokyo.2hu/madeup.zip", + build_dir: "" + }) + |> json_response_and_validate_schema(400) + + assert result == %{"error" => "Could not download or unzip the frontend"} + end end end