instance: Handle not getting a favicon
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Fri, 11 Sep 2020 09:22:50 +0000 (11:22 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Fri, 11 Sep 2020 09:37:46 +0000 (11:37 +0200)
lib/pleroma/instances/instance.ex
test/web/instances/instance_test.exs

index 8bf53c090a8cc07a7df4ddb775af4cb8121535a3..6948651c7bacdbba4768fe8df50a68904a5eeae5 100644 (file)
@@ -159,13 +159,11 @@ defmodule Pleroma.Instances.Instance do
              Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}],
                adapter: [pool: :media]
              ),
-           favicon_rel <-
-             html
-             |> Floki.parse_document!()
-             |> Floki.attribute("link[rel=icon]", "href")
-             |> List.first(),
-           favicon <- URI.merge(instance_uri, favicon_rel) |> to_string(),
-           true <- is_binary(favicon) do
+           {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
+             {:parse,
+              html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
+           {_, favicon} when is_binary(favicon) <-
+             {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
         favicon
       else
         _ -> nil
index dc6ace843f43a69d475bc991601f4649024b6112..4f080510018d22ad827af469af29f791bcc51d23 100644 (file)
@@ -99,35 +99,54 @@ defmodule Pleroma.Instances.InstanceTest do
     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
+  describe "get_or_update_favicon/1" do
+    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{"
+    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
+
+    test "Handles not getting a favicon URL properly" do
+      Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
+        %Tesla.Env{
+          status: 200,
+          body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
+        }
+      end)
+
+      refute capture_log(fn ->
+               assert nil ==
+                        Instance.get_or_update_favicon(
+                          URI.parse("https://no-favicon.example.org/")
+                        )
+             end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
+    end
   end
 end