Test Relay, switch to runtime configuration.
authorlain <lain@soykaf.club>
Sat, 8 Sep 2018 12:02:38 +0000 (14:02 +0200)
committerWilliam Pitcock <nenolod@dereferenced.org>
Thu, 1 Nov 2018 09:01:43 +0000 (09:01 +0000)
lib/pleroma/web/activity_pub/activity_pub_controller.ex
lib/pleroma/web/federator/federator.ex
lib/pleroma/web/router.ex
test/web/activity_pub/activity_pub_controller_test.exs
test/web/activity_pub/relay_test.exs [new file with mode: 0644]
test/web/federator_test.exs

index a7b1c0079f56771487259c4fc358a61e86bea3d1..531e98237a1abb537da41ffbdb0f9010e93ecea3 100644 (file)
@@ -6,11 +6,25 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
   alias Pleroma.Web.ActivityPub.Relay
   alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.Web.Federator
+  alias Pleroma.Config
 
   require Logger
 
   action_fallback(:errors)
 
+  plug(:relay_active? when action in [:relay])
+
+  def relay_active?(conn, _) do
+    if Config.get([:instance, :allow_relay]) do
+      conn
+    else
+      conn
+      |> put_status(404)
+      |> json(%{error: "not found"})
+      |> halt
+    end
+  end
+
   def user(conn, %{"nickname" => nickname}) do
     with %User{} = user <- User.get_cached_by_nickname(nickname),
          {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
index 078f3ec119baa2206c83d426e88b0fdb38225db8..9ea2507a1aacf4c495491b20dfc69ad8785ba17b 100644 (file)
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.Federator do
   alias Pleroma.Web.ActivityPub.Relay
   alias Pleroma.Web.ActivityPub.Transmogrifier
   alias Pleroma.Web.ActivityPub.Utils
+  alias Pleroma.Config
   require Logger
 
   @websub Application.get_env(:pleroma, :websub)
@@ -71,9 +72,9 @@ defmodule Pleroma.Web.Federator do
         Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
         Pleroma.Web.Salmon.publish(actor, activity)
 
-        if Mix.env() != :test do
+        if Config.get([:instance, :allow_relay]) do
           Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
-          Pleroma.Web.ActivityPub.Relay.publish(activity)
+          Relay.publish(activity)
         end
       end
 
index b531b618887ec7c33a60966f0e6f6beb8c1fd568..7b7affe5e5a50391be37e25aa09d71444dc9f8b8 100644 (file)
@@ -5,7 +5,6 @@ defmodule Pleroma.Web.Router do
 
   @instance Application.get_env(:pleroma, :instance)
   @federating Keyword.get(@instance, :federating)
-  @allow_relay Keyword.get(@instance, :allow_relay)
   @public Keyword.get(@instance, :public)
   @registrations_open Keyword.get(@instance, :registrations_open)
 
@@ -354,11 +353,9 @@ defmodule Pleroma.Web.Router do
   end
 
   if @federating do
-    if @allow_relay do
-      scope "/relay", Pleroma.Web.ActivityPub do
-        pipe_through(:ap_relay)
-        get("/", ActivityPubController, :relay)
-      end
+    scope "/relay", Pleroma.Web.ActivityPub do
+      pipe_through(:ap_relay)
+      get("/", ActivityPubController, :relay)
     end
 
     scope "/", Pleroma.Web.ActivityPub do
index e63cd65839cddde275c42510e84e6636e6801c51..5b46bbe768e6cdb2e188e6d06d26015615553e71 100644 (file)
@@ -4,6 +4,29 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
   alias Pleroma.Web.ActivityPub.{UserView, ObjectView}
   alias Pleroma.{Repo, User}
   alias Pleroma.Activity
+  alias Pleroma.Config
+
+  describe "/relay" do
+    test "with the relay active, it returns the relay user", %{conn: conn} do
+      Config.put([:instance, :allow_relay], true)
+
+      res =
+        conn
+        |> get(activity_pub_path(conn, :relay))
+        |> json_response(200)
+
+      assert res["id"] =~ "/relay"
+    end
+
+    test "with the relay disabled, it returns 404", %{conn: conn} do
+      Config.put([:instance, :allow_relay], false)
+
+      res =
+        conn
+        |> get(activity_pub_path(conn, :relay))
+        |> json_response(404)
+    end
+  end
 
   describe "/users/:nickname" do
     test "it returns a json representation of the user", %{conn: conn} do
diff --git a/test/web/activity_pub/relay_test.exs b/test/web/activity_pub/relay_test.exs
new file mode 100644 (file)
index 0000000..41d13e0
--- /dev/null
@@ -0,0 +1,11 @@
+defmodule Pleroma.Web.ActivityPub.RelayTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.Web.ActivityPub.Relay
+
+  test "gets an actor for the relay" do
+    user = Relay.get_actor()
+
+    assert user.ap_id =~ "/relay"
+  end
+end
index 09533362a5ca80037954c47795755796e7b4a133..966702935d9406f46dc2c2a41fb5517490a40d5c 100644 (file)
@@ -1,6 +1,10 @@
 defmodule Pleroma.Web.FederatorTest do
   alias Pleroma.Web.Federator
+  alias Pleroma.Web.CommonAPI
+  alias Pleroma.Config
   use Pleroma.DataCase
+  import Pleroma.Factory
+  import Mock
 
   test "enqueues an element according to priority" do
     queue = [%{item: 1, priority: 2}]
@@ -17,4 +21,45 @@ defmodule Pleroma.Web.FederatorTest do
 
     assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
   end
+
+  describe "Publish an activity" do
+    setup do
+      user = insert(:user)
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
+
+      relay_mock = {
+        Pleroma.Web.ActivityPub.Relay,
+        [],
+        [publish: fn _activity -> send(self(), :relay_publish) end]
+      }
+
+      %{activity: activity, relay_mock: relay_mock}
+    end
+
+    test "with relays active, it publishes to the relay", %{
+      activity: activity,
+      relay_mock: relay_mock
+    } do
+      Config.put([:instance, :allow_relay], true)
+
+      with_mocks([relay_mock]) do
+        Federator.handle(:publish, activity)
+      end
+
+      assert_received :relay_publish
+    end
+
+    test "with relays deactivated, it does not publish to the relay", %{
+      activity: activity,
+      relay_mock: relay_mock
+    } do
+      Config.put([:instance, :allow_relay], false)
+
+      with_mocks([relay_mock]) do
+        Federator.handle(:publish, activity)
+      end
+
+      refute_received :relay_publish
+    end
+  end
 end