Scrape instance nodeinfo (#251)
[akkoma] / test / pleroma / instances / instance_test.exs
index e49922724e12fc9890c2221e9dfc2f7f7ac043d2..adc847da5bcc791513755d753da087a7d1623f24 100644 (file)
@@ -9,12 +9,16 @@ defmodule Pleroma.Instances.InstanceTest do
   alias Pleroma.Tests.ObanHelpers
   alias Pleroma.Web.CommonAPI
 
-  use Pleroma.DataCase
+  use Pleroma.DataCase, async: true
 
   import ExUnit.CaptureLog
   import Pleroma.Factory
 
-  setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
+  setup_all do
+    clear_config([:instance, :federation_reachability_timeout_days], 1)
+    clear_config([:instances_nodeinfo, :enabled], true)
+    clear_config([:instances_favicons, :enabled], true)
+  end
 
   describe "set_reachable/1" do
     test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
@@ -102,62 +106,220 @@ defmodule Pleroma.Instances.InstanceTest do
     end
   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>]
-        }
+  describe "update_metadata/1" do
+    test "Scrapes favicon URLs and nodeinfo" 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>]
+          }
+
+        %{url: "https://favicon.example.org/.well-known/nodeinfo"} ->
+          %Tesla.Env{
+            status: 200,
+            body:
+              Jason.encode!(%{
+                links: [
+                  %{
+                    rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
+                    href: "https://favicon.example.org/nodeinfo/2.0"
+                  }
+                ]
+              })
+          }
+
+        %{url: "https://favicon.example.org/nodeinfo/2.0"} ->
+          %Tesla.Env{
+            status: 200,
+            body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
+          }
       end)
 
-      assert "https://favicon.example.org/favicon.png" ==
-               Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
+      assert {:ok, true} ==
+               Instance.update_metadata(URI.parse("https://favicon.example.org/"))
+
+      {:ok, instance} = Instance.get_cached_by_url("https://favicon.example.org/")
+      assert instance.favicon == "https://favicon.example.org/favicon.png"
+      assert instance.nodeinfo == %{"version" => "2.0", "software" => %{"name" => "Akkoma"}}
     end
 
-    test "Returns nil on too long favicon URLs" do
+    test "Does not retain favicons that are too long" 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>]
-        }
+      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>]
+          }
+
+        %{url: "https://long-favicon.example.org/.well-known/nodeinfo"} ->
+          %Tesla.Env{
+            status: 200,
+            body:
+              Jason.encode!(%{
+                links: [
+                  %{
+                    rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
+                    href: "https://long-favicon.example.org/nodeinfo/2.0"
+                  }
+                ]
+              })
+          }
+
+        %{url: "https://long-favicon.example.org/nodeinfo/2.0"} ->
+          %Tesla.Env{
+            status: 200,
+            body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
+          }
       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{"
+      assert {:ok, true} ==
+               Instance.update_metadata(URI.parse("https://long-favicon.example.org/"))
+
+      {:ok, instance} = Instance.get_cached_by_url("https://long-favicon.example.org/")
+      assert instance.favicon == nil
     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>]
-        }
+      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>]
+          }
+
+        %{url: "https://no-favicon.example.org/.well-known/nodeinfo"} ->
+          %Tesla.Env{
+            status: 200,
+            body:
+              Jason.encode!(%{
+                links: [
+                  %{
+                    rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
+                    href: "https://no-favicon.example.org/nodeinfo/2.0"
+                  }
+                ]
+              })
+          }
+
+        %{url: "https://no-favicon.example.org/nodeinfo/2.0"} ->
+          %Tesla.Env{
+            status: 200,
+            body: Jason.encode!(%{version: "2.0", software: %{name: "Akkoma"}})
+          }
       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: "
+               assert {:ok, true} =
+                        Instance.update_metadata(URI.parse("https://no-favicon.example.org/"))
+             end) =~ "Instance.update_metadata(\"https://no-favicon.example.org/\") error: "
     end
 
-    test "Doesn't scrapes unreachable instances" do
+    test "Doesn't scrape unreachable instances" do
       instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
       url = "https://" <> instance.host
 
-      assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
-               "Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
+      assert {:discard, :unreachable} == Instance.update_metadata(URI.parse(url))
+    end
+
+    test "doesn't continue scraping nodeinfo if we can't find a link" do
+      Tesla.Mock.mock(fn
+        %{url: "https://bad-nodeinfo.example.org/"} ->
+          %Tesla.Env{
+            status: 200,
+            body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
+          }
+
+        %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
+          %Tesla.Env{
+            status: 200,
+            body: "oepsie woepsie de nodeinfo is kapotie uwu"
+          }
+      end)
+
+      assert {:ok, true} ==
+               Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
+
+      {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
+      assert instance.nodeinfo == nil
+    end
+
+    test "doesn't store bad json in the nodeinfo" do
+      Tesla.Mock.mock(fn
+        %{url: "https://bad-nodeinfo.example.org/"} ->
+          %Tesla.Env{
+            status: 200,
+            body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
+          }
+
+        %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
+          %Tesla.Env{
+            status: 200,
+            body:
+              Jason.encode!(%{
+                links: [
+                  %{
+                    rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
+                    href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
+                  }
+                ]
+              })
+          }
+
+        %{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
+          %Tesla.Env{
+            status: 200,
+            body: "oepsie woepsie de json might be bad uwu"
+          }
+      end)
+
+      assert {:ok, true} ==
+               Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
+
+      {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
+      assert instance.nodeinfo == nil
+    end
+
+    test "doesn't store incredibly long json nodeinfo" do
+      too_long = String.duplicate("a", 50_000)
+
+      Tesla.Mock.mock(fn
+        %{url: "https://bad-nodeinfo.example.org/"} ->
+          %Tesla.Env{
+            status: 200,
+            body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
+          }
+
+        %{url: "https://bad-nodeinfo.example.org/.well-known/nodeinfo"} ->
+          %Tesla.Env{
+            status: 200,
+            body:
+              Jason.encode!(%{
+                links: [
+                  %{
+                    rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
+                    href: "https://bad-nodeinfo.example.org/nodeinfo/2.0"
+                  }
+                ]
+              })
+          }
+
+        %{url: "https://bad-nodeinfo.example.org/nodeinfo/2.0"} ->
+          %Tesla.Env{
+            status: 200,
+            body: Jason.encode!(%{version: "2.0", software: %{name: too_long}})
+          }
+      end)
+
+      assert {:ok, true} ==
+               Instance.update_metadata(URI.parse("https://bad-nodeinfo.example.org/"))
+
+      {:ok, instance} = Instance.get_cached_by_url("https://bad-nodeinfo.example.org/")
+      assert instance.nodeinfo == nil
     end
   end