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