logo

pleroma

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

frontend_controller_test.exs (4785B)


  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.FrontendControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. alias Pleroma.Config
  8. @dir "test/frontend_static_test"
  9. setup do
  10. clear_config([:instance, :static_dir], @dir)
  11. File.mkdir_p!(Pleroma.Frontend.dir())
  12. on_exit(fn ->
  13. File.rm_rf(@dir)
  14. end)
  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/frontends" do
  24. test "it lists available frontends", %{conn: conn} do
  25. response =
  26. conn
  27. |> get("/api/pleroma/admin/frontends")
  28. |> json_response_and_validate_schema(:ok)
  29. assert Enum.map(response, & &1["name"]) ==
  30. Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
  31. refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
  32. end
  33. test "it lists available frontends when no frontend folder was created yet", %{conn: conn} do
  34. File.rm_rf(@dir)
  35. response =
  36. conn
  37. |> get("/api/pleroma/admin/frontends")
  38. |> json_response_and_validate_schema(:ok)
  39. assert Enum.map(response, & &1["name"]) ==
  40. Enum.map(Config.get([:frontends, :available]), fn {_, map} -> map["name"] end)
  41. refute Enum.any?(response, fn frontend -> frontend["installed"] == true end)
  42. end
  43. end
  44. describe "POST /api/pleroma/admin/frontends/install" do
  45. test "from available frontends", %{conn: conn} do
  46. clear_config([:frontends, :available], %{
  47. "pleroma" => %{
  48. "ref" => "fantasy",
  49. "name" => "pleroma",
  50. "build_url" => "http://gensokyo.2hu/builds/${ref}"
  51. }
  52. })
  53. Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} ->
  54. %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")}
  55. end)
  56. conn
  57. |> put_req_header("content-type", "application/json")
  58. |> post("/api/pleroma/admin/frontends/install", %{name: "pleroma"})
  59. |> json_response_and_validate_schema(:ok)
  60. assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
  61. response =
  62. conn
  63. |> get("/api/pleroma/admin/frontends")
  64. |> json_response_and_validate_schema(:ok)
  65. assert response == [
  66. %{
  67. "build_url" => "http://gensokyo.2hu/builds/${ref}",
  68. "git" => nil,
  69. "installed" => true,
  70. "installed_refs" => ["fantasy"],
  71. "name" => "pleroma",
  72. "ref" => "fantasy"
  73. }
  74. ]
  75. end
  76. test "from a file", %{conn: conn} do
  77. clear_config([:frontends, :available], %{
  78. "pleroma" => %{
  79. "ref" => "fantasy",
  80. "name" => "pleroma",
  81. "build_dir" => ""
  82. }
  83. })
  84. conn
  85. |> put_req_header("content-type", "application/json")
  86. |> post("/api/pleroma/admin/frontends/install", %{
  87. name: "pleroma",
  88. file: "test/fixtures/tesla_mock/frontend.zip"
  89. })
  90. |> json_response_and_validate_schema(:ok)
  91. assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
  92. end
  93. test "from an URL", %{conn: conn} do
  94. Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
  95. %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")}
  96. end)
  97. conn
  98. |> put_req_header("content-type", "application/json")
  99. |> post("/api/pleroma/admin/frontends/install", %{
  100. name: "unknown",
  101. ref: "baka",
  102. build_url: "http://gensokyo.2hu/madeup.zip",
  103. build_dir: ""
  104. })
  105. |> json_response_and_validate_schema(:ok)
  106. assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
  107. end
  108. test "failing returns an error", %{conn: conn} do
  109. Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
  110. %Tesla.Env{status: 404, body: ""}
  111. end)
  112. result =
  113. conn
  114. |> put_req_header("content-type", "application/json")
  115. |> post("/api/pleroma/admin/frontends/install", %{
  116. name: "unknown",
  117. ref: "baka",
  118. build_url: "http://gensokyo.2hu/madeup.zip",
  119. build_dir: ""
  120. })
  121. |> json_response_and_validate_schema(400)
  122. assert result == %{"error" => "Could not download or unzip the frontend"}
  123. end
  124. end
  125. end