1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
14 import ExUnit.CaptureLog
15 import Pleroma.Factory
17 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
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)
24 assert {:ok, instance} = Instance.set_reachable(instance.host)
25 refute instance.unreachable_since
28 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
29 instance = insert(:instance, unreachable_since: nil)
31 assert {:ok, instance} = Instance.set_reachable(instance.host)
32 refute instance.unreachable_since
36 describe "set_unreachable/1" do
37 test "creates new record having `unreachable_since` to current time if record does not exist" do
38 assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
40 instance = Repo.get(Instance, instance.id)
41 assert instance.unreachable_since
42 assert "domain.com" == instance.host
45 test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
46 instance = insert(:instance, unreachable_since: nil)
47 refute instance.unreachable_since
49 assert {:ok, _} = Instance.set_unreachable(instance.host)
51 instance = Repo.get(Instance, instance.id)
52 assert instance.unreachable_since
55 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
57 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
59 assert instance.unreachable_since
60 initial_value = instance.unreachable_since
62 assert {:ok, _} = Instance.set_unreachable(instance.host)
64 instance = Repo.get(Instance, instance.id)
65 assert initial_value == instance.unreachable_since
69 describe "set_unreachable/2" do
70 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
72 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
74 assert instance.unreachable_since
76 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
77 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
79 instance = Repo.get(Instance, instance.id)
80 assert past_value == instance.unreachable_since
83 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
85 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
87 assert instance.unreachable_since
88 initial_value = instance.unreachable_since
90 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
92 instance = Repo.get(Instance, instance.id)
93 assert initial_value == instance.unreachable_since
97 describe "get_or_update_favicon/1" do
98 test "Scrapes favicon URLs" do
99 Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
102 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
106 assert "https://favicon.example.org/favicon.png" ==
107 Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
110 test "Returns nil on too long favicon URLs" do
112 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
114 Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
118 ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
122 assert capture_log(fn ->
124 Instance.get_or_update_favicon(
125 URI.parse("https://long-favicon.example.org/")
128 "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
131 test "Handles not getting a favicon URL properly" do
132 Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
135 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
139 refute capture_log(fn ->
141 Instance.get_or_update_favicon(
142 URI.parse("https://no-favicon.example.org/")
144 end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
147 test "Doesn't scrapes unreachable instances" do
148 instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
149 url = "https://" <> instance.host
151 assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
152 "Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
156 describe "get_or_update_metadata/1" do
157 test "Scrapes Wildebeest NodeInfo" do
159 %{url: "https://wildebeest.example.org/.well-known/nodeinfo"} ->
162 body: File.read!("test/fixtures/wildebeest-well-known-nodeinfo.json")
165 %{url: "https://wildebeest.example.org/nodeinfo/2.1"} ->
168 body: File.read!("test/fixtures/wildebeest-nodeinfo21.json")
173 software_name: "wildebeest",
174 software_repository: "https://github.com/cloudflare/wildebeest",
175 software_version: "0.0.1"
179 Instance.get_or_update_metadata(URI.parse("https://wildebeest.example.org/"))
181 expected = %Pleroma.Instances.Instance.Pleroma.Instances.Metadata{
182 software_name: "wildebeest",
183 software_repository: "https://github.com/cloudflare/wildebeest",
184 software_version: "0.0.1"
188 Repo.get_by(Pleroma.Instances.Instance, %{host: "wildebeest.example.org"}).metadata
191 test "Scrapes Mastodon NodeInfo" do
193 %{url: "https://mastodon.example.org/.well-known/nodeinfo"} ->
196 body: File.read!("test/fixtures/mastodon-well-known-nodeinfo.json")
199 %{url: "https://mastodon.example.org/nodeinfo/2.0"} ->
202 body: File.read!("test/fixtures/mastodon-nodeinfo20.json")
207 software_name: "mastodon",
208 software_version: "4.1.0"
212 Instance.get_or_update_metadata(URI.parse("https://mastodon.example.org/"))
216 test "delete_users_and_activities/1 deletes remote instance users and activities" do
217 [mario, luigi, _peach, wario] =
219 insert(:user, nickname: "mario@mushroom.kingdom", name: "Mario"),
220 insert(:user, nickname: "luigi@mushroom.kingdom", name: "Luigi"),
221 insert(:user, nickname: "peach@mushroom.kingdom", name: "Peach"),
222 insert(:user, nickname: "wario@greedville.biz", name: "Wario")
225 {:ok, post1} = CommonAPI.post(mario, %{status: "letsa go!"})
226 {:ok, post2} = CommonAPI.post(luigi, %{status: "itsa me... luigi"})
227 {:ok, post3} = CommonAPI.post(wario, %{status: "WHA-HA-HA!"})
229 {:ok, job} = Instance.delete_users_and_activities("mushroom.kingdom")
230 :ok = ObanHelpers.perform(job)
232 [mario, luigi, peach, wario] = Repo.reload(users)
234 refute mario.is_active
235 refute luigi.is_active
236 refute peach.is_active
237 refute peach.name == "Peach"
239 assert wario.is_active
240 assert wario.name == "Wario"
242 assert [nil, nil, %{}] = Repo.reload([post1, post2, post3])