1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.InstancesTest do
6 alias Pleroma.Instances
7 alias Pleroma.Instances.Instance
11 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
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")
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)
23 refute Instances.reachable?(host)
24 refute Instances.reachable?("http://#{host}/path")
27 test "returns `true` for host / url marked unreachable for less than `reachability_datetime_threshold()`" do
28 url = "http://eventually-unreachable.name/path"
30 Instances.set_unreachable(url)
32 assert Instances.reachable?(url)
33 assert Instances.reachable?(URI.parse(url).host)
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
42 describe "filter_reachable/1" do
44 host = "consistently-unreachable.name"
45 url1 = "http://eventually-unreachable.com/path"
46 url2 = "http://domain.com/path"
48 Instances.set_consistently_unreachable(host)
49 Instances.set_unreachable(url1)
51 result = Instances.filter_reachable([host, url1, url2, nil])
52 %{result: result, url1: url1, url2: url2}
55 test "returns a map with keys containing 'not marked consistently unreachable' elements of supplied list",
56 %{result: result, url1: url1, url2: url2} do
58 assert Enum.sort([url1, url2]) == result |> Map.keys() |> Enum.sort()
61 test "returns a map with `unreachable_since` values for keys",
62 %{result: result, url1: url1, url2: url2} do
64 assert %NaiveDateTime{} = result[url1]
65 assert is_nil(result[url2])
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])
74 describe "set_reachable/1" do
75 test "sets unreachable url or host reachable" do
77 Instances.set_consistently_unreachable(host)
78 refute Instances.reachable?(host)
80 Instances.set_reachable(host)
81 assert Instances.reachable?(host)
84 test "keeps reachable url or host reachable" do
85 url = "https://site.name?q="
86 assert Instances.reachable?(url)
88 Instances.set_reachable(url)
89 assert Instances.reachable?(url)
92 test "returns error status on non-binary input" do
93 assert {:error, _} = Instances.set_reachable(nil)
94 assert {:error, _} = Instances.set_reachable(1)
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)
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)
112 Instances.set_consistently_unreachable(url)
113 refute Instances.reachable?(url)
116 test "keeps unreachable url or host unreachable" do
118 Instances.set_consistently_unreachable(host)
119 refute Instances.reachable?(host)
121 Instances.set_consistently_unreachable(host)
122 refute Instances.reachable?(host)
126 describe "set_request_signatures/1" do
127 test "sets instance has request signatures" do
130 {:ok, instance} = Instances.set_request_signatures(host)
131 assert instance.has_request_signatures
133 {:ok, cached_instance} = Instance.get_cached_by_url(host)
134 assert cached_instance.has_request_signatures
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)