Remove vapidPublicKey from Nodeinfo
[akkoma] / test / support / helpers.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.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.get(unquote(config_path))
21 unquote(yield)
22 on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
23 :ok
24 end
25 end
26
27 defmacro clear_config(config_path, temp_setting) do
28 quote do
29 clear_config(unquote(config_path)) do
30 Config.put(unquote(config_path), unquote(temp_setting))
31 end
32 end
33 end
34
35 defmacro __using__(_opts) do
36 quote do
37 import Pleroma.Tests.Helpers,
38 only: [
39 clear_config: 1,
40 clear_config: 2
41 ]
42
43 def to_datetime(naive_datetime) do
44 naive_datetime
45 |> DateTime.from_naive!("Etc/UTC")
46 |> DateTime.truncate(:second)
47 end
48
49 def collect_ids(collection) do
50 collection
51 |> Enum.map(& &1.id)
52 |> Enum.sort()
53 end
54
55 def refresh_record(%{id: id, __struct__: model} = _),
56 do: refresh_record(model, %{id: id})
57
58 def refresh_record(model, %{id: id} = _) do
59 Pleroma.Repo.get_by(model, id: id)
60 end
61
62 # Used for comparing json rendering during tests.
63 def render_json(view, template, assigns) do
64 assigns = Map.new(assigns)
65
66 view.render(template, assigns)
67 |> Poison.encode!()
68 |> Poison.decode!()
69 end
70
71 def stringify_keys(nil), do: nil
72
73 def stringify_keys(key) when key in [true, false], do: key
74 def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
75
76 def stringify_keys(map) when is_map(map) do
77 map
78 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
79 |> Enum.into(%{})
80 end
81
82 def stringify_keys([head | rest] = list) when is_list(list) do
83 [stringify_keys(head) | stringify_keys(rest)]
84 end
85
86 def stringify_keys(key), do: key
87
88 defmacro guards_config(config_path) do
89 quote do
90 initial_setting = Config.get(config_path)
91
92 Config.put(config_path, true)
93 on_exit(fn -> Config.put(config_path, initial_setting) end)
94 end
95 end
96 end
97 end
98 end