Merge branch 'stable' into stable-sync/2.1.1
[akkoma] / test / web / instances / instance_test.exs
index 3fd011fd3acd795223fa2694b85914c5786b7917..dc6ace843f43a69d475bc991601f4649024b6112 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Instances.InstanceTest do
@@ -8,15 +8,15 @@ defmodule Pleroma.Instances.InstanceTest do
 
   use Pleroma.DataCase
 
+  import ExUnit.CaptureLog
   import Pleroma.Factory
 
-  clear_config_all([:instance, :federation_reachability_timeout_days]) do
-    Pleroma.Config.put([:instance, :federation_reachability_timeout_days], 1)
-  end
+  setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
 
   describe "set_reachable/1" do
     test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
-      instance = insert(:instance, unreachable_since: NaiveDateTime.utc_now())
+      unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
+      instance = insert(:instance, unreachable_since: unreachable_since)
 
       assert {:ok, instance} = Instance.set_reachable(instance.host)
       refute instance.unreachable_since
@@ -98,4 +98,36 @@ defmodule Pleroma.Instances.InstanceTest do
       assert initial_value == instance.unreachable_since
     end
   end
+
+  test "Scrapes favicon URLs" do
+    Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
+      %Tesla.Env{
+        status: 200,
+        body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
+      }
+    end)
+
+    assert "https://favicon.example.org/favicon.png" ==
+             Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
+  end
+
+  test "Returns nil on too long favicon URLs" do
+    long_favicon_url =
+      "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
+
+    Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
+      %Tesla.Env{
+        status: 200,
+        body: ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
+      }
+    end)
+
+    assert capture_log(fn ->
+             assert nil ==
+                      Instance.get_or_update_favicon(
+                        URI.parse("https://long-favicon.example.org/")
+                      )
+           end) =~
+             "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
+  end
 end