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