logo

pleroma

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

relay_test.exs (3383B)


  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 Mix.Tasks.Pleroma.RelayTest do
  5. alias Pleroma.Activity
  6. alias Pleroma.User
  7. alias Pleroma.Web.ActivityPub.ActivityPub
  8. alias Pleroma.Web.ActivityPub.Relay
  9. alias Pleroma.Web.ActivityPub.Utils
  10. use Pleroma.DataCase
  11. import Pleroma.Factory
  12. setup_all do
  13. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  14. Mix.shell(Mix.Shell.Process)
  15. on_exit(fn ->
  16. Mix.shell(Mix.Shell.IO)
  17. end)
  18. :ok
  19. end
  20. describe "running follow" do
  21. test "relay is followed" do
  22. target_instance = "http://mastodon.example.org/users/admin"
  23. Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
  24. local_user = Relay.get_actor()
  25. assert local_user.ap_id =~ "/relay"
  26. target_user = User.get_cached_by_ap_id(target_instance)
  27. refute target_user.local
  28. activity = Utils.fetch_latest_follow(local_user, target_user)
  29. assert activity.data["type"] == "Follow"
  30. assert activity.data["actor"] == local_user.ap_id
  31. assert activity.data["object"] == target_user.ap_id
  32. :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
  33. assert_receive {:mix_shell, :info,
  34. [
  35. "http://mastodon.example.org/users/admin - no Accept received (relay didn't follow back)"
  36. ]}
  37. end
  38. end
  39. describe "running unfollow" do
  40. test "relay is unfollowed" do
  41. user = insert(:user)
  42. target_instance = user.ap_id
  43. Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
  44. %User{ap_id: follower_id} = local_user = Relay.get_actor()
  45. target_user = User.get_cached_by_ap_id(target_instance)
  46. follow_activity = Utils.fetch_latest_follow(local_user, target_user)
  47. User.follow(local_user, target_user)
  48. assert "#{target_instance}/followers" in User.following(local_user)
  49. Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance])
  50. cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
  51. assert cancelled_activity.data["state"] == "cancelled"
  52. [undo_activity] =
  53. ActivityPub.fetch_activities([], %{
  54. type: "Undo",
  55. actor_id: follower_id,
  56. limit: 1,
  57. skip_preload: true,
  58. invisible_actors: true
  59. })
  60. assert undo_activity.data["type"] == "Undo"
  61. assert undo_activity.data["actor"] == local_user.ap_id
  62. assert undo_activity.data["object"]["id"] == cancelled_activity.data["id"]
  63. refute "#{target_instance}/followers" in User.following(local_user)
  64. end
  65. end
  66. describe "mix pleroma.relay list" do
  67. test "Prints relay subscription list" do
  68. :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
  69. refute_receive {:mix_shell, :info, _}
  70. relay_user = Relay.get_actor()
  71. ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
  72. |> Enum.each(fn ap_id ->
  73. {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
  74. User.follow(relay_user, user)
  75. end)
  76. :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
  77. assert_receive {:mix_shell, :info, ["https://mstdn.io/users/mayuutann"]}
  78. assert_receive {:mix_shell, :info, ["http://mastodon.example.org/users/admin"]}
  79. end
  80. end
  81. end