From 190120fd7948751fc74eea3a65de19d5b7ed2753 Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Tue, 12 Dec 2023 14:03:46 +0400 Subject: [PATCH] Tests: More test fixes --- config/test.exs | 2 +- .../controllers/status_controller_test.exs | 33 +++++++++++++++---- .../chat_message_reference_view_test.exs | 18 ++++++++-- test/pleroma/web/rich_media/helpers_test.exs | 4 +-- test/support/data_case.ex | 1 + test/support/mocks.ex | 1 + 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/config/test.exs b/config/test.exs index d6d8af581..cf407ebf2 100644 --- a/config/test.exs +++ b/config/test.exs @@ -151,7 +151,7 @@ config :pleroma, Pleroma.Uploaders.S3, ex_aws_impl: Pleroma.Uploaders.S3.ExAwsMo config :pleroma, Pleroma.Uploaders.S3, config_impl: Pleroma.UnstubbedConfigMock config :pleroma, Pleroma.Upload, config_impl: Pleroma.UnstubbedConfigMock config :pleroma, Pleroma.ScheduledActivity, config_impl: Pleroma.UnstubbedConfigMock -config :pleroma, Pleroma.Web.RichMedia.Helpers, config_impl: Pleroma.UnstubbedConfigMock +config :pleroma, Pleroma.Web.RichMedia.Helpers, config_impl: Pleroma.StaticStubbedConfigMock if File.exists?("./config/test.secret.exs") do import_config "test.secret.exs" diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs index 76c092529..4e3d34172 100644 --- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs @@ -19,16 +19,24 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do alias Pleroma.Web.CommonAPI alias Pleroma.Workers.ScheduledActivityWorker + import Mox import Pleroma.Factory setup do: clear_config([:instance, :federating]) setup do: clear_config([:instance, :allow_relay]) - setup do: clear_config([:rich_media, :enabled]) setup do: clear_config([:mrf, :policies]) setup do: clear_config([:mrf_keyword, :reject]) setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Pleroma.UnstubbedConfigMock + |> stub_with(Pleroma.Config) + + Pleroma.StaticStubbedConfigMock + |> stub(:get, fn + [:rich_media, :enabled] -> false + path -> Pleroma.Test.StaticConfig.get(path) + end) + :ok end @@ -37,7 +45,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do test "posting a status does not increment reblog_count when relaying", %{conn: conn} do clear_config([:instance, :federating], true) - Config.get([:instance, :allow_relay], true) + clear_config([:instance, :allow_relay], true) response = conn @@ -321,7 +329,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do end test "fake statuses' preview card is not cached", %{conn: conn} do - clear_config([:rich_media, :enabled], true) + Pleroma.StaticStubbedConfigMock + |> stub(:get, fn + [:rich_media, :enabled] -> true + path -> Pleroma.Test.StaticConfig.get(path) + end) Tesla.Mock.mock(fn %{ @@ -358,7 +370,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do test "posting a status with OGP link preview", %{conn: conn} do Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) - clear_config([:rich_media, :enabled], true) + + Pleroma.StaticStubbedConfigMock + |> stub(:get, fn + [:rich_media, :enabled] -> true + path -> Pleroma.Test.StaticConfig.get(path) + end) conn = conn @@ -1689,7 +1706,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do describe "cards" do setup do - clear_config([:rich_media, :enabled], true) + Pleroma.StaticStubbedConfigMock + |> stub(:get, fn + [:rich_media, :enabled] -> true + path -> Pleroma.Test.StaticConfig.get(path) + end) oauth_access(["read:statuses"]) end diff --git a/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs b/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs index 6a1f84962..44d40269c 100644 --- a/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs +++ b/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs @@ -3,11 +3,13 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceViewTest do - use Pleroma.DataCase + alias Pleroma.NullCache + use Pleroma.DataCase, async: true alias Pleroma.Chat alias Pleroma.Chat.MessageReference alias Pleroma.Object + alias Pleroma.StaticStubbedConfigMock alias Pleroma.UnstubbedConfigMock, as: ConfigMock alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.CommonAPI @@ -40,6 +42,14 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceViewTest do cm_ref = MessageReference.for_chat_and_object(chat, object) + id = cm_ref.id + + Pleroma.CachexMock + |> stub(:get, fn + :chat_message_id_idempotency_key_cache, ^id -> {:ok, "123"} + cache, key -> NullCache.get(cache, key) + end) + chat_message = MessageReferenceView.render("show.json", chat_message_reference: cm_ref) assert chat_message[:id] == cm_ref.id @@ -51,7 +61,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceViewTest do assert match?([%{shortcode: "firefox"}], chat_message[:emojis]) assert chat_message[:idempotency_key] == "123" - clear_config([:rich_media, :enabled], true) + StaticStubbedConfigMock + |> stub(:get, fn + [:rich_media, :enabled] -> true + path -> Pleroma.Test.StaticConfig.get(path) + end) Tesla.Mock.mock_global(fn %{url: "https://example.com/ogp"} -> diff --git a/test/pleroma/web/rich_media/helpers_test.exs b/test/pleroma/web/rich_media/helpers_test.exs index e57e3719c..3ef5705ce 100644 --- a/test/pleroma/web/rich_media/helpers_test.exs +++ b/test/pleroma/web/rich_media/helpers_test.exs @@ -3,9 +3,9 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.RichMedia.HelpersTest do - use Pleroma.DataCase + use Pleroma.DataCase, async: true - alias Pleroma.UnstubbedConfigMock, as: ConfigMock + alias Pleroma.StaticStubbedConfigMock, as: ConfigMock alias Pleroma.Web.CommonAPI alias Pleroma.Web.RichMedia.Helpers diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 3c9cab061..14403f0b8 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -115,6 +115,7 @@ defmodule Pleroma.DataCase do Mox.stub_with(Pleroma.Web.ActivityPub.ActivityPubMock, Pleroma.Web.ActivityPub.ActivityPub) Mox.stub_with(Pleroma.Web.FederatorMock, Pleroma.Web.Federator) Mox.stub_with(Pleroma.ConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.StaticStubbedConfigMock, Pleroma.Test.StaticConfig) end def ensure_local_uploader(context) do diff --git a/test/support/mocks.ex b/test/support/mocks.ex index 22388c0c8..d906f0e1d 100644 --- a/test/support/mocks.ex +++ b/test/support/mocks.ex @@ -27,6 +27,7 @@ Mox.defmock(Pleroma.Web.FederatorMock, for: Pleroma.Web.Federator.Publishing) Mox.defmock(Pleroma.ConfigMock, for: Pleroma.Config.Getting) Mox.defmock(Pleroma.UnstubbedConfigMock, for: Pleroma.Config.Getting) +Mox.defmock(Pleroma.StaticStubbedConfigMock, for: Pleroma.Config.Getting) Mox.defmock(Pleroma.LoggerMock, for: Pleroma.Logging)