Merge branch 'optimisation/deactivated-subquery' into 'develop'
[akkoma] / test / pleroma / web / activity_pub / pipeline_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, async: true
7
8 import Mox
9 import Pleroma.Factory
10
11 alias Pleroma.ConfigMock
12 alias Pleroma.Web.ActivityPub.ActivityPubMock
13 alias Pleroma.Web.ActivityPub.MRFMock
14 alias Pleroma.Web.ActivityPub.ObjectValidatorMock
15 alias Pleroma.Web.ActivityPub.SideEffectsMock
16 alias Pleroma.Web.FederatorMock
17
18 setup :verify_on_exit!
19
20 describe "common_pipeline/2" do
21 setup do
22 ObjectValidatorMock
23 |> expect(:validate, fn o, m -> {:ok, o, m} end)
24
25 MRFMock
26 |> expect(:pipeline_filter, fn o, m -> {:ok, o, m} end)
27
28 SideEffectsMock
29 |> expect(:handle, fn o, m -> {:ok, o, m} end)
30 |> expect(:handle_after_transaction, fn m -> m end)
31
32 :ok
33 end
34
35 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
36 activity = insert(:note_activity)
37 object = %{"id" => "1", "type" => "Love"}
38 meta = [local: true, object_data: object]
39
40 activity_with_object = %{activity | data: Map.put(activity.data, "object", object)}
41
42 ActivityPubMock
43 |> expect(:persist, fn _, m -> {:ok, activity, m} end)
44
45 FederatorMock
46 |> expect(:publish, fn ^activity_with_object -> :ok end)
47
48 ConfigMock
49 |> expect(:get, fn [:instance, :federating] -> true end)
50
51 assert {:ok, ^activity, ^meta} =
52 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(
53 activity.data,
54 meta
55 )
56 end
57
58 test "it goes through validation, filtering, persisting, side effects and federation for local activities" do
59 activity = insert(:note_activity)
60 meta = [local: true]
61
62 ActivityPubMock
63 |> expect(:persist, fn _, m -> {:ok, activity, m} end)
64
65 FederatorMock
66 |> expect(:publish, fn ^activity -> :ok end)
67
68 ConfigMock
69 |> expect(:get, fn [:instance, :federating] -> true end)
70
71 assert {:ok, ^activity, ^meta} =
72 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity.data, meta)
73 end
74
75 test "it goes through validation, filtering, persisting, side effects without federation for remote activities" do
76 activity = insert(:note_activity)
77 meta = [local: false]
78
79 ActivityPubMock
80 |> expect(:persist, fn _, m -> {:ok, activity, m} end)
81
82 ConfigMock
83 |> expect(:get, fn [:instance, :federating] -> true end)
84
85 assert {:ok, ^activity, ^meta} =
86 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity.data, meta)
87 end
88
89 test "it goes through validation, filtering, persisting, side effects without federation for local activities if federation is deactivated" do
90 activity = insert(:note_activity)
91 meta = [local: true]
92
93 ActivityPubMock
94 |> expect(:persist, fn _, m -> {:ok, activity, m} end)
95
96 ConfigMock
97 |> expect(:get, fn [:instance, :federating] -> false end)
98
99 assert {:ok, ^activity, ^meta} =
100 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity.data, meta)
101 end
102 end
103 end