logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

instance_document_controller_test.exs (3291B)


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