PublisherTest: Mock -> Mox

This commit is contained in:
Lain Soykaf 2025-09-05 15:04:33 +04:00
commit b023e1591c
7 changed files with 62 additions and 51 deletions

View file

@ -172,6 +172,9 @@ config :pleroma, Pleroma.Upload.Filter.Mogrify, mogrify_impl: Pleroma.MogrifyMoc
config :pleroma, Pleroma.Signature, http_signatures_impl: Pleroma.StubbedHTTPSignaturesMock config :pleroma, Pleroma.Signature, http_signatures_impl: Pleroma.StubbedHTTPSignaturesMock
config :pleroma, Pleroma.Web.ActivityPub.Publisher, signature_impl: Pleroma.SignatureMock config :pleroma, Pleroma.Web.ActivityPub.Publisher, signature_impl: Pleroma.SignatureMock
config :pleroma, Pleroma.Web.ActivityPub.Publisher,
transmogrifier_impl: Pleroma.Web.ActivityPub.TransmogrifierMock
peer_module = peer_module =
if String.to_integer(System.otp_release()) >= 25 do if String.to_integer(System.otp_release()) >= 25 do
:peer :peer

View file

@ -13,7 +13,6 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.Publisher.Prepared alias Pleroma.Web.ActivityPub.Publisher.Prepared
alias Pleroma.Web.ActivityPub.Relay alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Workers.PublisherWorker alias Pleroma.Workers.PublisherWorker
require Pleroma.Constants require Pleroma.Constants
@ -32,6 +31,12 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
Pleroma.Signature Pleroma.Signature
) )
@transmogrifier_impl Application.compile_env(
:pleroma,
[__MODULE__, :transmogrifier_impl],
Pleroma.Web.ActivityPub.Transmogrifier
)
@doc """ @doc """
Enqueue publishing a single activity. Enqueue publishing a single activity.
""" """
@ -74,7 +79,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
Determine if an activity can be represented by running it through Transmogrifier. Determine if an activity can be represented by running it through Transmogrifier.
""" """
def representable?(%Activity{} = activity) do def representable?(%Activity{} = activity) do
with {:ok, _data} <- Transmogrifier.prepare_outgoing(activity.data) do with {:ok, _data} <- @transmogrifier_impl.prepare_outgoing(activity.data) do
true true
else else
_e -> _e ->
@ -97,7 +102,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
Logger.debug("Federating #{ap_id} to #{inbox}") Logger.debug("Federating #{ap_id} to #{inbox}")
uri = %{path: path} = URI.parse(inbox) uri = %{path: path} = URI.parse(inbox)
{:ok, data} = Transmogrifier.prepare_outgoing(activity.data) {:ok, data} = @transmogrifier_impl.prepare_outgoing(activity.data)
{actor, data} = {actor, data} =
with {_, false} <- {:actor_changed?, data["actor"] != activity.data["actor"]} do with {_, false} <- {:actor_changed?, data["actor"] != activity.data["actor"]} do

View file

@ -6,6 +6,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
@moduledoc """ @moduledoc """
A module to handle coding from internal to wire ActivityPub and back. A module to handle coding from internal to wire ActivityPub and back.
""" """
@behaviour Pleroma.Web.ActivityPub.Transmogrifier.API
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.EctoType.ActivityPub.ObjectValidators alias Pleroma.EctoType.ActivityPub.ObjectValidators
alias Pleroma.Maps alias Pleroma.Maps

View file

@ -0,0 +1,11 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.Transmogrifier.API do
@moduledoc """
Behaviour for the subset of Transmogrifier used by Publisher.
"""
@callback prepare_outgoing(map()) :: {:ok, map()} | {:error, term()}
end

View file

@ -8,7 +8,6 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
import Pleroma.Factory import Pleroma.Factory
import Tesla.Mock import Tesla.Mock
import Mock
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Object alias Pleroma.Object
@ -170,10 +169,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
end end
describe "publish/2" do describe "publish/2" do
test_with_mock "doesn't publish a non-public activity to quarantined instances.", test "doesn't publish a non-public activity to quarantined instances." do
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
Config.put([:instance, :quarantined_instances], [{"domain.com", "some reason"}]) Config.put([:instance, :quarantined_instances], [{"domain.com", "some reason"}])
follower = follower =
@ -208,10 +204,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
) )
end end
test_with_mock "Publishes a non-public activity to non-quarantined instances.", test "Publishes a non-public activity to non-quarantined instances." do
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
Config.put([:instance, :quarantined_instances], [{"somedomain.com", "some reason"}]) Config.put([:instance, :quarantined_instances], [{"somedomain.com", "some reason"}])
follower = follower =
@ -247,10 +240,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
) )
end end
test_with_mock "Publishes to directly addressed actors with higher priority.", test "Publishes to directly addressed actors with higher priority." do
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
note_activity = insert(:direct_note_activity) note_activity = insert(:direct_note_activity)
actor = Pleroma.User.get_by_ap_id(note_activity.data["actor"]) actor = Pleroma.User.get_by_ap_id(note_activity.data["actor"])
@ -259,21 +249,17 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
assert res == :ok assert res == :ok
assert called( assert_enqueued(
Publisher.enqueue_one( worker: "Pleroma.Workers.PublisherWorker",
%{ args: %{
inbox: :_, "op" => "publish_one",
activity_id: note_activity.id "params" => %{"activity_id" => note_activity.id}
}, },
priority: 0 priority: 0
) )
)
end end
test_with_mock "Publishes with the new actor if prepare_outgoing changes the actor.", test "Publishes with the new actor if prepare_outgoing changes the actor." do
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
mock(fn mock(fn
%{method: :post, url: "https://domain.com/users/nick1/inbox", body: body} -> %{method: :post, url: "https://domain.com/users/nick1/inbox", body: body} ->
{:ok, %Tesla.Env{status: 200, body: body}} {:ok, %Tesla.Env{status: 200, body: body}}
@ -294,28 +280,27 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
data_attrs: %{"to" => [other_user.ap_id]} data_attrs: %{"to" => [other_user.ap_id]}
) )
with_mock Pleroma.Web.ActivityPub.Transmogrifier, Pleroma.Web.ActivityPub.TransmogrifierMock
prepare_outgoing: fn data -> {:ok, Map.put(data, "actor", replaced_actor.ap_id)} end do |> Mox.expect(:prepare_outgoing, fn data ->
prepared = {:ok, Map.put(data, "actor", replaced_actor.ap_id)}
Publisher.prepare_one(%{ end)
inbox: "https://domain.com/users/nick1/inbox",
activity_id: note_activity.id,
cc: ["https://domain.com/users/nick2/inbox"]
})
{:ok, decoded} = Jason.decode(prepared.json) prepared =
assert decoded["actor"] == replaced_actor.ap_id Publisher.prepare_one(%{
inbox: "https://domain.com/users/nick1/inbox",
activity_id: note_activity.id,
cc: ["https://domain.com/users/nick2/inbox"]
})
{:ok, published} = Publisher.publish_one(prepared) {:ok, decoded} = Jason.decode(prepared.json)
sent_activity = Jason.decode!(published.body) assert decoded["actor"] == replaced_actor.ap_id
assert sent_activity["actor"] == replaced_actor.ap_id
end {:ok, published} = Publisher.publish_one(prepared)
sent_activity = Jason.decode!(published.body)
assert sent_activity["actor"] == replaced_actor.ap_id
end end
test_with_mock "publishes an activity with BCC to all relevant peers.", test "publishes an activity with BCC to all relevant peers." do
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
follower = follower =
insert(:user, %{ insert(:user, %{
local: false, local: false,
@ -347,10 +332,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
) )
end end
test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.", test "publishes a delete activity to peers who signed fetch requests to the create acitvity/object." do
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
fetcher = fetcher =
insert(:user, insert(:user,
local: false, local: false,

View file

@ -120,6 +120,11 @@ defmodule Pleroma.DataCase do
Mox.stub_with(Pleroma.DateTimeMock, Pleroma.DateTime.Impl) Mox.stub_with(Pleroma.DateTimeMock, Pleroma.DateTime.Impl)
Mox.stub_with(Pleroma.SignatureMock, Pleroma.Signature) Mox.stub_with(Pleroma.SignatureMock, Pleroma.Signature)
Mox.stub_with(
Pleroma.Web.ActivityPub.TransmogrifierMock,
Pleroma.Web.ActivityPub.Transmogrifier
)
end end
def ensure_local_uploader(context) do def ensure_local_uploader(context) do

View file

@ -42,3 +42,7 @@ Mox.defmock(Pleroma.DateTimeMock, for: Pleroma.DateTime)
Mox.defmock(Pleroma.MogrifyMock, for: Pleroma.MogrifyBehaviour) Mox.defmock(Pleroma.MogrifyMock, for: Pleroma.MogrifyBehaviour)
Mox.defmock(Pleroma.SignatureMock, for: Pleroma.Signature.API) Mox.defmock(Pleroma.SignatureMock, for: Pleroma.Signature.API)
Mox.defmock(Pleroma.Web.ActivityPub.TransmogrifierMock,
for: Pleroma.Web.ActivityPub.Transmogrifier.API
)