1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Instances.InstanceTest do
6 alias Pleroma.Instances
7 alias Pleroma.Instances.Instance
9 alias Pleroma.Tests.ObanHelpers
10 alias Pleroma.Web.CommonAPI
12 use Pleroma.DataCase, async: true
14 import ExUnit.CaptureLog
15 import Pleroma.Factory
18 clear_config([:instance, :federation_reachability_timeout_days], 1)
19 clear_config([:instances_nodeinfo, :enabled], true)
20 clear_config([:instances_favicons, :enabled], true)
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)
28 assert {:ok, instance} = Instance.set_reachable(instance.host)
29 refute instance.unreachable_since
32 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
33 instance = insert(:instance, unreachable_since: nil)
35 assert {:ok, instance} = Instance.set_reachable(instance.host)
36 refute instance.unreachable_since
39 test "does NOT create an Instance record in case of no existing matching record" do
41 assert nil == Instance.set_reachable(host)
43 assert [] = Repo.all(Ecto.Query.from(i in Instance))
44 assert Instance.reachable?(host)
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")
52 instance = Repo.get(Instance, instance.id)
53 assert instance.unreachable_since
54 assert "domain.com" == instance.host
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
61 assert {:ok, _} = Instance.set_unreachable(instance.host)
63 instance = Repo.get(Instance, instance.id)
64 assert instance.unreachable_since
67 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
69 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
71 assert instance.unreachable_since
72 initial_value = instance.unreachable_since
74 assert {:ok, _} = Instance.set_unreachable(instance.host)
76 instance = Repo.get(Instance, instance.id)
77 assert initial_value == instance.unreachable_since
81 describe "set_unreachable/2" do
82 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
84 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
86 assert instance.unreachable_since
88 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
89 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
91 instance = Repo.get(Instance, instance.id)
92 assert past_value == instance.unreachable_since
95 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
97 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
99 assert instance.unreachable_since
100 initial_value = instance.unreachable_since
102 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
104 instance = Repo.get(Instance, instance.id)
105 assert initial_value == instance.unreachable_since
109 describe "update_metadata/1" do
110 test "Scrapes favicon URLs and nodeinfo" do
112 %{url: "https://favicon.example.org/"} ->
115 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
118 %{url: "https://favicon.example.org/.well-known/nodeinfo"} ->
125 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
126 href: "https://favicon.example.org/nodeinfo/2.0"
132 %{url: "https://favicon.example.org/nodeinfo/2.0"} ->
135 body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
139 assert {:ok, %Instance{host: "favicon.example.org"}} =
140 Instance.update_metadata(URI.parse("https://favicon.example.org/"))
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"}}
147 test "Does not retain favicons that are too long" do
149 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
152 %{url: "https://long-favicon.example.org/"} ->
156 ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
159 %{url: "https://long-favicon.example.org/.well-known/nodeinfo"} ->
166 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
167 href: "https://long-favicon.example.org/nodeinfo/2.0"
173 %{url: "https://long-favicon.example.org/nodeinfo/2.0"} ->
176 body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
180 assert {:ok, %Instance{host: "long-favicon.example.org"}} =
181 Instance.update_metadata(URI.parse("https://long-favicon.example.org/"))
183 {:ok, instance} = Instance.get_cached_by_url("https://long-favicon.example.org/")
184 assert instance.favicon == nil
187 test "Handles not getting a favicon URL properly" do
189 %{url: "https://no-favicon.example.org/"} ->
192 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
195 %{url: "https://no-favicon.example.org/.well-known/nodeinfo"} ->
202 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
203 href: "https://no-favicon.example.org/nodeinfo/2.0"
209 %{url: "https://no-favicon.example.org/nodeinfo/2.0"} ->
212 body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
216 refute capture_log(fn ->
217 assert {:ok, %Instance{host: "no-favicon.example.org"}} =
218 Instance.update_metadata(URI.parse("https://no-favicon.example.org/"))
219 end) =~ "Instance.update_metadata(\"https://no-favicon.example.org/\") error: "
222 test "Doesn't scrape unreachable instances" do
223 instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
224 url = "https://" <> instance.host
226 assert {:discard, :unreachable} == Instance.update_metadata(URI.parse(url))
229 test "doesn't continue scraping nodeinfo if we can't find a link" do
231 %{url: "https://bad-nodeinfo.example.org/"} ->
234 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
237 %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
240 body: "oepsie woepsie de nodeinfo is kapotie uwu"
244 assert {:ok, %Instance{host: "bad-nodeinfo.example.org"}} =
245 Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
247 {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
248 assert instance.nodeinfo == nil
251 test "doesn't store bad json in the nodeinfo" do
253 %{url: "https://bad-nodeinfo.example.org/"} ->
256 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
259 %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
266 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
267 href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
273 %{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
276 body: "oepsie woepsie de json might be bad uwu"
280 assert {:ok, %Instance{host: "bad-nodeinfo.example.org"}} =
281 Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
283 {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
284 assert instance.nodeinfo == nil
287 test "doesn't store incredibly long json nodeinfo" do
288 too_long = String.duplicate("a", 50_000)
291 %{url: "https://bad-nodeinfo.example.org/"} ->
294 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
297 %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
304 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
305 href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
311 %{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
314 body: Jason.encode!(%{version: "2.0", software: %{name: too_long}})
318 assert {:ok, %Instance{host: "bad-nodeinfo.example.org"}} =
319 Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
321 {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
322 assert instance.nodeinfo == nil
326 test "delete_users_and_activities/1 deletes remote instance users and activities" do
327 [mario, luigi, _peach, wario] =
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")
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!"})
339 {:ok, job} = Instance.delete_users_and_activities("mushroom.kingdom")
340 :ok = ObanHelpers.perform(job)
342 [mario, luigi, peach, wario] = Repo.reload(users)
344 refute mario.is_active
345 refute luigi.is_active
346 refute peach.is_active
347 refute peach.name == "Peach"
349 assert wario.is_active
350 assert wario.name == "Wario"
352 assert [nil, nil, %{}] = Repo.reload([post1, post2, post3])