Tests: Reset all cachex caches between synchronous tests
[akkoma] / test / support / data_case.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.DataCase do
6 @moduledoc """
7 This module defines the setup for tests requiring
8 access to the application's data layer.
9
10 You may define functions here to be used as helpers in
11 your tests.
12
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.
17 """
18
19 use ExUnit.CaseTemplate
20
21 using do
22 quote do
23 alias Pleroma.Repo
24
25 import Ecto
26 import Ecto.Changeset
27 import Ecto.Query
28 import Pleroma.DataCase
29 use Pleroma.Tests.Helpers
30
31 # Sets up OAuth access with specified scopes
32 defp oauth_access(scopes, opts \\ []) do
33 user =
34 Keyword.get_lazy(opts, :user, fn ->
35 Pleroma.Factory.insert(:user)
36 end)
37
38 token =
39 Keyword.get_lazy(opts, :oauth_token, fn ->
40 Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
41 end)
42
43 %{user: user, token: token}
44 end
45 end
46 end
47
48 def clear_cachex do
49 Pleroma.Supervisor
50 |> Supervisor.which_children()
51 |> Enum.each(fn
52 {name, _, _, [Cachex]} ->
53 name
54 |> to_string
55 |> String.trim_leading("cachex_")
56 |> Kernel.<>("_cache")
57 |> String.to_existing_atom()
58 |> Cachex.clear()
59
60 _ ->
61 nil
62 end)
63 end
64
65 setup tags do
66 :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
67
68 unless tags[:async] do
69 Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
70 clear_cachex()
71 end
72
73 if tags[:needs_streamer] do
74 start_supervised(%{
75 id: Pleroma.Web.Streamer.registry(),
76 start:
77 {Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
78 })
79 end
80
81 :ok
82 end
83
84 def ensure_local_uploader(context) do
85 test_uploader = Map.get(context, :uploader, Pleroma.Uploaders.Local)
86 uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
87 filters = Pleroma.Config.get([Pleroma.Upload, :filters])
88
89 Pleroma.Config.put([Pleroma.Upload, :uploader], test_uploader)
90 Pleroma.Config.put([Pleroma.Upload, :filters], [])
91
92 on_exit(fn ->
93 Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
94 Pleroma.Config.put([Pleroma.Upload, :filters], filters)
95 end)
96
97 :ok
98 end
99
100 @doc """
101 A helper that transform changeset errors to a map of messages.
102
103 changeset = Accounts.create_user(%{password: "short"})
104 assert "password is too short" in errors_on(changeset).password
105 assert %{password: ["password is too short"]} = errors_on(changeset)
106
107 """
108 def errors_on(changeset) do
109 Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
110 Enum.reduce(opts, message, fn {key, value}, acc ->
111 String.replace(acc, "%{#{key}}", to_string(value))
112 end)
113 end)
114 end
115 end