Merge branch 'chores/bump-copyright' into 'develop'
[akkoma] / test / support / helpers.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Tests.Helpers do
6 @moduledoc """
7 Helpers for use in tests.
8 """
9 alias Pleroma.Config
10
11 defmacro clear_config(config_path) do
12 quote do
13 clear_config(unquote(config_path)) do
14 end
15 end
16 end
17
18 defmacro clear_config(config_path, do: yield) do
19 quote do
20 initial_setting = Config.fetch(unquote(config_path))
21 unquote(yield)
22
23 on_exit(fn ->
24 case initial_setting do
25 :error ->
26 Config.delete(unquote(config_path))
27
28 {:ok, value} ->
29 Config.put(unquote(config_path), value)
30 end
31 end)
32
33 :ok
34 end
35 end
36
37 defmacro clear_config(config_path, temp_setting) do
38 quote do
39 clear_config(unquote(config_path)) do
40 Config.put(unquote(config_path), unquote(temp_setting))
41 end
42 end
43 end
44
45 def require_migration(migration_name) do
46 [{module, _}] = Code.require_file("#{migration_name}.exs", "priv/repo/migrations")
47 {:ok, %{migration: module}}
48 end
49
50 defmacro __using__(_opts) do
51 quote do
52 import Pleroma.Tests.Helpers,
53 only: [
54 clear_config: 1,
55 clear_config: 2
56 ]
57
58 def time_travel(entity, seconds) do
59 new_time = NaiveDateTime.add(entity.inserted_at, seconds)
60
61 entity
62 |> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time})
63 |> Pleroma.Repo.update()
64 end
65
66 def to_datetime(%NaiveDateTime{} = naive_datetime) do
67 naive_datetime
68 |> DateTime.from_naive!("Etc/UTC")
69 |> DateTime.truncate(:second)
70 end
71
72 def to_datetime(datetime) when is_binary(datetime) do
73 datetime
74 |> NaiveDateTime.from_iso8601!()
75 |> to_datetime()
76 end
77
78 def collect_ids(collection) do
79 collection
80 |> Enum.map(& &1.id)
81 |> Enum.sort()
82 end
83
84 def refresh_record(%{id: id, __struct__: model} = _),
85 do: refresh_record(model, %{id: id})
86
87 def refresh_record(model, %{id: id} = _) do
88 Pleroma.Repo.get_by(model, id: id)
89 end
90
91 # Used for comparing json rendering during tests.
92 def render_json(view, template, assigns) do
93 assigns = Map.new(assigns)
94
95 view.render(template, assigns)
96 |> Jason.encode!()
97 |> Jason.decode!()
98 end
99
100 def stringify_keys(nil), do: nil
101
102 def stringify_keys(key) when key in [true, false], do: key
103 def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
104
105 def stringify_keys(map) when is_map(map) do
106 map
107 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
108 |> Enum.into(%{})
109 end
110
111 def stringify_keys([head | rest] = list) when is_list(list) do
112 [stringify_keys(head) | stringify_keys(rest)]
113 end
114
115 def stringify_keys(key), do: key
116
117 defmacro guards_config(config_path) do
118 quote do
119 initial_setting = Config.get(config_path)
120
121 Config.put(config_path, true)
122 on_exit(fn -> Config.put(config_path, initial_setting) end)
123 end
124 end
125 end
126 end
127 end