Merge branch '534_federation_targets_reachability' into 'develop'
[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 test "keeps only reachable elements of supplied list" 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 assert [url1, url2] == Instances.filter_reachable([host, url1, url2])
59 end
60 end
61
62 describe "set_reachable/1" do
63 test "sets unreachable url or host reachable" do
64 host = "domain.com"
65 Instances.set_consistently_unreachable(host)
66 refute Instances.reachable?(host)
67
68 Instances.set_reachable(host)
69 assert Instances.reachable?(host)
70 end
71
72 test "keeps reachable url or host reachable" do
73 url = "https://site.name?q="
74 assert Instances.reachable?(url)
75
76 Instances.set_reachable(url)
77 assert Instances.reachable?(url)
78 end
79
80 test "returns error status on non-binary input" do
81 assert {:error, _} = Instances.set_reachable(nil)
82 assert {:error, _} = Instances.set_reachable(1)
83 end
84 end
85
86 # Note: implementation-specific (e.g. Instance) details of set_unreachable/1 should be tested in implementation-specific tests
87 describe "set_unreachable/1" do
88 test "returns error status on non-binary input" do
89 assert {:error, _} = Instances.set_unreachable(nil)
90 assert {:error, _} = Instances.set_unreachable(1)
91 end
92 end
93
94 describe "set_consistently_unreachable/1" do
95 test "sets reachable url or host unreachable" do
96 url = "http://domain.com?q="
97 assert Instances.reachable?(url)
98
99 Instances.set_consistently_unreachable(url)
100 refute Instances.reachable?(url)
101 end
102
103 test "keeps unreachable url or host unreachable" do
104 host = "site.name"
105 Instances.set_consistently_unreachable(host)
106 refute Instances.reachable?(host)
107
108 Instances.set_consistently_unreachable(host)
109 refute Instances.reachable?(host)
110 end
111 end
112 end