logo

pleroma

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

pipeline_test.exs (6199B)


  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.PipelineTest do
  5. use Pleroma.DataCase
  6. import Mock
  7. import Pleroma.Factory
  8. describe "common_pipeline/2" do
  9. setup do
  10. clear_config([:instance, :federating], true)
  11. :ok
  12. end
  13. test "when given an `object_data` in meta, Federation will receive a the original activity with the `object` field set to this embedded object" do
  14. activity = insert(:note_activity)
  15. object = %{"id" => "1", "type" => "Love"}
  16. meta = [local: true, object_data: object]
  17. activity_with_object = %{activity | data: Map.put(activity.data, "object", object)}
  18. with_mocks([
  19. {Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]},
  20. {
  21. Pleroma.Web.ActivityPub.MRF,
  22. [],
  23. [pipeline_filter: fn o, m -> {:ok, o, m} end]
  24. },
  25. {
  26. Pleroma.Web.ActivityPub.ActivityPub,
  27. [],
  28. [persist: fn o, m -> {:ok, o, m} end]
  29. },
  30. {
  31. Pleroma.Web.ActivityPub.SideEffects,
  32. [],
  33. [
  34. handle: fn o, m -> {:ok, o, m} end,
  35. handle_after_transaction: fn m -> m end
  36. ]
  37. },
  38. {
  39. Pleroma.Web.Federator,
  40. [],
  41. [publish: fn _o -> :ok end]
  42. }
  43. ]) do
  44. assert {:ok, ^activity, ^meta} =
  45. Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta)
  46. assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta))
  47. assert_called(Pleroma.Web.ActivityPub.MRF.pipeline_filter(activity, meta))
  48. assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta))
  49. assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta))
  50. refute called(Pleroma.Web.Federator.publish(activity))
  51. assert_called(Pleroma.Web.Federator.publish(activity_with_object))
  52. end
  53. end
  54. test "it goes through validation, filtering, persisting, side effects and federation for local activities" do
  55. activity = insert(:note_activity)
  56. meta = [local: true]
  57. with_mocks([
  58. {Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]},
  59. {
  60. Pleroma.Web.ActivityPub.MRF,
  61. [],
  62. [pipeline_filter: fn o, m -> {:ok, o, m} end]
  63. },
  64. {
  65. Pleroma.Web.ActivityPub.ActivityPub,
  66. [],
  67. [persist: fn o, m -> {:ok, o, m} end]
  68. },
  69. {
  70. Pleroma.Web.ActivityPub.SideEffects,
  71. [],
  72. [
  73. handle: fn o, m -> {:ok, o, m} end,
  74. handle_after_transaction: fn m -> m end
  75. ]
  76. },
  77. {
  78. Pleroma.Web.Federator,
  79. [],
  80. [publish: fn _o -> :ok end]
  81. }
  82. ]) do
  83. assert {:ok, ^activity, ^meta} =
  84. Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta)
  85. assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta))
  86. assert_called(Pleroma.Web.ActivityPub.MRF.pipeline_filter(activity, meta))
  87. assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta))
  88. assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta))
  89. assert_called(Pleroma.Web.Federator.publish(activity))
  90. end
  91. end
  92. test "it goes through validation, filtering, persisting, side effects without federation for remote activities" do
  93. activity = insert(:note_activity)
  94. meta = [local: false]
  95. with_mocks([
  96. {Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]},
  97. {
  98. Pleroma.Web.ActivityPub.MRF,
  99. [],
  100. [pipeline_filter: fn o, m -> {:ok, o, m} end]
  101. },
  102. {
  103. Pleroma.Web.ActivityPub.ActivityPub,
  104. [],
  105. [persist: fn o, m -> {:ok, o, m} end]
  106. },
  107. {
  108. Pleroma.Web.ActivityPub.SideEffects,
  109. [],
  110. [handle: fn o, m -> {:ok, o, m} end, handle_after_transaction: fn m -> m end]
  111. },
  112. {
  113. Pleroma.Web.Federator,
  114. [],
  115. []
  116. }
  117. ]) do
  118. assert {:ok, ^activity, ^meta} =
  119. Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta)
  120. assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta))
  121. assert_called(Pleroma.Web.ActivityPub.MRF.pipeline_filter(activity, meta))
  122. assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta))
  123. assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta))
  124. end
  125. end
  126. test "it goes through validation, filtering, persisting, side effects without federation for local activities if federation is deactivated" do
  127. clear_config([:instance, :federating], false)
  128. activity = insert(:note_activity)
  129. meta = [local: true]
  130. with_mocks([
  131. {Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]},
  132. {
  133. Pleroma.Web.ActivityPub.MRF,
  134. [],
  135. [pipeline_filter: fn o, m -> {:ok, o, m} end]
  136. },
  137. {
  138. Pleroma.Web.ActivityPub.ActivityPub,
  139. [],
  140. [persist: fn o, m -> {:ok, o, m} end]
  141. },
  142. {
  143. Pleroma.Web.ActivityPub.SideEffects,
  144. [],
  145. [handle: fn o, m -> {:ok, o, m} end, handle_after_transaction: fn m -> m end]
  146. },
  147. {
  148. Pleroma.Web.Federator,
  149. [],
  150. []
  151. }
  152. ]) do
  153. assert {:ok, ^activity, ^meta} =
  154. Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta)
  155. assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta))
  156. assert_called(Pleroma.Web.ActivityPub.MRF.pipeline_filter(activity, meta))
  157. assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta))
  158. assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta))
  159. end
  160. end
  161. end
  162. end