diff --git a/config/test.exs b/config/test.exs index fefdc2bbc..6446ad49f 100644 --- a/config/test.exs +++ b/config/test.exs @@ -170,6 +170,7 @@ config :pleroma, Pleroma.Upload.Filter.Mogrify, config_impl: Pleroma.StaticStubb config :pleroma, Pleroma.Upload.Filter.Mogrify, mogrify_impl: Pleroma.MogrifyMock config :pleroma, Pleroma.Signature, http_signatures_impl: Pleroma.StubbedHTTPSignaturesMock +config :pleroma, Pleroma.Web.ActivityPub.Publisher, signature_impl: Pleroma.SignatureMock peer_module = if String.to_integer(System.otp_release()) >= 25 do diff --git a/lib/pleroma/signature.ex b/lib/pleroma/signature.ex index 195513478..47d9d46f6 100644 --- a/lib/pleroma/signature.ex +++ b/lib/pleroma/signature.ex @@ -3,6 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Signature do + @behaviour Pleroma.Signature.API @behaviour HTTPSignatures.Adapter alias Pleroma.EctoType.ActivityPub.ObjectValidators diff --git a/lib/pleroma/signature/api.ex b/lib/pleroma/signature/api.ex new file mode 100644 index 000000000..8b4f0c2e4 --- /dev/null +++ b/lib/pleroma/signature/api.ex @@ -0,0 +1,14 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Signature.API do + @moduledoc """ + Behaviour for signing requests and producing HTTP Date headers. + + This is used to allow tests to replace the signing implementation with Mox. + """ + + @callback sign(user :: Pleroma.User.t(), headers :: map()) :: String.t() + @callback signed_date() :: String.t() +end diff --git a/lib/pleroma/web/activity_pub/publisher.ex b/lib/pleroma/web/activity_pub/publisher.ex index 9b37712db..1d8896857 100644 --- a/lib/pleroma/web/activity_pub/publisher.ex +++ b/lib/pleroma/web/activity_pub/publisher.ex @@ -26,6 +26,12 @@ defmodule Pleroma.Web.ActivityPub.Publisher do ActivityPub outgoing federation module. """ + @signature_impl Application.compile_env( + :pleroma, + [__MODULE__, :signature_impl], + Pleroma.Signature + ) + @doc """ Enqueue publishing a single activity. """ @@ -125,10 +131,10 @@ defmodule Pleroma.Web.ActivityPub.Publisher do digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64()) - date = Pleroma.Signature.signed_date() + date = @signature_impl.signed_date() signature = - Pleroma.Signature.sign(actor, %{ + @signature_impl.sign(actor, %{ "(request-target)": "post #{path}", host: signature_host(uri), "content-length": byte_size(json), diff --git a/test/pleroma/web/activity_pub/publisher_test.exs b/test/pleroma/web/activity_pub/publisher_test.exs index 8da9df23c..4721b2432 100644 --- a/test/pleroma/web/activity_pub/publisher_test.exs +++ b/test/pleroma/web/activity_pub/publisher_test.exs @@ -556,13 +556,14 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do end describe "prepare_one/1 with reporter anonymization" do - test_with_mock "signs with the anonymized actor keys when Transmogrifier changes actor", - Pleroma.Signature, - [:passthrough], - sign: fn %Pleroma.User{} = user, headers -> - send(self(), {:signed_as, user.ap_id}) - "TESTSIG" - end do + test "signs with the anonymized actor keys when Transmogrifier changes actor" do + Pleroma.SignatureMock + |> Mox.stub(:signed_date, fn -> Pleroma.Signature.signed_date() end) + |> Mox.expect(:sign, fn %Pleroma.User{} = user, _headers -> + send(self(), {:signed_as, user.ap_id}) + "TESTSIG" + end) + placeholder = insert(:user) reporter = insert(:user) target_account = insert(:user) diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 304bee5da..e33ab4b34 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -119,6 +119,7 @@ defmodule Pleroma.DataCase do Mox.stub_with(Pleroma.StubbedHTTPSignaturesMock, Pleroma.Test.HTTPSignaturesProxy) Mox.stub_with(Pleroma.DateTimeMock, Pleroma.DateTime.Impl) + Mox.stub_with(Pleroma.SignatureMock, Pleroma.Signature) end def ensure_local_uploader(context) do diff --git a/test/support/mocks.ex b/test/support/mocks.ex index b26834871..25bcb6472 100644 --- a/test/support/mocks.ex +++ b/test/support/mocks.ex @@ -40,3 +40,5 @@ Mox.defmock(Pleroma.Language.LanguageDetectorMock, Mox.defmock(Pleroma.DateTimeMock, for: Pleroma.DateTime) Mox.defmock(Pleroma.MogrifyMock, for: Pleroma.MogrifyBehaviour) + +Mox.defmock(Pleroma.SignatureMock, for: Pleroma.Signature.API)