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.InstanceDocumentControllerTest do
6 use Pleroma.Web.ConnCase, async: true
9 @dir "test/tmp/instance_static"
10 @default_instance_panel ~s(<p>Welcome to <a href="https://akkoma.dev" target="_blank">Akkoma</a>!</p>)
14 on_exit(fn -> File.rm_rf(@dir) end)
17 setup do: clear_config([:instance, :static_dir], @dir)
20 admin = insert(:user, is_admin: true)
21 token = insert(:oauth_admin_token, user: admin)
25 |> assign(:user, admin)
26 |> assign(:token, token)
28 {:ok, %{admin: admin, token: token, conn: conn}}
31 describe "GET /api/pleroma/admin/instance_document/:name" do
32 test "return the instance document url", %{conn: conn} do
33 conn = get(conn, "/api/pleroma/admin/instance_document/instance-panel")
35 assert content = html_response(conn, 200)
36 assert String.contains?(content, @default_instance_panel)
39 test "it returns 403 if requested by a non-admin" do
40 non_admin_user = insert(:user)
41 token = insert(:oauth_token, user: non_admin_user)
45 |> assign(:user, non_admin_user)
46 |> assign(:token, token)
47 |> get("/api/pleroma/admin/instance_document/instance-panel")
49 assert json_response(conn, :forbidden)
52 test "it returns 404 if the instance document with the given name doesn't exist", %{
55 conn = get(conn, "/api/pleroma/admin/instance_document/1234")
57 assert json_response_and_validate_schema(conn, 404)
61 describe "PATCH /api/pleroma/admin/instance_document/:name" do
62 test "uploads the instance document", %{conn: conn} do
64 content_type: "text/html",
65 path: Path.absname("test/fixtures/custom_instance_panel.html"),
66 filename: "custom_instance_panel.html"
71 |> put_req_header("content-type", "multipart/form-data")
72 |> patch("/api/pleroma/admin/instance_document/instance-panel", %{
76 assert %{"url" => url} = json_response_and_validate_schema(conn, 200)
77 index = get(build_conn(), url)
78 assert html_response(index, 200) == "<h2>Custom instance panel</h2>"
82 describe "DELETE /api/pleroma/admin/instance_document/:name" do
83 test "deletes the instance document", %{conn: conn} do
84 File.mkdir!(@dir <> "/instance/")
85 File.write!(@dir <> "/instance/panel.html", "Custom instance panel")
89 |> get("/api/pleroma/admin/instance_document/instance-panel")
91 assert html_response(conn_resp, 200) == "Custom instance panel"
94 |> delete("/api/pleroma/admin/instance_document/instance-panel")
95 |> json_response_and_validate_schema(200)
99 |> get("/api/pleroma/admin/instance_document/instance-panel")
101 assert content = html_response(conn_resp, 200)
102 assert String.contains?(content, @default_instance_panel)