logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/
commit: b023e1591ce207d429d92b6fd1293bc49673c26e
parent 3de250da2361518b764beba26201c160c2e279dc
Author: Lain Soykaf <lain@lain.com>
Date:   Fri,  5 Sep 2025 15:04:33 +0400

PublisherTest: Mock -> Mox

Diffstat:

Mconfig/test.exs3+++
Mlib/pleroma/web/activity_pub/publisher.ex11++++++++---
Mlib/pleroma/web/activity_pub/transmogrifier.ex1+
Alib/pleroma/web/activity_pub/transmogrifier/api.ex11+++++++++++
Mtest/pleroma/web/activity_pub/publisher_test.exs78++++++++++++++++++++++++++++++------------------------------------------------
Mtest/support/data_case.ex5+++++
Mtest/support/mocks.ex4++++
7 files changed, 62 insertions(+), 51 deletions(-)

diff --git a/config/test.exs b/config/test.exs @@ -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.Web.ActivityPub.Publisher, signature_impl: Pleroma.SignatureMock +config :pleroma, Pleroma.Web.ActivityPub.Publisher, + transmogrifier_impl: Pleroma.Web.ActivityPub.TransmogrifierMock + peer_module = if String.to_integer(System.otp_release()) >= 25 do :peer diff --git a/lib/pleroma/web/activity_pub/publisher.ex b/lib/pleroma/web/activity_pub/publisher.ex @@ -13,7 +13,6 @@ defmodule Pleroma.Web.ActivityPub.Publisher do alias Pleroma.User alias Pleroma.Web.ActivityPub.Publisher.Prepared alias Pleroma.Web.ActivityPub.Relay - alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Workers.PublisherWorker require Pleroma.Constants @@ -32,6 +31,12 @@ defmodule Pleroma.Web.ActivityPub.Publisher do Pleroma.Signature ) + @transmogrifier_impl Application.compile_env( + :pleroma, + [__MODULE__, :transmogrifier_impl], + Pleroma.Web.ActivityPub.Transmogrifier + ) + @doc """ 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. """ 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 else _e -> @@ -97,7 +102,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do Logger.debug("Federating #{ap_id} to #{inbox}") uri = %{path: path} = URI.parse(inbox) - {:ok, data} = Transmogrifier.prepare_outgoing(activity.data) + {:ok, data} = @transmogrifier_impl.prepare_outgoing(activity.data) {actor, data} = with {_, false} <- {:actor_changed?, data["actor"] != activity.data["actor"]} do diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -6,6 +6,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do @moduledoc """ A module to handle coding from internal to wire ActivityPub and back. """ + @behaviour Pleroma.Web.ActivityPub.Transmogrifier.API alias Pleroma.Activity alias Pleroma.EctoType.ActivityPub.ObjectValidators alias Pleroma.Maps diff --git a/lib/pleroma/web/activity_pub/transmogrifier/api.ex b/lib/pleroma/web/activity_pub/transmogrifier/api.ex @@ -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 diff --git a/test/pleroma/web/activity_pub/publisher_test.exs b/test/pleroma/web/activity_pub/publisher_test.exs @@ -8,7 +8,6 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do import Pleroma.Factory import Tesla.Mock - import Mock alias Pleroma.Activity alias Pleroma.Object @@ -170,10 +169,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do end describe "publish/2" do - test_with_mock "doesn't publish a non-public activity to quarantined instances.", - Pleroma.Web.ActivityPub.Publisher, - [:passthrough], - [] do + test "doesn't publish a non-public activity to quarantined instances." do Config.put([:instance, :quarantined_instances], [{"domain.com", "some reason"}]) follower = @@ -208,10 +204,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do ) end - test_with_mock "Publishes a non-public activity to non-quarantined instances.", - Pleroma.Web.ActivityPub.Publisher, - [:passthrough], - [] do + test "Publishes a non-public activity to non-quarantined instances." do Config.put([:instance, :quarantined_instances], [{"somedomain.com", "some reason"}]) follower = @@ -247,10 +240,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do ) end - test_with_mock "Publishes to directly addressed actors with higher priority.", - Pleroma.Web.ActivityPub.Publisher, - [:passthrough], - [] do + test "Publishes to directly addressed actors with higher priority." do note_activity = insert(:direct_note_activity) 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 called( - Publisher.enqueue_one( - %{ - inbox: :_, - activity_id: note_activity.id - }, - priority: 0 - ) - ) + assert_enqueued( + worker: "Pleroma.Workers.PublisherWorker", + args: %{ + "op" => "publish_one", + "params" => %{"activity_id" => note_activity.id} + }, + priority: 0 + ) end - test_with_mock "Publishes with the new actor if prepare_outgoing changes the actor.", - Pleroma.Web.ActivityPub.Publisher, - [:passthrough], - [] do + test "Publishes with the new actor if prepare_outgoing changes the actor." do mock(fn %{method: :post, url: "https://domain.com/users/nick1/inbox", 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]} ) - with_mock Pleroma.Web.ActivityPub.Transmogrifier, - prepare_outgoing: fn data -> {:ok, Map.put(data, "actor", replaced_actor.ap_id)} end do - prepared = - Publisher.prepare_one(%{ - inbox: "https://domain.com/users/nick1/inbox", - activity_id: note_activity.id, - cc: ["https://domain.com/users/nick2/inbox"] - }) + Pleroma.Web.ActivityPub.TransmogrifierMock + |> Mox.expect(:prepare_outgoing, fn data -> + {:ok, Map.put(data, "actor", replaced_actor.ap_id)} + end) + + prepared = + Publisher.prepare_one(%{ + 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) - assert decoded["actor"] == replaced_actor.ap_id + {:ok, decoded} = Jason.decode(prepared.json) + assert decoded["actor"] == replaced_actor.ap_id - {:ok, published} = Publisher.publish_one(prepared) - sent_activity = Jason.decode!(published.body) - 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 - test_with_mock "publishes an activity with BCC to all relevant peers.", - Pleroma.Web.ActivityPub.Publisher, - [:passthrough], - [] do + test "publishes an activity with BCC to all relevant peers." do follower = insert(:user, %{ local: false, @@ -347,10 +332,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do ) end - test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.", - Pleroma.Web.ActivityPub.Publisher, - [:passthrough], - [] do + test "publishes a delete activity to peers who signed fetch requests to the create acitvity/object." do fetcher = insert(:user, local: false, diff --git a/test/support/data_case.ex b/test/support/data_case.ex @@ -120,6 +120,11 @@ defmodule Pleroma.DataCase do Mox.stub_with(Pleroma.DateTimeMock, Pleroma.DateTime.Impl) Mox.stub_with(Pleroma.SignatureMock, Pleroma.Signature) + + Mox.stub_with( + Pleroma.Web.ActivityPub.TransmogrifierMock, + Pleroma.Web.ActivityPub.Transmogrifier + ) end def ensure_local_uploader(context) do diff --git a/test/support/mocks.ex b/test/support/mocks.ex @@ -42,3 +42,7 @@ Mox.defmock(Pleroma.DateTimeMock, for: Pleroma.DateTime) Mox.defmock(Pleroma.MogrifyMock, for: Pleroma.MogrifyBehaviour) Mox.defmock(Pleroma.SignatureMock, for: Pleroma.Signature.API) + +Mox.defmock(Pleroma.Web.ActivityPub.TransmogrifierMock, + for: Pleroma.Web.ActivityPub.Transmogrifier.API +)