1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Instances.InstanceTest do
6 alias Pleroma.Instances.Instance
11 import Pleroma.Factory
13 clear_config_all([:instance, :federation_reachability_timeout_days]) do
14 Pleroma.Config.put([:instance, :federation_reachability_timeout_days], 1)
17 describe "set_reachable/1" do
18 test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
19 unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
20 instance = insert(:instance, unreachable_since: unreachable_since)
22 assert {:ok, instance} = Instance.set_reachable(instance.host)
23 refute instance.unreachable_since
26 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
27 instance = insert(:instance, unreachable_since: nil)
29 assert {:ok, instance} = Instance.set_reachable(instance.host)
30 refute instance.unreachable_since
33 test "does NOT create an Instance record in case of no existing matching record" do
35 assert nil == Instance.set_reachable(host)
37 assert [] = Repo.all(Ecto.Query.from(i in Instance))
38 assert Instance.reachable?(host)
42 describe "set_unreachable/1" do
43 test "creates new record having `unreachable_since` to current time if record does not exist" do
44 assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
46 instance = Repo.get(Instance, instance.id)
47 assert instance.unreachable_since
48 assert "domain.com" == instance.host
51 test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
52 instance = insert(:instance, unreachable_since: nil)
53 refute instance.unreachable_since
55 assert {:ok, _} = Instance.set_unreachable(instance.host)
57 instance = Repo.get(Instance, instance.id)
58 assert instance.unreachable_since
61 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
63 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
65 assert instance.unreachable_since
66 initial_value = instance.unreachable_since
68 assert {:ok, _} = Instance.set_unreachable(instance.host)
70 instance = Repo.get(Instance, instance.id)
71 assert initial_value == instance.unreachable_since
75 describe "set_unreachable/2" do
76 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
78 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
80 assert instance.unreachable_since
82 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
83 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
85 instance = Repo.get(Instance, instance.id)
86 assert past_value == instance.unreachable_since
89 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
91 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
93 assert instance.unreachable_since
94 initial_value = instance.unreachable_since
96 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
98 instance = Repo.get(Instance, instance.id)
99 assert initial_value == instance.unreachable_since