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.InstanceDocumentControllerTest do
6 use Pleroma.Web.ConnCase, async: true
10 @dir "test/tmp/instance_static"
11 @default_instance_panel ~s(<p>Welcome to <a href="https://pleroma.social" target="_blank">Pleroma!</a></p>)
15 on_exit(fn -> File.rm_rf(@dir) end)
18 setup do: clear_config([:instance, :static_dir], @dir)
21 admin = insert(:user, is_admin: true)
22 token = insert(:oauth_admin_token, user: admin)
26 |> assign(:user, admin)
27 |> assign(:token, token)
29 {:ok, %{admin: admin, token: token, conn: conn}}
32 describe "GET /api/pleroma/admin/instance_document/:name" do
33 test "return the instance document url", %{conn: conn} do
34 conn = get(conn, "/api/pleroma/admin/instance_document/instance-panel")
36 assert content = html_response(conn, 200)
37 assert String.contains?(content, @default_instance_panel)
40 test "it returns 403 if requested by a non-admin" do
41 non_admin_user = insert(:user)
42 token = insert(:oauth_token, user: non_admin_user)
46 |> assign(:user, non_admin_user)
47 |> assign(:token, token)
48 |> get("/api/pleroma/admin/instance_document/instance-panel")
50 assert json_response(conn, :forbidden)
53 test "it returns 404 if the instance document with the given name doesn't exist", %{
56 conn = get(conn, "/api/pleroma/admin/instance_document/1234")
58 assert json_response_and_validate_schema(conn, 404)
62 describe "PATCH /api/pleroma/admin/instance_document/:name" do
63 test "uploads the instance document", %{conn: conn} do
65 content_type: "text/html",
66 path: Path.absname("test/fixtures/custom_instance_panel.html"),
67 filename: "custom_instance_panel.html"
72 |> put_req_header("content-type", "multipart/form-data")
73 |> patch("/api/pleroma/admin/instance_document/instance-panel", %{
77 assert %{"url" => url} = json_response_and_validate_schema(conn, 200)
78 index = get(build_conn(), url)
79 assert html_response(index, 200) == "<h2>Custom instance panel</h2>"
83 describe "DELETE /api/pleroma/admin/instance_document/:name" do
84 test "deletes the instance document", %{conn: conn} do
85 File.mkdir!(@dir <> "/instance/")
86 File.write!(@dir <> "/instance/panel.html", "Custom instance panel")
90 |> get("/api/pleroma/admin/instance_document/instance-panel")
92 assert html_response(conn_resp, 200) == "Custom instance panel"
95 |> delete("/api/pleroma/admin/instance_document/instance-panel")
96 |> json_response_and_validate_schema(200)
100 |> get("/api/pleroma/admin/instance_document/instance-panel")
102 assert content = html_response(conn_resp, 200)
103 assert String.contains?(content, @default_instance_panel)