Merge branch 'develop' into refactor/subscription
[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 instance = insert(:instance, unreachable_since: NaiveDateTime.utc_now())
20
21 assert {:ok, instance} = Instance.set_reachable(instance.host)
22 refute instance.unreachable_since
23 end
24
25 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
26 instance = insert(:instance, unreachable_since: nil)
27
28 assert {:ok, instance} = Instance.set_reachable(instance.host)
29 refute instance.unreachable_since
30 end
31
32 test "does NOT create an Instance record in case of no existing matching record" do
33 host = "domain.org"
34 assert nil == Instance.set_reachable(host)
35
36 assert [] = Repo.all(Ecto.Query.from(i in Instance))
37 assert Instance.reachable?(host)
38 end
39 end
40
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")
44
45 instance = Repo.get(Instance, instance.id)
46 assert instance.unreachable_since
47 assert "domain.com" == instance.host
48 end
49
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
53
54 assert {:ok, _} = Instance.set_unreachable(instance.host)
55
56 instance = Repo.get(Instance, instance.id)
57 assert instance.unreachable_since
58 end
59
60 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
61 instance =
62 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
63
64 assert instance.unreachable_since
65 initial_value = instance.unreachable_since
66
67 assert {:ok, _} = Instance.set_unreachable(instance.host)
68
69 instance = Repo.get(Instance, instance.id)
70 assert initial_value == instance.unreachable_since
71 end
72 end
73
74 describe "set_unreachable/2" do
75 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
76 instance =
77 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
78
79 assert instance.unreachable_since
80
81 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
82 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
83
84 instance = Repo.get(Instance, instance.id)
85 assert past_value == instance.unreachable_since
86 end
87
88 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
89 instance =
90 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
91
92 assert instance.unreachable_since
93 initial_value = instance.unreachable_since
94
95 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
96
97 instance = Repo.get(Instance, instance.id)
98 assert initial_value == instance.unreachable_since
99 end
100 end
101 end