diff options
23 files changed, 67 insertions, 29 deletions
diff --git a/config/test.exs b/config/test.exs index 6fe84478a..6bba555b0 100644 --- a/config/test.exs +++ b/config/test.exs @@ -144,6 +144,7 @@ config :pleroma, Pleroma.Search.Meilisearch, url: "http://127.0.0.1:7700/", priv config :phoenix, :plug_init_mode, :runtime config :pleroma, :config_impl, Pleroma.UnstubbedConfigMock +config :pleroma, :datetime_impl, Pleroma.DateTimeMock config :pleroma, Pleroma.PromEx, disabled: true diff --git a/lib/pleroma/datetime.ex b/lib/pleroma/datetime.ex new file mode 100644 index 000000000..d79cb848b --- /dev/null +++ b/lib/pleroma/datetime.ex @@ -0,0 +1,3 @@ +defmodule Pleroma.DateTime do + @callback utc_now() :: NaiveDateTime.t() +end diff --git a/lib/pleroma/datetime/impl.ex b/lib/pleroma/datetime/impl.ex new file mode 100644 index 000000000..102be047b --- /dev/null +++ b/lib/pleroma/datetime/impl.ex @@ -0,0 +1,6 @@ +defmodule Pleroma.DateTime.Impl do + @behaviour Pleroma.DateTime + + @impl true + def utc_now, do: NaiveDateTime.utc_now() +end diff --git a/lib/pleroma/user_relationship.ex b/lib/pleroma/user_relationship.ex index 82fcc1cdd..5b48d321a 100644 --- a/lib/pleroma/user_relationship.ex +++ b/lib/pleroma/user_relationship.ex @@ -55,9 +55,13 @@ defmodule Pleroma.UserRelationship do def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__() + def datetime_impl do + Application.get_env(:pleroma, :datetime_impl, Pleroma.DateTime.Impl) + end + def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do user_relationship - |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at]) + |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at, :inserted_at]) |> validate_required([:relationship_type, :source_id, :target_id]) |> unique_constraint(:relationship_type, name: :user_relationships_source_id_relationship_type_target_id_index @@ -65,6 +69,7 @@ defmodule Pleroma.UserRelationship do |> validate_not_self_relationship() end + @spec exists?(any(), Pleroma.User.t(), Pleroma.User.t()) :: boolean() def exists?(relationship_type, %User{} = source, %User{} = target) do UserRelationship |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id) @@ -90,7 +95,8 @@ defmodule Pleroma.UserRelationship do relationship_type: relationship_type, source_id: source.id, target_id: target.id, - expires_at: expires_at + expires_at: expires_at, + inserted_at: datetime_impl().utc_now() }) |> Repo.insert( on_conflict: {:replace_all_except, [:id, :inserted_at]}, diff --git a/test/mix/tasks/pleroma/digest_test.exs b/test/mix/tasks/pleroma/digest_test.exs index 08482aadb..0d1804cdb 100644 --- a/test/mix/tasks/pleroma/digest_test.exs +++ b/test/mix/tasks/pleroma/digest_test.exs @@ -24,7 +24,7 @@ defmodule Mix.Tasks.Pleroma.DigestTest do setup do: clear_config([Pleroma.Emails.Mailer, :enabled], true) setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/mix/tasks/pleroma/user_test.exs b/test/mix/tasks/pleroma/user_test.exs index c9bcf2951..7ce5e92cb 100644 --- a/test/mix/tasks/pleroma/user_test.exs +++ b/test/mix/tasks/pleroma/user_test.exs @@ -21,7 +21,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do import Pleroma.Factory setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/conversation_test.exs b/test/pleroma/conversation_test.exs index 809c1951a..02b5de615 100644 --- a/test/pleroma/conversation_test.exs +++ b/test/pleroma/conversation_test.exs @@ -14,7 +14,7 @@ defmodule Pleroma.ConversationTest do setup_all do: clear_config([:instance, :federating], true) setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/emoji/pack_test.exs b/test/pleroma/emoji/pack_test.exs index 00001abfc..a05609e9a 100644 --- a/test/pleroma/emoji/pack_test.exs +++ b/test/pleroma/emoji/pack_test.exs @@ -62,7 +62,7 @@ defmodule Pleroma.Emoji.PackTest do path: Path.absname("test/instance_static/emoji/test_pack/blank.png") } - assert Pack.add_file(pack, nil, nil, file) == {:error, :einval} + assert {:error, _} = Pack.add_file(pack, nil, nil, file) end test "returns pack when zip file is empty", %{pack: pack} do diff --git a/test/pleroma/notification_test.exs b/test/pleroma/notification_test.exs index e595c5c53..4b20e07cf 100644 --- a/test/pleroma/notification_test.exs +++ b/test/pleroma/notification_test.exs @@ -19,7 +19,7 @@ defmodule Pleroma.NotificationTest do alias Pleroma.Web.MastodonAPI.NotificationView setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/user_relationship_test.exs b/test/pleroma/user_relationship_test.exs index 7d205a746..5b43cb2b6 100644 --- a/test/pleroma/user_relationship_test.exs +++ b/test/pleroma/user_relationship_test.exs @@ -3,11 +3,12 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.UserRelationshipTest do + alias Pleroma.DateTimeMock alias Pleroma.UserRelationship - use Pleroma.DataCase, async: false + use Pleroma.DataCase, async: true - import Mock + import Mox import Pleroma.Factory describe "*_exists?/2" do @@ -52,6 +53,9 @@ defmodule Pleroma.UserRelationshipTest do end test "creates user relationship record if it doesn't exist", %{users: [user1, user2]} do + DateTimeMock + |> stub_with(Pleroma.DateTime.Impl) + for relationship_type <- [ :block, :mute, @@ -80,13 +84,15 @@ defmodule Pleroma.UserRelationshipTest do end test "if record already exists, returns it", %{users: [user1, user2]} do - user_block = - with_mock NaiveDateTime, [:passthrough], utc_now: fn -> ~N[2017-03-17 17:09:58] end do - {:ok, %{inserted_at: ~N[2017-03-17 17:09:58]}} = - UserRelationship.create_block(user1, user2) - end + fixed_datetime = ~N[2017-03-17 17:09:58] + + Pleroma.DateTimeMock + |> expect(:utc_now, 2, fn -> fixed_datetime end) + + {:ok, %{inserted_at: ^fixed_datetime}} = UserRelationship.create_block(user1, user2) - assert user_block == UserRelationship.create_block(user1, user2) + # Test the idempotency without caring about the exact time + assert {:ok, _} = UserRelationship.create_block(user1, user2) end end diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 4a3d6bacc..c05241f50 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -20,7 +20,7 @@ defmodule Pleroma.UserTest do import Swoosh.TestAssertions setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs index b627478dc..b08690ed2 100644 --- a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs @@ -26,7 +26,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do require Pleroma.Constants setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/web/admin_api/controllers/user_controller_test.exs b/test/pleroma/web/admin_api/controllers/user_controller_test.exs index c8495c477..0e5650285 100644 --- a/test/pleroma/web/admin_api/controllers/user_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/user_controller_test.exs @@ -20,7 +20,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do alias Pleroma.Web.MediaProxy setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index 54f6818bd..cd3107f32 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -19,7 +19,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do import Pleroma.Factory setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/web/mastodon_api/controllers/notification_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/notification_controller_test.exs index 8fc22dde1..88f2fb7af 100644 --- a/test/pleroma/web/mastodon_api/controllers/notification_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/notification_controller_test.exs @@ -13,7 +13,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do import Pleroma.Factory setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/web/mastodon_api/controllers/search_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/search_controller_test.exs index d38767c96..d8263dfad 100644 --- a/test/pleroma/web/mastodon_api/controllers/search_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/search_controller_test.exs @@ -14,7 +14,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do import Mock setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/web/mastodon_api/views/notification_view_test.exs b/test/pleroma/web/mastodon_api/views/notification_view_test.exs index b1f3523ac..ce5ddd0fc 100644 --- a/test/pleroma/web/mastodon_api/views/notification_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/notification_view_test.exs @@ -23,7 +23,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do import Pleroma.Factory setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/web/o_auth/app_test.exs b/test/pleroma/web/o_auth/app_test.exs index 44219cf90..a69ba371e 100644 --- a/test/pleroma/web/o_auth/app_test.exs +++ b/test/pleroma/web/o_auth/app_test.exs @@ -58,16 +58,28 @@ defmodule Pleroma.Web.OAuth.AppTest do attrs = %{client_name: "Mastodon-Local", redirect_uris: "."} {:ok, %App{} = old_app} = App.get_or_make(attrs, ["write"]) + # backdate the old app so it's within the threshold for being cleaned up + one_hour_ago = DateTime.add(DateTime.utc_now(), -3600) + + {:ok, _} = + "UPDATE apps SET inserted_at = $1, updated_at = $1 WHERE id = $2" + |> Pleroma.Repo.query([one_hour_ago, old_app.id]) + + # Create the new app after backdating the old one attrs = %{client_name: "PleromaFE", redirect_uris: "."} {:ok, %App{} = app} = App.get_or_make(attrs, ["write"]) - # backdate the old app so it's within the threshold for being cleaned up + # Ensure the new app has a recent timestamp + now = DateTime.utc_now() + {:ok, _} = - "UPDATE apps SET inserted_at = now() - interval '1 hour' WHERE id = #{old_app.id}" - |> Pleroma.Repo.query() + "UPDATE apps SET inserted_at = $1, updated_at = $1 WHERE id = $2" + |> Pleroma.Repo.query([now, app.id]) App.remove_orphans() - assert [app] == Pleroma.Repo.all(App) + assert [returned_app] = Pleroma.Repo.all(App) + assert returned_app.client_name == "PleromaFE" + assert returned_app.id == app.id end end diff --git a/test/pleroma/web/pleroma_api/controllers/emoji_reaction_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/emoji_reaction_controller_test.exs index 8c2dcc1bb..c1e452a1e 100644 --- a/test/pleroma/web/pleroma_api/controllers/emoji_reaction_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/emoji_reaction_controller_test.exs @@ -14,7 +14,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do import Pleroma.Factory setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/workers/cron/digest_emails_worker_test.exs b/test/pleroma/workers/cron/digest_emails_worker_test.exs index e0bdf303e..46be82a4f 100644 --- a/test/pleroma/workers/cron/digest_emails_worker_test.exs +++ b/test/pleroma/workers/cron/digest_emails_worker_test.exs @@ -14,7 +14,7 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do setup do: clear_config([:email_notifications, :digest]) setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/pleroma/workers/cron/new_users_digest_worker_test.exs b/test/pleroma/workers/cron/new_users_digest_worker_test.exs index 0e4234cc8..ca4139eac 100644 --- a/test/pleroma/workers/cron/new_users_digest_worker_test.exs +++ b/test/pleroma/workers/cron/new_users_digest_worker_test.exs @@ -11,7 +11,7 @@ defmodule Pleroma.Workers.Cron.NewUsersDigestWorkerTest do alias Pleroma.Workers.Cron.NewUsersDigestWorker setup do - Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config) + Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig) :ok end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 52d4bef1a..304bee5da 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -117,6 +117,8 @@ defmodule Pleroma.DataCase do Mox.stub_with(Pleroma.ConfigMock, Pleroma.Config) Mox.stub_with(Pleroma.StaticStubbedConfigMock, Pleroma.Test.StaticConfig) Mox.stub_with(Pleroma.StubbedHTTPSignaturesMock, Pleroma.Test.HTTPSignaturesProxy) + + Mox.stub_with(Pleroma.DateTimeMock, Pleroma.DateTime.Impl) end def ensure_local_uploader(context) do diff --git a/test/support/mocks.ex b/test/support/mocks.ex index d84958e15..228b9882e 100644 --- a/test/support/mocks.ex +++ b/test/support/mocks.ex @@ -33,3 +33,5 @@ Mox.defmock(Pleroma.StubbedHTTPSignaturesMock, for: Pleroma.HTTPSignaturesAPI) Mox.defmock(Pleroma.LoggerMock, for: Pleroma.Logging) Mox.defmock(Pleroma.Uploaders.S3.ExAwsMock, for: Pleroma.Uploaders.S3.ExAwsAPI) + +Mox.defmock(Pleroma.DateTimeMock, for: Pleroma.DateTime) |