instance: Do not fetch unreachable instances
[akkoma] / test / pleroma / instances / instance_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Instances.InstanceTest do
6 alias Pleroma.Instances
7 alias Pleroma.Instances.Instance
8 alias Pleroma.Repo
9
10 use Pleroma.DataCase
11
12 import ExUnit.CaptureLog
13 import Pleroma.Factory
14
15 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
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
103 describe "get_or_update_favicon/1" do
104 test "Scrapes favicon URLs" do
105 Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
106 %Tesla.Env{
107 status: 200,
108 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
109 }
110 end)
111
112 assert "https://favicon.example.org/favicon.png" ==
113 Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
114 end
115
116 test "Returns nil on too long favicon URLs" do
117 long_favicon_url =
118 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
119
120 Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
121 %Tesla.Env{
122 status: 200,
123 body:
124 ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
125 }
126 end)
127
128 assert capture_log(fn ->
129 assert nil ==
130 Instance.get_or_update_favicon(
131 URI.parse("https://long-favicon.example.org/")
132 )
133 end) =~
134 "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
135 end
136
137 test "Handles not getting a favicon URL properly" do
138 Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
139 %Tesla.Env{
140 status: 200,
141 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
142 }
143 end)
144
145 refute capture_log(fn ->
146 assert nil ==
147 Instance.get_or_update_favicon(
148 URI.parse("https://no-favicon.example.org/")
149 )
150 end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
151 end
152
153 test "Doesn't scrapes unreachable instances" do
154 instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
155 url = "https://" <> instance.host
156
157 assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
158 "Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
159 end
160 end
161 end