Scrape instance nodeinfo (#251)
[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, async: true
13
14 import ExUnit.CaptureLog
15 import Pleroma.Factory
16
17 setup_all do
18 clear_config([:instance, :federation_reachability_timeout_days], 1)
19 clear_config([:instances_nodeinfo, :enabled], true)
20 clear_config([:instances_favicons, :enabled], true)
21 end
22
23 describe "set_reachable/1" do
24 test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
25 unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
26 instance = insert(:instance, unreachable_since: unreachable_since)
27
28 assert {:ok, instance} = Instance.set_reachable(instance.host)
29 refute instance.unreachable_since
30 end
31
32 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
33 instance = insert(:instance, unreachable_since: nil)
34
35 assert {:ok, instance} = Instance.set_reachable(instance.host)
36 refute instance.unreachable_since
37 end
38
39 test "does NOT create an Instance record in case of no existing matching record" do
40 host = "domain.org"
41 assert nil == Instance.set_reachable(host)
42
43 assert [] = Repo.all(Ecto.Query.from(i in Instance))
44 assert Instance.reachable?(host)
45 end
46 end
47
48 describe "set_unreachable/1" do
49 test "creates new record having `unreachable_since` to current time if record does not exist" do
50 assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
51
52 instance = Repo.get(Instance, instance.id)
53 assert instance.unreachable_since
54 assert "domain.com" == instance.host
55 end
56
57 test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
58 instance = insert(:instance, unreachable_since: nil)
59 refute instance.unreachable_since
60
61 assert {:ok, _} = Instance.set_unreachable(instance.host)
62
63 instance = Repo.get(Instance, instance.id)
64 assert instance.unreachable_since
65 end
66
67 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
68 instance =
69 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
70
71 assert instance.unreachable_since
72 initial_value = instance.unreachable_since
73
74 assert {:ok, _} = Instance.set_unreachable(instance.host)
75
76 instance = Repo.get(Instance, instance.id)
77 assert initial_value == instance.unreachable_since
78 end
79 end
80
81 describe "set_unreachable/2" do
82 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
83 instance =
84 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
85
86 assert instance.unreachable_since
87
88 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
89 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
90
91 instance = Repo.get(Instance, instance.id)
92 assert past_value == instance.unreachable_since
93 end
94
95 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
96 instance =
97 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
98
99 assert instance.unreachable_since
100 initial_value = instance.unreachable_since
101
102 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
103
104 instance = Repo.get(Instance, instance.id)
105 assert initial_value == instance.unreachable_since
106 end
107 end
108
109 describe "update_metadata/1" do
110 test "Scrapes favicon URLs and nodeinfo" do
111 Tesla.Mock.mock(fn
112 %{url: "https://favicon.example.org/"} ->
113 %Tesla.Env{
114 status: 200,
115 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
116 }
117
118 %{url: "https://favicon.example.org/.well-known/nodeinfo"} ->
119 %Tesla.Env{
120 status: 200,
121 body:
122 Jason.encode!(%{
123 links: [
124 %{
125 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
126 href: "https://favicon.example.org/nodeinfo/2.0"
127 }
128 ]
129 })
130 }
131
132 %{url: "https://favicon.example.org/nodeinfo/2.0"} ->
133 %Tesla.Env{
134 status: 200,
135 body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
136 }
137 end)
138
139 assert {:ok, true} ==
140 Instance.update_metadata(URI.parse("https://favicon.example.org/"))
141
142 {:ok, instance} = Instance.get_cached_by_url("https://favicon.example.org/")
143 assert instance.favicon == "https://favicon.example.org/favicon.png"
144 assert instance.nodeinfo == %{"version" => "2.0", "software" => %{"name" => "Akkoma"}}
145 end
146
147 test "Does not retain favicons that are too long" do
148 long_favicon_url =
149 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
150
151 Tesla.Mock.mock(fn
152 %{url: "https://long-favicon.example.org/"} ->
153 %Tesla.Env{
154 status: 200,
155 body:
156 ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
157 }
158
159 %{url: "https://long-favicon.example.org/.well-known/nodeinfo"} ->
160 %Tesla.Env{
161 status: 200,
162 body:
163 Jason.encode!(%{
164 links: [
165 %{
166 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
167 href: "https://long-favicon.example.org/nodeinfo/2.0"
168 }
169 ]
170 })
171 }
172
173 %{url: "https://long-favicon.example.org/nodeinfo/2.0"} ->
174 %Tesla.Env{
175 status: 200,
176 body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
177 }
178 end)
179
180 assert {:ok, true} ==
181 Instance.update_metadata(URI.parse("https://long-favicon.example.org/"))
182
183 {:ok, instance} = Instance.get_cached_by_url("https://long-favicon.example.org/")
184 assert instance.favicon == nil
185 end
186
187 test "Handles not getting a favicon URL properly" do
188 Tesla.Mock.mock(fn
189 %{url: "https://no-favicon.example.org/"} ->
190 %Tesla.Env{
191 status: 200,
192 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
193 }
194
195 %{url: "https://no-favicon.example.org/.well-known/nodeinfo"} ->
196 %Tesla.Env{
197 status: 200,
198 body:
199 Jason.encode!(%{
200 links: [
201 %{
202 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
203 href: "https://no-favicon.example.org/nodeinfo/2.0"
204 }
205 ]
206 })
207 }
208
209 %{url: "https://no-favicon.example.org/nodeinfo/2.0"} ->
210 %Tesla.Env{
211 status: 200,
212 body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
213 }
214 end)
215
216 refute capture_log(fn ->
217 assert {:ok, true} =
218 Instance.update_metadata(URI.parse("https://no-favicon.example.org/"))
219 end) =~ "Instance.update_metadata(\"https://no-favicon.example.org/\") error: "
220 end
221
222 test "Doesn't scrape unreachable instances" do
223 instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
224 url = "https://" <> instance.host
225
226 assert {:discard, :unreachable} == Instance.update_metadata(URI.parse(url))
227 end
228
229 test "doesn't continue scraping nodeinfo if we can't find a link" do
230 Tesla.Mock.mock(fn
231 %{url: "https://bad-nodeinfo.example.org/"} ->
232 %Tesla.Env{
233 status: 200,
234 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
235 }
236
237 %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
238 %Tesla.Env{
239 status: 200,
240 body: "oepsie woepsie de nodeinfo is kapotie uwu"
241 }
242 end)
243
244 assert {:ok, true} ==
245 Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
246
247 {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
248 assert instance.nodeinfo == nil
249 end
250
251 test "doesn't store bad json in the nodeinfo" do
252 Tesla.Mock.mock(fn
253 %{url: "https://bad-nodeinfo.example.org/"} ->
254 %Tesla.Env{
255 status: 200,
256 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
257 }
258
259 %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
260 %Tesla.Env{
261 status: 200,
262 body:
263 Jason.encode!(%{
264 links: [
265 %{
266 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
267 href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
268 }
269 ]
270 })
271 }
272
273 %{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
274 %Tesla.Env{
275 status: 200,
276 body: "oepsie woepsie de json might be bad uwu"
277 }
278 end)
279
280 assert {:ok, true} ==
281 Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
282
283 {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
284 assert instance.nodeinfo == nil
285 end
286
287 test "doesn't store incredibly long json nodeinfo" do
288 too_long = String.duplicate("a", 50_000)
289
290 Tesla.Mock.mock(fn
291 %{url: "https://bad-nodeinfo.example.org/"} ->
292 %Tesla.Env{
293 status: 200,
294 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
295 }
296
297 %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
298 %Tesla.Env{
299 status: 200,
300 body:
301 Jason.encode!(%{
302 links: [
303 %{
304 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
305 href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
306 }
307 ]
308 })
309 }
310
311 %{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
312 %Tesla.Env{
313 status: 200,
314 body: Jason.encode!(%{version: "2.0", software: %{name: too_long}})
315 }
316 end)
317
318 assert {:ok, true} ==
319 Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
320
321 {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
322 assert instance.nodeinfo == nil
323 end
324 end
325
326 test "delete_users_and_activities/1 deletes remote instance users and activities" do
327 [mario, luigi, _peach, wario] =
328 users = [
329 insert(:user, nickname: "mario@mushroom.kingdom", name: "Mario"),
330 insert(:user, nickname: "luigi@mushroom.kingdom", name: "Luigi"),
331 insert(:user, nickname: "peach@mushroom.kingdom", name: "Peach"),
332 insert(:user, nickname: "wario@greedville.biz", name: "Wario")
333 ]
334
335 {:ok, post1} = CommonAPI.post(mario, %{status: "letsa go!"})
336 {:ok, post2} = CommonAPI.post(luigi, %{status: "itsa me... luigi"})
337 {:ok, post3} = CommonAPI.post(wario, %{status: "WHA-HA-HA!"})
338
339 {:ok, job} = Instance.delete_users_and_activities("mushroom.kingdom")
340 :ok = ObanHelpers.perform(job)
341
342 [mario, luigi, peach, wario] = Repo.reload(users)
343
344 refute mario.is_active
345 refute luigi.is_active
346 refute peach.is_active
347 refute peach.name == "Peach"
348
349 assert wario.is_active
350 assert wario.name == "Wario"
351
352 assert [nil, nil, %{}] = Repo.reload([post1, post2, post3])
353 end
354 end