2018-12-23 15:11:29 -05:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 01:49:20 -05:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 15:11:29 -05:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-03-17 12:09:58 -04:00
|
|
|
defmodule Pleroma.DataCase do
|
|
|
|
@moduledoc """
|
|
|
|
This module defines the setup for tests requiring
|
|
|
|
access to the application's data layer.
|
|
|
|
|
|
|
|
You may define functions here to be used as helpers in
|
|
|
|
your tests.
|
|
|
|
|
|
|
|
Finally, if the test case interacts with the database,
|
|
|
|
it cannot be async. For this reason, every test runs
|
|
|
|
inside a transaction which is reset at the beginning
|
|
|
|
of the test unless the test case is marked as async.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
|
2021-01-16 15:05:31 -05:00
|
|
|
import Pleroma.Tests.Helpers, only: [clear_config: 2]
|
|
|
|
|
2017-03-17 12:09:58 -04:00
|
|
|
using do
|
|
|
|
quote do
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
import Ecto
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
import Pleroma.DataCase
|
2018-12-11 07:31:52 -05:00
|
|
|
use Pleroma.Tests.Helpers
|
2020-09-19 12:16:55 -04:00
|
|
|
|
|
|
|
# Sets up OAuth access with specified scopes
|
|
|
|
defp oauth_access(scopes, opts \\ []) do
|
|
|
|
user =
|
|
|
|
Keyword.get_lazy(opts, :user, fn ->
|
|
|
|
Pleroma.Factory.insert(:user)
|
|
|
|
end)
|
|
|
|
|
|
|
|
token =
|
|
|
|
Keyword.get_lazy(opts, :oauth_token, fn ->
|
|
|
|
Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
|
|
|
|
end)
|
|
|
|
|
|
|
|
%{user: user, token: token}
|
|
|
|
end
|
2017-03-17 12:09:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-18 05:53:43 -05:00
|
|
|
def clear_cachex do
|
2021-03-14 07:51:39 -04:00
|
|
|
Pleroma.Application.StartUpDependencies.cachex_deps()
|
|
|
|
|> Enum.each(fn {name, _} ->
|
|
|
|
"#{name}_cache"
|
|
|
|
|> String.to_existing_atom()
|
|
|
|
|> Cachex.clear()
|
2020-12-18 05:53:43 -05:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-01-18 16:23:39 -05:00
|
|
|
def setup_multi_process_mode(tags) do
|
2017-03-17 12:09:58 -04:00
|
|
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
|
|
|
|
|
2020-12-18 13:49:01 -05:00
|
|
|
if tags[:async] do
|
|
|
|
Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache)
|
|
|
|
Mox.set_mox_private()
|
|
|
|
else
|
2017-03-17 12:09:58 -04:00
|
|
|
Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
|
2021-01-18 16:23:39 -05:00
|
|
|
|
2020-12-18 13:49:01 -05:00
|
|
|
Mox.set_mox_global()
|
2021-01-18 16:23:39 -05:00
|
|
|
Mox.stub_with(Pleroma.CachexMock, Pleroma.CachexProxy)
|
2020-12-18 05:53:43 -05:00
|
|
|
clear_cachex()
|
2017-03-17 12:09:58 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 16:23:39 -05:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_streamer(tags) do
|
2019-09-16 06:03:37 -04:00
|
|
|
if tags[:needs_streamer] do
|
2020-05-07 05:13:32 -04:00
|
|
|
start_supervised(%{
|
|
|
|
id: Pleroma.Web.Streamer.registry(),
|
|
|
|
start:
|
|
|
|
{Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
|
|
|
|
})
|
2019-09-16 06:03:37 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 16:23:39 -05:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
setup tags do
|
|
|
|
setup_multi_process_mode(tags)
|
|
|
|
setup_streamer(tags)
|
2020-12-21 10:38:34 -05:00
|
|
|
stub_pipeline()
|
|
|
|
|
2021-01-04 12:40:59 -05:00
|
|
|
Mox.verify_on_exit!()
|
|
|
|
|
2017-03-17 12:09:58 -04:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2020-12-21 10:38:34 -05:00
|
|
|
def stub_pipeline do
|
|
|
|
Mox.stub_with(Pleroma.Web.ActivityPub.SideEffectsMock, Pleroma.Web.ActivityPub.SideEffects)
|
|
|
|
|
|
|
|
Mox.stub_with(
|
|
|
|
Pleroma.Web.ActivityPub.ObjectValidatorMock,
|
|
|
|
Pleroma.Web.ActivityPub.ObjectValidator
|
|
|
|
)
|
|
|
|
|
|
|
|
Mox.stub_with(Pleroma.Web.ActivityPub.MRFMock, Pleroma.Web.ActivityPub.MRF)
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
2019-07-18 08:30:18 -04:00
|
|
|
def ensure_local_uploader(context) do
|
2021-01-16 15:05:31 -05:00
|
|
|
test_uploader = Map.get(context, :uploader) || Pleroma.Uploaders.Local
|
2018-12-05 05:37:06 -05:00
|
|
|
|
2021-01-16 15:05:31 -05:00
|
|
|
clear_config([Pleroma.Upload, :uploader], test_uploader)
|
|
|
|
clear_config([Pleroma.Upload, :filters], [])
|
2018-12-05 05:37:06 -05:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2017-03-17 12:09:58 -04:00
|
|
|
@doc """
|
|
|
|
A helper that transform changeset errors to a map of messages.
|
|
|
|
|
|
|
|
changeset = Accounts.create_user(%{password: "short"})
|
|
|
|
assert "password is too short" in errors_on(changeset).password
|
|
|
|
assert %{password: ["password is too short"]} = errors_on(changeset)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def errors_on(changeset) do
|
|
|
|
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
|
|
|
|
Enum.reduce(opts, message, fn {key, value}, acc ->
|
|
|
|
String.replace(acc, "%{#{key}}", to_string(value))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|