1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.InstancesTest do
6 alias Pleroma.Instances
11 config_path = [:instance, :federation_reachability_timeout_days]
12 initial_setting = Pleroma.Config.get(config_path)
14 Pleroma.Config.put(config_path, 1)
15 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
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")
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)
30 refute Instances.reachable?(host)
31 refute Instances.reachable?("http://#{host}/path")
34 test "returns `true` for host / url marked unreachable for less than `reachability_datetime_threshold()`" do
35 url = "http://eventually-unreachable.name/path"
37 Instances.set_unreachable(url)
39 assert Instances.reachable?(url)
40 assert Instances.reachable?(URI.parse(url).host)
43 test "returns true on non-binary input" do
44 assert Instances.reachable?(nil)
45 assert Instances.reachable?(1)
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"
55 Instances.set_consistently_unreachable(host)
56 Instances.set_unreachable(url1)
58 assert [url1, url2] == Instances.filter_reachable([host, url1, url2])
62 describe "set_reachable/1" do
63 test "sets unreachable url or host reachable" do
65 Instances.set_consistently_unreachable(host)
66 refute Instances.reachable?(host)
68 Instances.set_reachable(host)
69 assert Instances.reachable?(host)
72 test "keeps reachable url or host reachable" do
73 url = "https://site.name?q="
74 assert Instances.reachable?(url)
76 Instances.set_reachable(url)
77 assert Instances.reachable?(url)
80 test "returns error status on non-binary input" do
81 assert {:error, _} = Instances.set_reachable(nil)
82 assert {:error, _} = Instances.set_reachable(1)
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)
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)
99 Instances.set_consistently_unreachable(url)
100 refute Instances.reachable?(url)
103 test "keeps unreachable url or host unreachable" do
105 Instances.set_consistently_unreachable(host)
106 refute Instances.reachable?(host)
108 Instances.set_consistently_unreachable(host)
109 refute Instances.reachable?(host)