Merge branch 'issue/2069' into 'develop'
[akkoma] / test / pleroma / web / admin_api / controllers / instance_document_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.InstanceDocumentControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7 import Pleroma.Factory
8 alias Pleroma.Config
9
10 @dir "test/tmp/instance_static"
11 @default_instance_panel ~s(<p>Welcome to <a href="https://pleroma.social" target="_blank">Pleroma!</a></p>)
12
13 setup do
14 File.mkdir_p!(@dir)
15 on_exit(fn -> File.rm_rf(@dir) end)
16 end
17
18 setup do: clear_config([:instance, :static_dir], @dir)
19
20 setup do
21 admin = insert(:user, is_admin: true)
22 token = insert(:oauth_admin_token, user: admin)
23
24 conn =
25 build_conn()
26 |> assign(:user, admin)
27 |> assign(:token, token)
28
29 {:ok, %{admin: admin, token: token, conn: conn}}
30 end
31
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")
35
36 assert content = html_response(conn, 200)
37 assert String.contains?(content, @default_instance_panel)
38 end
39
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)
43
44 conn =
45 build_conn()
46 |> assign(:user, non_admin_user)
47 |> assign(:token, token)
48 |> get("/api/pleroma/admin/instance_document/instance-panel")
49
50 assert json_response(conn, :forbidden)
51 end
52
53 test "it returns 404 if the instance document with the given name doesn't exist", %{
54 conn: conn
55 } do
56 conn = get(conn, "/api/pleroma/admin/instance_document/1234")
57
58 assert json_response_and_validate_schema(conn, 404)
59 end
60 end
61
62 describe "PATCH /api/pleroma/admin/instance_document/:name" do
63 test "uploads the instance document", %{conn: conn} do
64 image = %Plug.Upload{
65 content_type: "text/html",
66 path: Path.absname("test/fixtures/custom_instance_panel.html"),
67 filename: "custom_instance_panel.html"
68 }
69
70 conn =
71 conn
72 |> put_req_header("content-type", "multipart/form-data")
73 |> patch("/api/pleroma/admin/instance_document/instance-panel", %{
74 "file" => image
75 })
76
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>"
80 end
81 end
82
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")
87
88 conn_resp =
89 conn
90 |> get("/api/pleroma/admin/instance_document/instance-panel")
91
92 assert html_response(conn_resp, 200) == "Custom instance panel"
93
94 conn
95 |> delete("/api/pleroma/admin/instance_document/instance-panel")
96 |> json_response_and_validate_schema(200)
97
98 conn_resp =
99 conn
100 |> get("/api/pleroma/admin/instance_document/instance-panel")
101
102 assert content = html_response(conn_resp, 200)
103 assert String.contains?(content, @default_instance_panel)
104 end
105 end
106 end