1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Instances.InstanceTest do
6 alias Pleroma.Instances.Instance
11 import ExUnit.CaptureLog
12 import Pleroma.Factory
14 setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
16 describe "set_reachable/1" do
17 test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
18 unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
19 instance = insert(:instance, unreachable_since: unreachable_since)
21 assert {:ok, instance} = Instance.set_reachable(instance.host)
22 refute instance.unreachable_since
25 test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
26 instance = insert(:instance, unreachable_since: nil)
28 assert {:ok, instance} = Instance.set_reachable(instance.host)
29 refute instance.unreachable_since
32 test "does NOT create an Instance record in case of no existing matching record" do
34 assert nil == Instance.set_reachable(host)
36 assert [] = Repo.all(Ecto.Query.from(i in Instance))
37 assert Instance.reachable?(host)
41 describe "set_unreachable/1" do
42 test "creates new record having `unreachable_since` to current time if record does not exist" do
43 assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
45 instance = Repo.get(Instance, instance.id)
46 assert instance.unreachable_since
47 assert "domain.com" == instance.host
50 test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
51 instance = insert(:instance, unreachable_since: nil)
52 refute instance.unreachable_since
54 assert {:ok, _} = Instance.set_unreachable(instance.host)
56 instance = Repo.get(Instance, instance.id)
57 assert instance.unreachable_since
60 test "does NOT modify `unreachable_since` value of existing record in case it's present" do
62 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
64 assert instance.unreachable_since
65 initial_value = instance.unreachable_since
67 assert {:ok, _} = Instance.set_unreachable(instance.host)
69 instance = Repo.get(Instance, instance.id)
70 assert initial_value == instance.unreachable_since
74 describe "set_unreachable/2" do
75 test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
77 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
79 assert instance.unreachable_since
81 past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
82 assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
84 instance = Repo.get(Instance, instance.id)
85 assert past_value == instance.unreachable_since
88 test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
90 insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
92 assert instance.unreachable_since
93 initial_value = instance.unreachable_since
95 assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
97 instance = Repo.get(Instance, instance.id)
98 assert initial_value == instance.unreachable_since
102 test "Scrapes favicon URLs" do
103 Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
106 body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
110 assert "https://favicon.example.org/favicon.png" ==
111 Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
114 test "Returns nil on too long favicon URLs" do
116 "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
118 Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
121 body: ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
125 assert capture_log(fn ->
127 Instance.get_or_update_favicon(
128 URI.parse("https://long-favicon.example.org/")
131 "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"