logo

pleroma

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

relay_test.exs (4391B)


  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.ActivityPub.RelayTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.User
  8. alias Pleroma.Web.ActivityPub.Relay
  9. alias Pleroma.Web.CommonAPI
  10. import ExUnit.CaptureLog
  11. import Pleroma.Factory
  12. import Mock
  13. test "gets an actor for the relay" do
  14. user = Relay.get_actor()
  15. assert user.ap_id == "#{Pleroma.Web.Endpoint.url()}/relay"
  16. end
  17. test "relay actor is invisible" do
  18. user = Relay.get_actor()
  19. assert User.invisible?(user)
  20. end
  21. describe "follow/1" do
  22. test "returns errors when user not found" do
  23. assert capture_log(fn ->
  24. {:error, _} = Relay.follow("test-ap-id")
  25. end) =~ "Could not decode user at fetch"
  26. end
  27. test "returns activity" do
  28. user = insert(:user)
  29. service_actor = Relay.get_actor()
  30. assert {:ok, %Activity{} = activity} = Relay.follow(user.ap_id)
  31. assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
  32. assert user.ap_id in activity.recipients
  33. assert activity.data["type"] == "Follow"
  34. assert activity.data["actor"] == service_actor.ap_id
  35. assert activity.data["object"] == user.ap_id
  36. end
  37. end
  38. describe "unfollow/1" do
  39. test "returns errors when user not found" do
  40. assert capture_log(fn ->
  41. {:error, _} = Relay.unfollow("test-ap-id")
  42. end) =~ "Could not decode user at fetch"
  43. end
  44. test "returns activity" do
  45. user = insert(:user)
  46. service_actor = Relay.get_actor()
  47. CommonAPI.follow(service_actor, user)
  48. assert "#{user.ap_id}/followers" in User.following(service_actor)
  49. assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id)
  50. assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
  51. assert user.ap_id in activity.recipients
  52. assert activity.data["type"] == "Undo"
  53. assert activity.data["actor"] == service_actor.ap_id
  54. assert activity.data["to"] == [user.ap_id]
  55. refute "#{user.ap_id}/followers" in User.following(service_actor)
  56. end
  57. end
  58. describe "publish/1" do
  59. setup do: clear_config([:instance, :federating])
  60. test "returns error when activity not `Create` type" do
  61. activity = insert(:like_activity)
  62. assert Relay.publish(activity) == {:error, "Not implemented"}
  63. end
  64. @tag capture_log: true
  65. test "returns error when activity not public" do
  66. activity = insert(:direct_note_activity)
  67. assert Relay.publish(activity) == {:error, false}
  68. end
  69. test "returns error when object is unknown" do
  70. activity =
  71. insert(:note_activity,
  72. data: %{
  73. "type" => "Create",
  74. "object" => "http://mastodon.example.org/eee/99541947525187367"
  75. }
  76. )
  77. Tesla.Mock.mock(fn
  78. %{method: :get, url: "http://mastodon.example.org/eee/99541947525187367"} ->
  79. %Tesla.Env{status: 500, body: ""}
  80. end)
  81. assert capture_log(fn ->
  82. assert Relay.publish(activity) == {:error, false}
  83. end) =~ "[error] error: false"
  84. end
  85. test_with_mock "returns announce activity and publish to federate",
  86. Pleroma.Web.Federator,
  87. [:passthrough],
  88. [] do
  89. clear_config([:instance, :federating], true)
  90. service_actor = Relay.get_actor()
  91. note = insert(:note_activity)
  92. assert {:ok, %Activity{} = activity} = Relay.publish(note)
  93. assert activity.data["type"] == "Announce"
  94. assert activity.data["actor"] == service_actor.ap_id
  95. assert activity.data["to"] == [service_actor.follower_address]
  96. assert called(Pleroma.Web.Federator.publish(activity))
  97. end
  98. test_with_mock "returns announce activity and not publish to federate",
  99. Pleroma.Web.Federator,
  100. [:passthrough],
  101. [] do
  102. clear_config([:instance, :federating], false)
  103. service_actor = Relay.get_actor()
  104. note = insert(:note_activity)
  105. assert {:ok, %Activity{} = activity} = Relay.publish(note)
  106. assert activity.data["type"] == "Announce"
  107. assert activity.data["actor"] == service_actor.ap_id
  108. refute called(Pleroma.Web.Federator.publish(activity))
  109. end
  110. end
  111. end