Fix rate_limiter_test.exs test "it restricts based on config values" (#233)
[akkoma] / test / pleroma / instances / instance_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 alias Pleroma.Tests.ObanHelpers
10 alias Pleroma.Web.CommonAPI
11
12 use Pleroma.DataCase
13
14 import ExUnit.CaptureLog
15 import Pleroma.Factory
16
17 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
18
19 describe "set_reachable/1" do
20 test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
21 unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
22 instance = insert(:instance, unreachable_since: unreachable_since)
23
24 assert {:ok, instance} = Instance.set_reachable(instance.host)
25 refute instance.unreachable_since
26 end
27
28 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
29 instance = insert(:instance, unreachable_since: nil)
30
31 assert {:ok, instance} = Instance.set_reachable(instance.host)
32 refute instance.unreachable_since
33 end
34
35 test "does NOT create an Instance record in case of no existing matching record" do
36 host = "domain.org"
37 assert nil == Instance.set_reachable(host)
38
39 assert [] = Repo.all(Ecto.Query.from(i in Instance))
40 assert Instance.reachable?(host)
41 end
42 end
43
44 describe "set_unreachable/1" do
45 test "creates new record having `unreachable_since` to current time if record does not exist" do
46 assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
47
48 instance = Repo.get(Instance, instance.id)
49 assert instance.unreachable_since
50 assert "domain.com" == instance.host
51 end
52
53 test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
54 instance = insert(:instance, unreachable_since: nil)
55 refute instance.unreachable_since
56
57 assert {:ok, _} = Instance.set_unreachable(instance.host)
58
59 instance = Repo.get(Instance, instance.id)
60 assert instance.unreachable_since
61 end
62
63 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
64 instance =
65 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
66
67 assert instance.unreachable_since
68 initial_value = instance.unreachable_since
69
70 assert {:ok, _} = Instance.set_unreachable(instance.host)
71
72 instance = Repo.get(Instance, instance.id)
73 assert initial_value == instance.unreachable_since
74 end
75 end
76
77 describe "set_unreachable/2" do
78 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
79 instance =
80 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
81
82 assert instance.unreachable_since
83
84 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
85 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
86
87 instance = Repo.get(Instance, instance.id)
88 assert past_value == instance.unreachable_since
89 end
90
91 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
92 instance =
93 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
94
95 assert instance.unreachable_since
96 initial_value = instance.unreachable_since
97
98 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
99
100 instance = Repo.get(Instance, instance.id)
101 assert initial_value == instance.unreachable_since
102 end
103 end
104
105 describe "get_or_update_favicon/1" do
106 test "Scrapes favicon URLs" do
107 Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
108 %Tesla.Env{
109 status: 200,
110 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
111 }
112 end)
113
114 assert "https://favicon.example.org/favicon.png" ==
115 Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
116 end
117
118 test "Returns nil on too long favicon URLs" do
119 long_favicon_url =
120 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
121
122 Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
123 %Tesla.Env{
124 status: 200,
125 body:
126 ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
127 }
128 end)
129
130 assert capture_log(fn ->
131 assert nil ==
132 Instance.get_or_update_favicon(
133 URI.parse("https://long-favicon.example.org/")
134 )
135 end) =~
136 "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
137 end
138
139 test "Handles not getting a favicon URL properly" do
140 Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
141 %Tesla.Env{
142 status: 200,
143 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
144 }
145 end)
146
147 refute capture_log(fn ->
148 assert nil ==
149 Instance.get_or_update_favicon(
150 URI.parse("https://no-favicon.example.org/")
151 )
152 end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
153 end
154
155 test "Doesn't scrapes unreachable instances" do
156 instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
157 url = "https://" <> instance.host
158
159 assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
160 "Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
161 end
162 end
163
164 test "delete_users_and_activities/1 deletes remote instance users and activities" do
165 [mario, luigi, _peach, wario] =
166 users = [
167 insert(:user, nickname: "mario@mushroom.kingdom", name: "Mario"),
168 insert(:user, nickname: "luigi@mushroom.kingdom", name: "Luigi"),
169 insert(:user, nickname: "peach@mushroom.kingdom", name: "Peach"),
170 insert(:user, nickname: "wario@greedville.biz", name: "Wario")
171 ]
172
173 {:ok, post1} = CommonAPI.post(mario, %{status: "letsa go!"})
174 {:ok, post2} = CommonAPI.post(luigi, %{status: "itsa me... luigi"})
175 {:ok, post3} = CommonAPI.post(wario, %{status: "WHA-HA-HA!"})
176
177 {:ok, job} = Instance.delete_users_and_activities("mushroom.kingdom")
178 :ok = ObanHelpers.perform(job)
179
180 [mario, luigi, peach, wario] = Repo.reload(users)
181
182 refute mario.is_active
183 refute luigi.is_active
184 refute peach.is_active
185 refute peach.name == "Peach"
186
187 assert wario.is_active
188 assert wario.name == "Wario"
189
190 assert [nil, nil, %{}] = Repo.reload([post1, post2, post3])
191 end
192 end