logo

pleroma

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

federator_test.exs (5415B)


  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.FederatorTest do
  5. alias Pleroma.Instances
  6. alias Pleroma.Tests.ObanHelpers
  7. alias Pleroma.Web.CommonAPI
  8. alias Pleroma.Web.Federator
  9. alias Pleroma.Workers.PublisherWorker
  10. use Pleroma.DataCase
  11. use Oban.Testing, repo: Pleroma.Repo
  12. import Pleroma.Factory
  13. import Mock
  14. setup_all do
  15. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  16. :ok
  17. end
  18. setup_all do: clear_config([:instance, :federating], true)
  19. setup do: clear_config([:instance, :allow_relay])
  20. setup do: clear_config([:mrf, :policies])
  21. setup do: clear_config([:mrf_keyword])
  22. describe "Publish an activity" do
  23. setup do
  24. user = insert(:user)
  25. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  26. relay_mock = {
  27. Pleroma.Web.ActivityPub.Relay,
  28. [],
  29. [publish: fn _activity -> send(self(), :relay_publish) end]
  30. }
  31. %{activity: activity, relay_mock: relay_mock}
  32. end
  33. test "with relays active, it publishes to the relay", %{
  34. activity: activity,
  35. relay_mock: relay_mock
  36. } do
  37. with_mocks([relay_mock]) do
  38. Federator.publish(activity)
  39. ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
  40. end
  41. assert_received :relay_publish
  42. end
  43. test "with relays deactivated, it does not publish to the relay", %{
  44. activity: activity,
  45. relay_mock: relay_mock
  46. } do
  47. Pleroma.Config.put([:instance, :allow_relay], false)
  48. with_mocks([relay_mock]) do
  49. Federator.publish(activity)
  50. ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
  51. end
  52. refute_received :relay_publish
  53. end
  54. end
  55. describe "Targets reachability filtering in `publish`" do
  56. test "it federates only to reachable instances via AP" do
  57. user = insert(:user)
  58. {inbox1, inbox2} =
  59. {"https://domain.com/users/nick1/inbox", "https://domain2.com/users/nick2/inbox"}
  60. insert(:user, %{
  61. local: false,
  62. nickname: "nick1@domain.com",
  63. ap_id: "https://domain.com/users/nick1",
  64. inbox: inbox1,
  65. ap_enabled: true
  66. })
  67. insert(:user, %{
  68. local: false,
  69. nickname: "nick2@domain2.com",
  70. ap_id: "https://domain2.com/users/nick2",
  71. inbox: inbox2,
  72. ap_enabled: true
  73. })
  74. dt = NaiveDateTime.utc_now()
  75. Instances.set_unreachable(inbox1, dt)
  76. Instances.set_consistently_unreachable(URI.parse(inbox2).host)
  77. {:ok, _activity} =
  78. CommonAPI.post(user, %{status: "HI @nick1@domain.com, @nick2@domain2.com!"})
  79. expected_dt = NaiveDateTime.to_iso8601(dt)
  80. ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
  81. assert ObanHelpers.member?(
  82. %{
  83. "op" => "publish_one",
  84. "params" => %{"inbox" => inbox1, "unreachable_since" => expected_dt}
  85. },
  86. all_enqueued(worker: PublisherWorker)
  87. )
  88. end
  89. end
  90. describe "Receive an activity" do
  91. test "successfully processes incoming AP docs with correct origin" do
  92. params = %{
  93. "@context" => "https://www.w3.org/ns/activitystreams",
  94. "actor" => "http://mastodon.example.org/users/admin",
  95. "type" => "Create",
  96. "id" => "http://mastodon.example.org/users/admin/activities/1",
  97. "object" => %{
  98. "type" => "Note",
  99. "content" => "hi world!",
  100. "id" => "http://mastodon.example.org/users/admin/objects/1",
  101. "attributedTo" => "http://mastodon.example.org/users/admin",
  102. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  103. },
  104. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  105. }
  106. assert {:ok, job} = Federator.incoming_ap_doc(params)
  107. assert {:ok, _activity} = ObanHelpers.perform(job)
  108. assert {:ok, job} = Federator.incoming_ap_doc(params)
  109. assert {:error, :already_present} = ObanHelpers.perform(job)
  110. end
  111. test "rejects incoming AP docs with incorrect origin" do
  112. params = %{
  113. "@context" => "https://www.w3.org/ns/activitystreams",
  114. "actor" => "https://niu.moe/users/rye",
  115. "type" => "Create",
  116. "id" => "http://mastodon.example.org/users/admin/activities/1",
  117. "object" => %{
  118. "type" => "Note",
  119. "content" => "hi world!",
  120. "id" => "http://mastodon.example.org/users/admin/objects/1",
  121. "attributedTo" => "http://mastodon.example.org/users/admin",
  122. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  123. },
  124. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  125. }
  126. assert {:ok, job} = Federator.incoming_ap_doc(params)
  127. assert {:error, :origin_containment_failed} = ObanHelpers.perform(job)
  128. end
  129. test "it does not crash if MRF rejects the post" do
  130. Pleroma.Config.put([:mrf_keyword, :reject], ["lain"])
  131. Pleroma.Config.put(
  132. [:mrf, :policies],
  133. Pleroma.Web.ActivityPub.MRF.KeywordPolicy
  134. )
  135. params =
  136. File.read!("test/fixtures/mastodon-post-activity.json")
  137. |> Poison.decode!()
  138. assert {:ok, job} = Federator.incoming_ap_doc(params)
  139. assert {:error, _} = ObanHelpers.perform(job)
  140. end
  141. end
  142. end