revert ae54c06bb4bd2389d6eb1502b842c6b632e12e40
[akkoma] / test / pleroma / instances_test.exs
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.InstancesTest do
6 alias Pleroma.Instances
7 alias Pleroma.Instances.Instance
8
9 use Pleroma.DataCase
10
11 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
12
13 describe "reachable?/1" do
14 test "returns `true` for host / url with unknown reachability status" do
15 assert Instances.reachable?("unknown.site")
16 assert Instances.reachable?("http://unknown.site")
17 end
18
19 test "returns `false` for host / url marked unreachable for at least `reachability_datetime_threshold()`" do
20 host = "consistently-unreachable.name"
21 Instances.set_consistently_unreachable(host)
22
23 refute Instances.reachable?(host)
24 refute Instances.reachable?("http://#{host}/path")
25 end
26
27 test "returns `true` for host / url marked unreachable for less than `reachability_datetime_threshold()`" do
28 url = "http://eventually-unreachable.name/path"
29
30 Instances.set_unreachable(url)
31
32 assert Instances.reachable?(url)
33 assert Instances.reachable?(URI.parse(url).host)
34 end
35
36 test "raises FunctionClauseError exception on non-binary input" do
37 assert_raise FunctionClauseError, fn -> Instances.reachable?(nil) end
38 assert_raise FunctionClauseError, fn -> Instances.reachable?(1) end
39 end
40 end
41
42 describe "filter_reachable/1" do
43 setup do
44 host = "consistently-unreachable.name"
45 url1 = "http://eventually-unreachable.com/path"
46 url2 = "http://domain.com/path"
47
48 Instances.set_consistently_unreachable(host)
49 Instances.set_unreachable(url1)
50
51 result = Instances.filter_reachable([host, url1, url2, nil])
52 %{result: result, url1: url1, url2: url2}
53 end
54
55 test "returns a map with keys containing 'not marked consistently unreachable' elements of supplied list",
56 %{result: result, url1: url1, url2: url2} do
57 assert is_map(result)
58 assert Enum.sort([url1, url2]) == result |> Map.keys() |> Enum.sort()
59 end
60
61 test "returns a map with `unreachable_since` values for keys",
62 %{result: result, url1: url1, url2: url2} do
63 assert is_map(result)
64 assert %NaiveDateTime{} = result[url1]
65 assert is_nil(result[url2])
66 end
67
68 test "returns an empty map for empty list or list containing no hosts / url" do
69 assert %{} == Instances.filter_reachable([])
70 assert %{} == Instances.filter_reachable([nil])
71 end
72 end
73
74 describe "set_reachable/1" do
75 test "sets unreachable url or host reachable" do
76 host = "domain.com"
77 Instances.set_consistently_unreachable(host)
78 refute Instances.reachable?(host)
79
80 Instances.set_reachable(host)
81 assert Instances.reachable?(host)
82 end
83
84 test "keeps reachable url or host reachable" do
85 url = "https://site.name?q="
86 assert Instances.reachable?(url)
87
88 Instances.set_reachable(url)
89 assert Instances.reachable?(url)
90 end
91
92 test "returns error status on non-binary input" do
93 assert {:error, _} = Instances.set_reachable(nil)
94 assert {:error, _} = Instances.set_reachable(1)
95 end
96 end
97
98 # Note: implementation-specific (e.g. Instance) details of set_unreachable/1
99 # should be tested in implementation-specific tests
100 describe "set_unreachable/1" do
101 test "returns error status on non-binary input" do
102 assert {:error, _} = Instances.set_unreachable(nil)
103 assert {:error, _} = Instances.set_unreachable(1)
104 end
105 end
106
107 describe "set_consistently_unreachable/1" do
108 test "sets reachable url or host unreachable" do
109 url = "http://domain.com?q="
110 assert Instances.reachable?(url)
111
112 Instances.set_consistently_unreachable(url)
113 refute Instances.reachable?(url)
114 end
115
116 test "keeps unreachable url or host unreachable" do
117 host = "site.name"
118 Instances.set_consistently_unreachable(host)
119 refute Instances.reachable?(host)
120
121 Instances.set_consistently_unreachable(host)
122 refute Instances.reachable?(host)
123 end
124 end
125
126 describe "set_request_signatures/1" do
127 test "sets instance has request signatures" do
128 host = "domain.com"
129
130 {:ok, instance} = Instances.set_request_signatures(host)
131 assert instance.has_request_signatures
132
133 {:ok, cached_instance} = Instance.get_cached_by_url(host)
134 assert cached_instance.has_request_signatures
135 end
136
137 test "returns error status on non-binary input" do
138 assert {:error, _} = Instances.set_request_signatures(nil)
139 assert {:error, _} = Instances.set_request_signatures(1)
140 end
141 end
142 end