logo

pleroma

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

relay_controller_test.exs (3107B)


  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.RelayControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. alias Pleroma.Config
  8. alias Pleroma.ModerationLog
  9. alias Pleroma.Repo
  10. alias Pleroma.User
  11. setup_all do
  12. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  13. :ok
  14. end
  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 "relays" do
  25. test "POST /relay", %{conn: conn, admin: admin} do
  26. conn =
  27. conn
  28. |> put_req_header("content-type", "application/json")
  29. |> post("/api/pleroma/admin/relay", %{
  30. relay_url: "http://mastodon.example.org/users/admin"
  31. })
  32. assert json_response_and_validate_schema(conn, 200) == %{
  33. "actor" => "http://mastodon.example.org/users/admin",
  34. "followed_back" => false
  35. }
  36. log_entry = Repo.one(ModerationLog)
  37. assert ModerationLog.get_log_entry_message(log_entry) ==
  38. "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
  39. end
  40. test "GET /relay", %{conn: conn} do
  41. relay_user = Pleroma.Web.ActivityPub.Relay.get_actor()
  42. ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
  43. |> Enum.each(fn ap_id ->
  44. {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
  45. User.follow(relay_user, user)
  46. end)
  47. conn = get(conn, "/api/pleroma/admin/relay")
  48. assert json_response_and_validate_schema(conn, 200)["relays"] == [
  49. %{
  50. "actor" => "http://mastodon.example.org/users/admin",
  51. "followed_back" => true
  52. },
  53. %{"actor" => "https://mstdn.io/users/mayuutann", "followed_back" => true}
  54. ]
  55. end
  56. test "DELETE /relay", %{conn: conn, admin: admin} do
  57. conn
  58. |> put_req_header("content-type", "application/json")
  59. |> post("/api/pleroma/admin/relay", %{
  60. relay_url: "http://mastodon.example.org/users/admin"
  61. })
  62. conn =
  63. conn
  64. |> put_req_header("content-type", "application/json")
  65. |> delete("/api/pleroma/admin/relay", %{
  66. relay_url: "http://mastodon.example.org/users/admin"
  67. })
  68. assert json_response_and_validate_schema(conn, 200) ==
  69. "http://mastodon.example.org/users/admin"
  70. [log_entry_one, log_entry_two] = Repo.all(ModerationLog)
  71. assert ModerationLog.get_log_entry_message(log_entry_one) ==
  72. "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
  73. assert ModerationLog.get_log_entry_message(log_entry_two) ==
  74. "@#{admin.nickname} unfollowed relay: http://mastodon.example.org/users/admin"
  75. end
  76. end
  77. end