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
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 result = Instances.filter_reachable([host, url1, url2, nil])
59 %{result: result, url1: url1, url2: url2}
62 test "returns a map with keys containing 'not marked consistently unreachable' elements of supplied list",
63 %{result: result, url1: url1, url2: url2} do
65 assert Enum.sort([url1, url2]) == result |> Map.keys() |> Enum.sort()
68 test "returns a map with `unreachable_since` values for keys",
69 %{result: result, url1: url1, url2: url2} do
71 assert %NaiveDateTime{} = result[url1]
72 assert is_nil(result[url2])
75 test "returns an empty map for empty list or list containing no hosts / url" do
76 assert %{} == Instances.filter_reachable([])
77 assert %{} == Instances.filter_reachable([nil])
81 describe "set_reachable/1" do
82 test "sets unreachable url or host reachable" do
84 Instances.set_consistently_unreachable(host)
85 refute Instances.reachable?(host)
87 Instances.set_reachable(host)
88 assert Instances.reachable?(host)
91 test "keeps reachable url or host reachable" do
92 url = "https://site.name?q="
93 assert Instances.reachable?(url)
95 Instances.set_reachable(url)
96 assert Instances.reachable?(url)
99 test "returns error status on non-binary input" do
100 assert {:error, _} = Instances.set_reachable(nil)
101 assert {:error, _} = Instances.set_reachable(1)
105 # Note: implementation-specific (e.g. Instance) details of set_unreachable/1 should be tested in implementation-specific tests
106 describe "set_unreachable/1" do
107 test "returns error status on non-binary input" do
108 assert {:error, _} = Instances.set_unreachable(nil)
109 assert {:error, _} = Instances.set_unreachable(1)
113 describe "set_consistently_unreachable/1" do
114 test "sets reachable url or host unreachable" do
115 url = "http://domain.com?q="
116 assert Instances.reachable?(url)
118 Instances.set_consistently_unreachable(url)
119 refute Instances.reachable?(url)
122 test "keeps unreachable url or host unreachable" do
124 Instances.set_consistently_unreachable(host)
125 refute Instances.reachable?(host)
127 Instances.set_consistently_unreachable(host)
128 refute Instances.reachable?(host)