1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.InstancesTest do
6 alias Pleroma.Instances
10 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
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")
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)
22 refute Instances.reachable?(host)
23 refute Instances.reachable?("http://#{host}/path")
26 test "returns `true` for host / url marked unreachable for less than `reachability_datetime_threshold()`" do
27 url = "http://eventually-unreachable.name/path"
29 Instances.set_unreachable(url)
31 assert Instances.reachable?(url)
32 assert Instances.reachable?(URI.parse(url).host)
35 test "returns true on non-binary input" do
36 assert Instances.reachable?(nil)
37 assert Instances.reachable?(1)
41 describe "filter_reachable/1" do
43 host = "consistently-unreachable.name"
44 url1 = "http://eventually-unreachable.com/path"
45 url2 = "http://domain.com/path"
47 Instances.set_consistently_unreachable(host)
48 Instances.set_unreachable(url1)
50 result = Instances.filter_reachable([host, url1, url2, nil])
51 %{result: result, url1: url1, url2: url2}
54 test "returns a map with keys containing 'not marked consistently unreachable' elements of supplied list",
55 %{result: result, url1: url1, url2: url2} do
57 assert Enum.sort([url1, url2]) == result |> Map.keys() |> Enum.sort()
60 test "returns a map with `unreachable_since` values for keys",
61 %{result: result, url1: url1, url2: url2} do
63 assert %NaiveDateTime{} = result[url1]
64 assert is_nil(result[url2])
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])
73 describe "set_reachable/1" do
74 test "sets unreachable url or host reachable" do
76 Instances.set_consistently_unreachable(host)
77 refute Instances.reachable?(host)
79 Instances.set_reachable(host)
80 assert Instances.reachable?(host)
83 test "keeps reachable url or host reachable" do
84 url = "https://site.name?q="
85 assert Instances.reachable?(url)
87 Instances.set_reachable(url)
88 assert Instances.reachable?(url)
91 test "returns error status on non-binary input" do
92 assert {:error, _} = Instances.set_reachable(nil)
93 assert {:error, _} = Instances.set_reachable(1)
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)
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)
111 Instances.set_consistently_unreachable(url)
112 refute Instances.reachable?(url)
115 test "keeps unreachable url or host unreachable" do
117 Instances.set_consistently_unreachable(host)
118 refute Instances.reachable?(host)
120 Instances.set_consistently_unreachable(host)
121 refute Instances.reachable?(host)