eva
[akkoma] / test / web / instances / instance_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Instances.InstanceTest do
6 alias Pleroma.Instances.Instance
7 alias Pleroma.Repo
8
9 use Pleroma.DataCase
10
11 import Pleroma.Factory
12
13 clear_config_all([:instance, :federation_reachability_timeout_days]) do
14 Pleroma.Config.put([:instance, :federation_reachability_timeout_days], 1)
15 end
16
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)
21
22 assert {:ok, instance} = Instance.set_reachable(instance.host)
23 refute instance.unreachable_since
24 end
25
26 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
27 instance = insert(:instance, unreachable_since: nil)
28
29 assert {:ok, instance} = Instance.set_reachable(instance.host)
30 refute instance.unreachable_since
31 end
32
33 test "does NOT create an Instance record in case of no existing matching record" do
34 host = "domain.org"
35 assert nil == Instance.set_reachable(host)
36
37 assert [] = Repo.all(Ecto.Query.from(i in Instance))
38 assert Instance.reachable?(host)
39 end
40 end
41
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")
45
46 instance = Repo.get(Instance, instance.id)
47 assert instance.unreachable_since
48 assert "domain.com" == instance.host
49 end
50
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
54
55 assert {:ok, _} = Instance.set_unreachable(instance.host)
56
57 instance = Repo.get(Instance, instance.id)
58 assert instance.unreachable_since
59 end
60
61 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
62 instance =
63 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
64
65 assert instance.unreachable_since
66 initial_value = instance.unreachable_since
67
68 assert {:ok, _} = Instance.set_unreachable(instance.host)
69
70 instance = Repo.get(Instance, instance.id)
71 assert initial_value == instance.unreachable_since
72 end
73 end
74
75 describe "set_unreachable/2" do
76 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
77 instance =
78 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
79
80 assert instance.unreachable_since
81
82 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
83 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
84
85 instance = Repo.get(Instance, instance.id)
86 assert past_value == instance.unreachable_since
87 end
88
89 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
90 instance =
91 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
92
93 assert instance.unreachable_since
94 initial_value = instance.unreachable_since
95
96 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
97
98 instance = Repo.get(Instance, instance.id)
99 assert initial_value == instance.unreachable_since
100 end
101 end
102 end