summaryrefslogtreecommitdiff
path: root/test/pleroma/web/admin_api/controllers/frontend_controller_test.exs
blob: bc827cc12ee48cb2f10905ca66864b29346ac7b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do
  use Pleroma.Web.ConnCase

  import Pleroma.Factory

  alias Pleroma.Config

  @dir "test/frontend_static_test"

  setup do
    clear_config([:instance, :static_dir], @dir)
    File.mkdir_p!(Pleroma.Frontend.dir())

    on_exit(fn ->
      File.rm_rf(@dir)
    end)

    admin = insert(:user, is_admin: true)
    token = insert(:oauth_admin_token, user: admin)

    conn =
      build_conn()
      |> assign(:user, admin)
      |> assign(:token, token)

    {:ok, %{admin: admin, token: token, conn: conn}}
  end

  describe "GET /api/pleroma/admin/frontends" do
    test "it lists available frontends", %{conn: conn} do
      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/install" do
    test "from available frontends", %{conn: conn} do
      clear_config([:frontends, :available], %{
        "pleroma" => %{
          "ref" => "fantasy",
          "name" => "pleroma",
          "build_url" => "http://gensokyo.2hu/builds/${ref}"
        }
      })

      Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} ->
        %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")}
      end)

      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"})
      |> json_response_and_validate_schema(:ok)

      assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))

      response =
        conn
        |> get("/api/pleroma/admin/frontends")
        |> json_response_and_validate_schema(:ok)

      assert response == [
               %{
                 "build_url" => "http://gensokyo.2hu/builds/${ref}",
                 "git" => nil,
                 "installed" => true,
                 "name" => "pleroma",
                 "ref" => "fantasy"
               }
             ]
    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