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.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 instance = insert(:instance, unreachable_since: NaiveDateTime.utc_now())
21 assert {:ok, instance} = Instance.set_reachable(instance.host)
22 refute instance.unreachable_since
25 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
26 instance = insert(:instance, unreachable_since: nil)
28 assert {:ok, instance} = Instance.set_reachable(instance.host)
29 refute instance.unreachable_since
32 test "does NOT create an Instance record in case of no existing matching record" do
34 assert nil == Instance.set_reachable(host)
36 assert [] = Repo.all(Ecto.Query.from(i in Instance))
37 assert Instance.reachable?(host)
41 describe "set_unreachable/1" do
42 test "creates new record having `unreachable_since` to current time if record does not exist" do
43 assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
45 instance = Repo.get(Instance, instance.id)
46 assert instance.unreachable_since
47 assert "domain.com" == instance.host
50 test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
51 instance = insert(:instance, unreachable_since: nil)
52 refute instance.unreachable_since
54 assert {:ok, _} = Instance.set_unreachable(instance.host)
56 instance = Repo.get(Instance, instance.id)
57 assert instance.unreachable_since
60 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
62 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
64 assert instance.unreachable_since
65 initial_value = instance.unreachable_since
67 assert {:ok, _} = Instance.set_unreachable(instance.host)
69 instance = Repo.get(Instance, instance.id)
70 assert initial_value == instance.unreachable_since
74 describe "set_unreachable/2" do
75 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
77 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
79 assert instance.unreachable_since
81 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
82 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
84 instance = Repo.get(Instance, instance.id)
85 assert past_value == instance.unreachable_since
88 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
90 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
92 assert instance.unreachable_since
93 initial_value = instance.unreachable_since
95 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
97 instance = Repo.get(Instance, instance.id)
98 assert initial_value == instance.unreachable_since