[#582] Optimized federation retirement by reducing the number of SQL calls
[akkoma] / test / web / instances / instances_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.InstancesTest do
6 alias Pleroma.Instances
7
8 use Pleroma.DataCase
9
10 setup_all do
11 config_path = [:instance, :federation_reachability_timeout_days]
12 initial_setting = Pleroma.Config.get(config_path)
13
14 Pleroma.Config.put(config_path, 1)
15 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
16
17 :ok
18 end
19
20 describe "reachable?/1" do
21 test "returns `true` for host / url with unknown reachability status" do
22 assert Instances.reachable?("unknown.site")
23 assert Instances.reachable?("http://unknown.site")
24 end
25
26 test "returns `false` for host / url marked unreachable for at least `reachability_datetime_threshold()`" do
27 host = "consistently-unreachable.name"
28 Instances.set_consistently_unreachable(host)
29
30 refute Instances.reachable?(host)
31 refute Instances.reachable?("http://#{host}/path")
32 end
33
34 test "returns `true` for host / url marked unreachable for less than `reachability_datetime_threshold()`" do
35 url = "http://eventually-unreachable.name/path"
36
37 Instances.set_unreachable(url)
38
39 assert Instances.reachable?(url)
40 assert Instances.reachable?(URI.parse(url).host)
41 end
42
43 test "returns true on non-binary input" do
44 assert Instances.reachable?(nil)
45 assert Instances.reachable?(1)
46 end
47 end
48
49 describe "filter_reachable/1" do
50 setup do
51 host = "consistently-unreachable.name"
52 url1 = "http://eventually-unreachable.com/path"
53 url2 = "http://domain.com/path"
54
55 Instances.set_consistently_unreachable(host)
56 Instances.set_unreachable(url1)
57
58 result = Instances.filter_reachable([host, url1, url2, nil])
59 %{result: result, url1: url1, url2: url2}
60 end
61
62 test "returns a map with keys containing 'not marked consistently unreachable' elements of supplied list",
63 %{result: result, url1: url1, url2: url2} do
64 assert is_map(result)
65 assert Enum.sort([url1, url2]) == result |> Map.keys() |> Enum.sort()
66 end
67
68 test "returns a map with `unreachable_since` values for keys",
69 %{result: result, url1: url1, url2: url2} do
70 assert is_map(result)
71 assert %NaiveDateTime{} = result[url1]
72 assert is_nil(result[url2])
73 end
74
75 test "returns an empty map for empty list or list containing no hosts / url" do
76 assert %{} == Instances.filter_reachable([])
77 assert %{} == Instances.filter_reachable([nil])
78 end
79 end
80
81 describe "set_reachable/1" do
82 test "sets unreachable url or host reachable" do
83 host = "domain.com"
84 Instances.set_consistently_unreachable(host)
85 refute Instances.reachable?(host)
86
87 Instances.set_reachable(host)
88 assert Instances.reachable?(host)
89 end
90
91 test "keeps reachable url or host reachable" do
92 url = "https://site.name?q="
93 assert Instances.reachable?(url)
94
95 Instances.set_reachable(url)
96 assert Instances.reachable?(url)
97 end
98
99 test "returns error status on non-binary input" do
100 assert {:error, _} = Instances.set_reachable(nil)
101 assert {:error, _} = Instances.set_reachable(1)
102 end
103 end
104
105 # Note: implementation-specific (e.g. Instance) details of set_unreachable/1 should be tested in implementation-specific tests
106 describe "set_unreachable/1" do
107 test "returns error status on non-binary input" do
108 assert {:error, _} = Instances.set_unreachable(nil)
109 assert {:error, _} = Instances.set_unreachable(1)
110 end
111 end
112
113 describe "set_consistently_unreachable/1" do
114 test "sets reachable url or host unreachable" do
115 url = "http://domain.com?q="
116 assert Instances.reachable?(url)
117
118 Instances.set_consistently_unreachable(url)
119 refute Instances.reachable?(url)
120 end
121
122 test "keeps unreachable url or host unreachable" do
123 host = "site.name"
124 Instances.set_consistently_unreachable(host)
125 refute Instances.reachable?(host)
126
127 Instances.set_consistently_unreachable(host)
128 refute Instances.reachable?(host)
129 end
130 end
131 end