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
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
35 test "does NOT create an Instance record in case of no existing matching record" do
37 assert nil == Instance.set_reachable(host)
39 assert [] = Repo.all(Ecto.Query.from(i in Instance))
40 assert Instance.reachable?(host)
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")
48 instance = Repo.get(Instance, instance.id)
49 assert instance.unreachable_since
50 assert "domain.com" == instance.host
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
57 assert {:ok, _} = Instance.set_unreachable(instance.host)
59 instance = Repo.get(Instance, instance.id)
60 assert instance.unreachable_since
63 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
65 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
67 assert instance.unreachable_since
68 initial_value = instance.unreachable_since
70 assert {:ok, _} = Instance.set_unreachable(instance.host)
72 instance = Repo.get(Instance, instance.id)
73 assert initial_value == instance.unreachable_since
77 describe "set_unreachable/2" do
78 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
80 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
82 assert instance.unreachable_since
84 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
85 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
87 instance = Repo.get(Instance, instance.id)
88 assert past_value == instance.unreachable_since
91 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
93 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
95 assert instance.unreachable_since
96 initial_value = instance.unreachable_since
98 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
100 instance = Repo.get(Instance, instance.id)
101 assert initial_value == instance.unreachable_since
105 describe "get_or_update_favicon/1" do
106 test "Scrapes favicon URLs" do
107 Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
110 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
114 assert "https://favicon.example.org/favicon.png" ==
115 Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
118 test "Returns nil on too long favicon URLs" do
120 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
122 Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
126 ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
130 assert capture_log(fn ->
132 Instance.get_or_update_favicon(
133 URI.parse("https://long-favicon.example.org/")
136 "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
139 test "Handles not getting a favicon URL properly" do
140 Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
143 body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
147 refute capture_log(fn ->
149 Instance.get_or_update_favicon(
150 URI.parse("https://no-favicon.example.org/")
152 end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
155 test "Doesn't scrapes unreachable instances" do
156 instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
157 url = "https://" <> instance.host
159 assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
160 "Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
164 test "delete_users_and_activities/1 deletes remote instance users and activities" do
165 [mario, luigi, _peach, wario] =
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")
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!"})
177 {:ok, job} = Instance.delete_users_and_activities("mushroom.kingdom")
178 :ok = ObanHelpers.perform(job)
180 [mario, luigi, peach, wario] = Repo.reload(users)
182 refute mario.is_active
183 refute luigi.is_active
184 refute peach.is_active
185 refute peach.name == "Peach"
187 assert wario.is_active
188 assert wario.name == "Wario"
190 assert [nil, nil, %{}] = Repo.reload([post1, post2, post3])