logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

instance_document_controller_test.exs (3327B)


  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. defmodule Pleroma.Web.AdminAPI.InstanceDocumentControllerTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. import Pleroma.Factory
  7. alias Pleroma.Config
  8. @dir "test/tmp/instance_static"
  9. @default_instance_panel ~s(<p>Welcome to <a href="https://pleroma.social" target="_blank">Pleroma!</a></p>)
  10. setup do
  11. File.mkdir_p!(@dir)
  12. on_exit(fn -> File.rm_rf(@dir) end)
  13. end
  14. setup do: clear_config([:instance, :static_dir], @dir)
  15. setup do
  16. admin = insert(:user, is_admin: true)
  17. token = insert(:oauth_admin_token, user: admin)
  18. conn =
  19. build_conn()
  20. |> assign(:user, admin)
  21. |> assign(:token, token)
  22. {:ok, %{admin: admin, token: token, conn: conn}}
  23. end
  24. describe "GET /api/pleroma/admin/instance_document/:name" do
  25. test "return the instance document url", %{conn: conn} do
  26. conn = get(conn, "/api/pleroma/admin/instance_document/instance-panel")
  27. assert content = html_response(conn, 200)
  28. assert String.contains?(content, @default_instance_panel)
  29. end
  30. test "it returns 403 if requested by a non-admin" do
  31. non_admin_user = insert(:user)
  32. token = insert(:oauth_token, user: non_admin_user)
  33. conn =
  34. build_conn()
  35. |> assign(:user, non_admin_user)
  36. |> assign(:token, token)
  37. |> get("/api/pleroma/admin/instance_document/instance-panel")
  38. assert json_response(conn, :forbidden)
  39. end
  40. test "it returns 404 if the instance document with the given name doesn't exist", %{
  41. conn: conn
  42. } do
  43. conn = get(conn, "/api/pleroma/admin/instance_document/1234")
  44. assert json_response_and_validate_schema(conn, 404)
  45. end
  46. end
  47. describe "PATCH /api/pleroma/admin/instance_document/:name" do
  48. test "uploads the instance document", %{conn: conn} do
  49. image = %Plug.Upload{
  50. content_type: "text/html",
  51. path: Path.absname("test/fixtures/custom_instance_panel.html"),
  52. filename: "custom_instance_panel.html"
  53. }
  54. conn =
  55. conn
  56. |> put_req_header("content-type", "multipart/form-data")
  57. |> patch("/api/pleroma/admin/instance_document/instance-panel", %{
  58. "file" => image
  59. })
  60. assert %{"url" => url} = json_response_and_validate_schema(conn, 200)
  61. index = get(build_conn(), url)
  62. assert html_response(index, 200) == "<h2>Custom instance panel</h2>"
  63. end
  64. end
  65. describe "DELETE /api/pleroma/admin/instance_document/:name" do
  66. test "deletes the instance document", %{conn: conn} do
  67. File.mkdir!(@dir <> "/instance/")
  68. File.write!(@dir <> "/instance/panel.html", "Custom instance panel")
  69. conn_resp =
  70. conn
  71. |> get("/api/pleroma/admin/instance_document/instance-panel")
  72. assert html_response(conn_resp, 200) == "Custom instance panel"
  73. conn
  74. |> delete("/api/pleroma/admin/instance_document/instance-panel")
  75. |> json_response_and_validate_schema(200)
  76. conn_resp =
  77. conn
  78. |> get("/api/pleroma/admin/instance_document/instance-panel")
  79. assert content = html_response(conn_resp, 200)
  80. assert String.contains?(content, @default_instance_panel)
  81. end
  82. end
  83. end