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
14 config_path = [:instance, :federation_reachability_timeout_days]
15 initial_setting = Pleroma.Config.get(config_path)
17 Pleroma.Config.put(config_path, 1)
18 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
23 describe "set_reachable/1" do
24 test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
25 instance = insert(:instance, unreachable_since: NaiveDateTime.utc_now())
27 assert {:ok, instance} = Instance.set_reachable(instance.host)
28 refute instance.unreachable_since
31 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
32 instance = insert(:instance, unreachable_since: nil)
34 assert {:ok, instance} = Instance.set_reachable(instance.host)
35 refute instance.unreachable_since
38 test "does NOT create an Instance record in case of no existing matching record" do
40 assert nil == Instance.set_reachable(host)
42 assert [] = Repo.all(Ecto.Query.from(i in Instance))
43 assert Instance.reachable?(host)
47 describe "set_unreachable/1" do
48 test "creates new record having `unreachable_since` to current time if record does not exist" do
49 assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
51 instance = Repo.get(Instance, instance.id)
52 assert instance.unreachable_since
53 assert "domain.com" == instance.host
56 test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
57 instance = insert(:instance, unreachable_since: nil)
58 refute instance.unreachable_since
60 assert {:ok, _} = Instance.set_unreachable(instance.host)
62 instance = Repo.get(Instance, instance.id)
63 assert instance.unreachable_since
66 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
68 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
70 assert instance.unreachable_since
71 initial_value = instance.unreachable_since
73 assert {:ok, _} = Instance.set_unreachable(instance.host)
75 instance = Repo.get(Instance, instance.id)
76 assert initial_value == instance.unreachable_since
80 describe "set_unreachable/2" do
81 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
83 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
85 assert instance.unreachable_since
87 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
88 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
90 instance = Repo.get(Instance, instance.id)
91 assert past_value == instance.unreachable_since
94 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
96 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
98 assert instance.unreachable_since
99 initial_value = instance.unreachable_since
101 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
103 instance = Repo.get(Instance, instance.id)
104 assert initial_value == instance.unreachable_since