Add test for friendica featured collection
[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 require Logger
12
13 defmacro clear_config(config_path) do
14 quote do
15 clear_config(unquote(config_path)) do
16 end
17 end
18 end
19
20 defmacro clear_config(config_path, do: yield) do
21 quote do
22 initial_setting = Config.fetch(unquote(config_path))
23
24 unquote(yield)
25
26 on_exit(fn ->
27 case initial_setting do
28 :error ->
29 Config.delete(unquote(config_path))
30
31 {:ok, value} ->
32 Config.put(unquote(config_path), value)
33 end
34 end)
35
36 :ok
37 end
38 end
39
40 defmacro clear_config(config_path, temp_setting) do
41 # NOTE: `clear_config([section, key], value)` != `clear_config([section], key: value)` (!)
42 # Displaying a warning to prevent unintentional clearing of all but one keys in section
43 if Keyword.keyword?(temp_setting) and length(temp_setting) == 1 do
44 Logger.warn(
45 "Please change `clear_config([section], key: value)` to `clear_config([section, key], value)`"
46 )
47 end
48
49 quote do
50 clear_config(unquote(config_path)) do
51 Config.put(unquote(config_path), unquote(temp_setting))
52 end
53 end
54 end
55
56 def require_migration(migration_name) do
57 [{module, _}] = Code.require_file("#{migration_name}.exs", "priv/repo/migrations")
58 {:ok, %{migration: module}}
59 end
60
61 defmacro __using__(_opts) do
62 quote do
63 import Pleroma.Tests.Helpers,
64 only: [
65 clear_config: 1,
66 clear_config: 2
67 ]
68
69 def time_travel(entity, seconds) do
70 new_time = NaiveDateTime.add(entity.inserted_at, seconds)
71
72 entity
73 |> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time})
74 |> Pleroma.Repo.update()
75 end
76
77 def to_datetime(%NaiveDateTime{} = naive_datetime) do
78 naive_datetime
79 |> DateTime.from_naive!("Etc/UTC")
80 |> DateTime.truncate(:second)
81 end
82
83 def to_datetime(datetime) when is_binary(datetime) do
84 datetime
85 |> NaiveDateTime.from_iso8601!()
86 |> to_datetime()
87 end
88
89 def collect_ids(collection) do
90 collection
91 |> Enum.map(& &1.id)
92 |> Enum.sort()
93 end
94
95 def refresh_record(%{id: id, __struct__: model} = _),
96 do: refresh_record(model, %{id: id})
97
98 def refresh_record(model, %{id: id} = _) do
99 Pleroma.Repo.get_by(model, id: id)
100 end
101
102 # Used for comparing json rendering during tests.
103 def render_json(view, template, assigns) do
104 assigns = Map.new(assigns)
105
106 view.render(template, assigns)
107 |> Jason.encode!()
108 |> Jason.decode!()
109 end
110
111 def stringify_keys(nil), do: nil
112
113 def stringify_keys(key) when key in [true, false], do: key
114 def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
115
116 def stringify_keys(map) when is_map(map) do
117 map
118 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
119 |> Enum.into(%{})
120 end
121
122 def stringify_keys([head | rest] = list) when is_list(list) do
123 [stringify_keys(head) | stringify_keys(rest)]
124 end
125
126 def stringify_keys(key), do: key
127
128 defmacro guards_config(config_path) do
129 quote do
130 initial_setting = Config.get(config_path)
131
132 Config.put(config_path, true)
133 on_exit(fn -> Config.put(config_path, initial_setting) end)
134 end
135 end
136 end
137 end
138 end