4f080510018d22ad827af469af29f791bcc51d23
[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.Instance
7 alias Pleroma.Repo
8
9 use Pleroma.DataCase
10
11 import ExUnit.CaptureLog
12 import Pleroma.Factory
13
14 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
15
16 describe "set_reachable/1" do
17 test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
18 unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
19 instance = insert(:instance, unreachable_since: unreachable_since)
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
102 describe "get_or_update_favicon/1" do
103 test "Scrapes favicon URLs" do
104 Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
105 %Tesla.Env{
106 status: 200,
107 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
108 }
109 end)
110
111 assert "https://favicon.example.org/favicon.png" ==
112 Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
113 end
114
115 test "Returns nil on too long favicon URLs" do
116 long_favicon_url =
117 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
118
119 Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
120 %Tesla.Env{
121 status: 200,
122 body:
123 ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
124 }
125 end)
126
127 assert capture_log(fn ->
128 assert nil ==
129 Instance.get_or_update_favicon(
130 URI.parse("https://long-favicon.example.org/")
131 )
132 end) =~
133 "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
134 end
135
136 test "Handles not getting a favicon URL properly" do
137 Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
138 %Tesla.Env{
139 status: 200,
140 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
141 }
142 end)
143
144 refute capture_log(fn ->
145 assert nil ==
146 Instance.get_or_update_favicon(
147 URI.parse("https://no-favicon.example.org/")
148 )
149 end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
150 end
151 end
152 end