1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.DataCase do
7 This module defines the setup for tests requiring
8 access to the application's data layer.
10 You may define functions here to be used as helpers in
13 Finally, if the test case interacts with the database,
14 it cannot be async. For this reason, every test runs
15 inside a transaction which is reset at the beginning
16 of the test unless the test case is marked as async.
19 use ExUnit.CaseTemplate
28 import Pleroma.DataCase
29 use Pleroma.Tests.Helpers
31 # Sets up OAuth access with specified scopes
32 defp oauth_access(scopes, opts \\ []) do
34 Keyword.get_lazy(opts, :user, fn ->
35 Pleroma.Factory.insert(:user)
39 Keyword.get_lazy(opts, :oauth_token, fn ->
40 Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
43 %{user: user, token: token}
50 |> Supervisor.which_children()
52 {name, _, _, [Cachex]} ->
55 |> String.trim_leading("cachex_")
56 |> Kernel.<>("_cache")
57 |> String.to_existing_atom()
66 :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
69 Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache)
72 Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
73 Mox.stub_with(Pleroma.CachexMock, Pleroma.CachexProxy)
78 if tags[:needs_streamer] do
80 id: Pleroma.Web.Streamer.registry(),
82 {Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
94 Mox.stub_with(Pleroma.Web.ActivityPub.SideEffectsMock, Pleroma.Web.ActivityPub.SideEffects)
97 Pleroma.Web.ActivityPub.ObjectValidatorMock,
98 Pleroma.Web.ActivityPub.ObjectValidator
101 Mox.stub_with(Pleroma.Web.ActivityPub.MRFMock, Pleroma.Web.ActivityPub.MRF)
102 Mox.stub_with(Pleroma.Web.ActivityPub.ActivityPubMock, Pleroma.Web.ActivityPub.ActivityPub)
103 Mox.stub_with(Pleroma.Web.FederatorMock, Pleroma.Web.Federator)
104 Mox.stub_with(Pleroma.ConfigMock, Pleroma.Config)
107 def ensure_local_uploader(context) do
108 test_uploader = Map.get(context, :uploader, Pleroma.Uploaders.Local)
109 uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
110 filters = Pleroma.Config.get([Pleroma.Upload, :filters])
112 Pleroma.Config.put([Pleroma.Upload, :uploader], test_uploader)
113 Pleroma.Config.put([Pleroma.Upload, :filters], [])
116 Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
117 Pleroma.Config.put([Pleroma.Upload, :filters], filters)
124 A helper that transform changeset errors to a map of messages.
126 changeset = Accounts.create_user(%{password: "short"})
127 assert "password is too short" in errors_on(changeset).password
128 assert %{password: ["password is too short"]} = errors_on(changeset)
131 def errors_on(changeset) do
132 Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
133 Enum.reduce(opts, message, fn {key, value}, acc ->
134 String.replace(acc, "%{#{key}}", to_string(value))