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