Merge branch 'fix/mediaproxy-config-descriptions' into 'develop'
[akkoma] / test / pleroma / web / admin_api / controllers / frontend_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.FrontendControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Config
11
12 @dir "test/frontend_static_test"
13
14 setup do
15 clear_config([:instance, :static_dir], @dir)
16 File.mkdir_p!(Pleroma.Frontend.dir())
17
18 on_exit(fn ->
19 File.rm_rf(@dir)
20 end)
21
22 admin = insert(:user, is_admin: true)
23 token = insert(:oauth_admin_token, user: admin)
24
25 conn =
26 build_conn()
27 |> assign(:user, admin)
28 |> assign(:token, token)
29
30 {:ok, %{admin: admin, token: token, conn: conn}}
31 end
32
33 describe "GET /api/pleroma/admin/frontends" do
34 test "it lists available frontends", %{conn: conn} do
35 response =
36 conn
37 |> get("/api/pleroma/admin/frontends")
38 |> json_response_and_validate_schema(:ok)
39
40 assert Enum.map(response, & &1["name"]) ==
41 Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
42
43 refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
44 end
45 end
46
47 describe "POST /api/pleroma/admin/frontends/install" do
48 test "from available frontends", %{conn: conn} do
49 clear_config([:frontends, :available], %{
50 "pleroma" => %{
51 "ref" => "fantasy",
52 "name" => "pleroma",
53 "build_url" => "http://gensokyo.2hu/builds/${ref}"
54 }
55 })
56
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")}
59 end)
60
61 conn
62 |> put_req_header("content-type", "application/json")
63 |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"})
64 |> json_response_and_validate_schema(:ok)
65
66 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
67
68 response =
69 conn
70 |> get("/api/pleroma/admin/frontends")
71 |> json_response_and_validate_schema(:ok)
72
73 assert response == [
74 %{
75 "build_url" => "http://gensokyo.2hu/builds/${ref}",
76 "git" => nil,
77 "installed" => true,
78 "name" => "pleroma",
79 "ref" => "fantasy"
80 }
81 ]
82 end
83
84 test "from a file", %{conn: conn} do
85 clear_config([:frontends, :available], %{
86 "pleroma" => %{
87 "ref" => "fantasy",
88 "name" => "pleroma",
89 "build_dir" => ""
90 }
91 })
92
93 conn
94 |> put_req_header("content-type", "application/json")
95 |> post("/api/pleroma/admin/frontends/install", %{
96 name: "pleroma",
97 file: "test/fixtures/tesla_mock/frontend.zip"
98 })
99 |> json_response_and_validate_schema(:ok)
100
101 assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
102 end
103
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")}
107 end)
108
109 conn
110 |> put_req_header("content-type", "application/json")
111 |> post("/api/pleroma/admin/frontends/install", %{
112 name: "unknown",
113 ref: "baka",
114 build_url: "http://gensokyo.2hu/madeup.zip",
115 build_dir: ""
116 })
117 |> json_response_and_validate_schema(:ok)
118
119 assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
120 end
121
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: ""}
125 end)
126
127 result =
128 conn
129 |> put_req_header("content-type", "application/json")
130 |> post("/api/pleroma/admin/frontends/install", %{
131 name: "unknown",
132 ref: "baka",
133 build_url: "http://gensokyo.2hu/madeup.zip",
134 build_dir: ""
135 })
136 |> json_response_and_validate_schema(400)
137
138 assert result == %{"error" => "Could not download or unzip the frontend"}
139 end
140 end
141 end