From dd775b6ae25f381cf76e00999fd7d37764870122 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 19 Dec 2023 09:52:32 +0100 Subject: [PATCH 1/3] fix(nodeinfo): fix getting application actor information from NodeInfo response Signed-off-by: Thomas Citharel --- lib/federation/node_info.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/federation/node_info.ex b/lib/federation/node_info.ex index 688abdac8..66e1bc43a 100644 --- a/lib/federation/node_info.ex +++ b/lib/federation/node_info.ex @@ -25,9 +25,10 @@ defmodule Mobilizon.Federation.NodeInfo do defp extract_application_actor(body) do body - |> Enum.find(%{rel: @application_uri, href: nil}, fn %{rel: rel, href: href} -> + |> Map.get("links", []) + |> Enum.find(%{"rel" => @application_uri, "href" => nil}, fn %{"rel" => rel, "href" => href} -> rel == @application_uri and is_binary(href) end) - |> Map.get(:href) + |> Map.get("href") end end From 00250ff33a97d5b7ef94acf2281f5a6ee72f28dd Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 19 Dec 2023 09:53:05 +0100 Subject: [PATCH 2/3] refactor(activitypub): cleanup unused imports and variables Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/relay.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/federation/activity_pub/relay.ex b/lib/federation/activity_pub/relay.ex index 3bc9e9212..50fef4737 100644 --- a/lib/federation/activity_pub/relay.ex +++ b/lib/federation/activity_pub/relay.ex @@ -16,7 +16,6 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do alias Mobilizon.Federation.{NodeInfo, WebFinger} alias Mobilizon.GraphQL.API.Follows alias Mobilizon.Service.Workers.Background - import Mobilizon.Federation.ActivityPub.Utils, only: [create_full_domain_string: 1] require Logger @@ -185,7 +184,7 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do defp fetch_actor("http://" <> address), do: fetch_actor(address) defp fetch_actor(address) do - %URI{host: host} = uri = URI.parse("http://" <> address) + %URI{host: host} = URI.parse("http://" <> address) cond do String.contains?(address, "@") -> From 78d3e76e465202c4fc246d7295007f07b09ca746 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 19 Dec 2023 09:53:41 +0100 Subject: [PATCH 3/3] test(nodeinfo): add tests for Mobilizon.Federation.NodeInfo Signed-off-by: Thomas Citharel --- test/federation/node_info_test.exs | 60 +++++++++++++++++++ test/fixtures/nodeinfo/regular.json | 8 +++ .../nodeinfo/wp-event-federation.json | 12 ++++ 3 files changed, 80 insertions(+) create mode 100644 test/federation/node_info_test.exs create mode 100644 test/fixtures/nodeinfo/regular.json create mode 100644 test/fixtures/nodeinfo/wp-event-federation.json diff --git a/test/federation/node_info_test.exs b/test/federation/node_info_test.exs new file mode 100644 index 000000000..10a5c4b38 --- /dev/null +++ b/test/federation/node_info_test.exs @@ -0,0 +1,60 @@ +defmodule Mobilizon.Federation.NodeInfoTest do + use Mobilizon.DataCase + import Mox + + alias Mobilizon.Federation.NodeInfo + alias Mobilizon.Service.HTTP.WebfingerClient.Mock, as: WebfingerClientMock + + @instance_domain "event-federation.eu" + @nodeinfo_fixture_path "test/fixtures/nodeinfo/wp-event-federation.json" + @nodeinfo_regular_fixture_path "test/fixtures/nodeinfo/regular.json" + + describe "getting application actor" do + test "from wordpress event federation" do + nodeinfo_data = @nodeinfo_fixture_path |> File.read!() |> Jason.decode!() + + WebfingerClientMock + |> expect(:call, fn + %{ + method: :get, + url: "https://event-federation.eu/.well-known/nodeinfo" + }, + _opts -> + {:ok, %Tesla.Env{status: 200, body: nodeinfo_data}} + end) + + assert "https://event-federation.eu/actor-relay" == + NodeInfo.application_actor(@instance_domain) + end + + test "with no FEP-2677 information" do + nodeinfo_data = @nodeinfo_regular_fixture_path |> File.read!() |> Jason.decode!() + + WebfingerClientMock + |> expect(:call, fn + %{ + method: :get, + url: "https://event-federation.eu/.well-known/nodeinfo" + }, + _opts -> + {:ok, %Tesla.Env{status: 200, body: nodeinfo_data}} + end) + + assert nil == NodeInfo.application_actor(@instance_domain) + end + + test "with no result" do + WebfingerClientMock + |> expect(:call, fn + %{ + method: :get, + url: "https://event-federation.eu/.well-known/nodeinfo" + }, + _opts -> + {:ok, %Tesla.Env{status: 404, body: ""}} + end) + + assert nil == NodeInfo.application_actor(@instance_domain) + end + end +end diff --git a/test/fixtures/nodeinfo/regular.json b/test/fixtures/nodeinfo/regular.json new file mode 100644 index 000000000..f18dfcfab --- /dev/null +++ b/test/fixtures/nodeinfo/regular.json @@ -0,0 +1,8 @@ +{ + "links": [ + { + "rel": "http:\/\/nodeinfo.diaspora.software\/ns\/schema\/2.0", + "href": "https:\/\/event-federation.eu\/wp-json\/activitypub\/1.0\/nodeinfo" + } + ] +} \ No newline at end of file diff --git a/test/fixtures/nodeinfo/wp-event-federation.json b/test/fixtures/nodeinfo/wp-event-federation.json new file mode 100644 index 000000000..be64bcb71 --- /dev/null +++ b/test/fixtures/nodeinfo/wp-event-federation.json @@ -0,0 +1,12 @@ +{ + "links": [ + { + "rel": "http:\/\/nodeinfo.diaspora.software\/ns\/schema\/2.0", + "href": "https:\/\/event-federation.eu\/wp-json\/activitypub\/1.0\/nodeinfo" + }, + { + "rel": "https://www.w3.org/ns/activitystreams#Application", + "href": "https://event-federation.eu/actor-relay" + } + ] +} \ No newline at end of file