Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.2KB

  1. defmodule Pleroma.Web.FederatorTest do
  2. alias Pleroma.Web.Federator
  3. alias Pleroma.Web.CommonAPI
  4. use Pleroma.DataCase
  5. import Pleroma.Factory
  6. import Mock
  7. setup_all do
  8. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  9. :ok
  10. end
  11. test "enqueues an element according to priority" do
  12. queue = [%{item: 1, priority: 2}]
  13. new_queue = Federator.enqueue_sorted(queue, 2, 1)
  14. assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
  15. new_queue = Federator.enqueue_sorted(queue, 2, 3)
  16. assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}]
  17. end
  18. test "pop first item" do
  19. queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
  20. assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
  21. end
  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.handle(:publish, activity)
  39. end
  40. assert_received :relay_publish
  41. end
  42. test "with relays deactivated, it does not publish to the relay", %{
  43. activity: activity,
  44. relay_mock: relay_mock
  45. } do
  46. Pleroma.Config.put([:instance, :allow_relay], false)
  47. with_mocks([relay_mock]) do
  48. Federator.handle(:publish, activity)
  49. end
  50. refute_received :relay_publish
  51. Pleroma.Config.put([:instance, :allow_relay], true)
  52. end
  53. end
  54. describe "Receive an activity" do
  55. test "successfully processes incoming AP docs with correct origin" do
  56. params = %{
  57. "@context" => "https://www.w3.org/ns/activitystreams",
  58. "actor" => "http://mastodon.example.org/users/admin",
  59. "type" => "Create",
  60. "id" => "http://mastodon.example.org/users/admin/activities/1",
  61. "object" => %{
  62. "type" => "Note",
  63. "content" => "hi world!",
  64. "id" => "http://mastodon.example.org/users/admin/objects/1",
  65. "attributedTo" => "http://mastodon.example.org/users/admin"
  66. },
  67. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  68. }
  69. {:ok, _activity} = Federator.handle(:incoming_ap_doc, params)
  70. end
  71. test "rejects incoming AP docs with incorrect origin" do
  72. params = %{
  73. "@context" => "https://www.w3.org/ns/activitystreams",
  74. "actor" => "https://niu.moe/users/rye",
  75. "type" => "Create",
  76. "id" => "http://mastodon.example.org/users/admin/activities/1",
  77. "object" => %{
  78. "type" => "Note",
  79. "content" => "hi world!",
  80. "id" => "http://mastodon.example.org/users/admin/objects/1",
  81. "attributedTo" => "http://mastodon.example.org/users/admin"
  82. },
  83. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  84. }
  85. :error = Federator.handle(:incoming_ap_doc, params)
  86. end
  87. end
  88. end