forked from AkkomaGang/akkoma
Merge branch 'feature/relay-tests' into 'develop'
relay tests See merge request pleroma/pleroma!411
This commit is contained in:
commit
eba9a62024
9 changed files with 125 additions and 8 deletions
|
@ -10,6 +10,7 @@ def start(_type, _args) do
|
||||||
# Define workers and child supervisors to be supervised
|
# Define workers and child supervisors to be supervised
|
||||||
children =
|
children =
|
||||||
[
|
[
|
||||||
|
worker(Pleroma.Config, [Application.get_all_env(:pleroma)]),
|
||||||
# Start the Ecto repository
|
# Start the Ecto repository
|
||||||
supervisor(Pleroma.Repo, []),
|
supervisor(Pleroma.Repo, []),
|
||||||
# Start the endpoint when the application starts
|
# Start the endpoint when the application starts
|
||||||
|
|
15
lib/pleroma/config.ex
Normal file
15
lib/pleroma/config.ex
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
defmodule Pleroma.Config do
|
||||||
|
use Agent
|
||||||
|
|
||||||
|
def start_link(initial) do
|
||||||
|
Agent.start_link(fn -> initial end, name: __MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(path) do
|
||||||
|
Agent.get(__MODULE__, Kernel, :get_in, [path])
|
||||||
|
end
|
||||||
|
|
||||||
|
def put(path, value) do
|
||||||
|
Agent.update(__MODULE__, Kernel, :put_in, [path, value])
|
||||||
|
end
|
||||||
|
end
|
|
@ -6,11 +6,25 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|
||||||
alias Pleroma.Web.ActivityPub.Relay
|
alias Pleroma.Web.ActivityPub.Relay
|
||||||
alias Pleroma.Web.ActivityPub.Utils
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
alias Pleroma.Web.Federator
|
alias Pleroma.Web.Federator
|
||||||
|
alias Pleroma.Config
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
action_fallback(:errors)
|
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
|
def user(conn, %{"nickname" => nickname}) do
|
||||||
with %User{} = user <- User.get_cached_by_nickname(nickname),
|
with %User{} = user <- User.get_cached_by_nickname(nickname),
|
||||||
{:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
|
{:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
|
||||||
|
|
|
@ -7,6 +7,7 @@ defmodule Pleroma.Web.Federator do
|
||||||
alias Pleroma.Web.ActivityPub.Relay
|
alias Pleroma.Web.ActivityPub.Relay
|
||||||
alias Pleroma.Web.ActivityPub.Transmogrifier
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
||||||
alias Pleroma.Web.ActivityPub.Utils
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
|
alias Pleroma.Config
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@websub Application.get_env(:pleroma, :websub)
|
@websub Application.get_env(:pleroma, :websub)
|
||||||
|
@ -71,9 +72,9 @@ def handle(:publish, activity) do
|
||||||
Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
|
Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
|
||||||
Pleroma.Web.Salmon.publish(actor, activity)
|
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)
|
Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
|
||||||
Pleroma.Web.ActivityPub.Relay.publish(activity)
|
Relay.publish(activity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ defmodule Pleroma.Web.Router do
|
||||||
|
|
||||||
@instance Application.get_env(:pleroma, :instance)
|
@instance Application.get_env(:pleroma, :instance)
|
||||||
@federating Keyword.get(@instance, :federating)
|
@federating Keyword.get(@instance, :federating)
|
||||||
@allow_relay Keyword.get(@instance, :allow_relay)
|
|
||||||
@public Keyword.get(@instance, :public)
|
@public Keyword.get(@instance, :public)
|
||||||
@registrations_open Keyword.get(@instance, :registrations_open)
|
@registrations_open Keyword.get(@instance, :registrations_open)
|
||||||
|
|
||||||
|
@ -354,12 +353,10 @@ defmodule Pleroma.Web.Router do
|
||||||
end
|
end
|
||||||
|
|
||||||
if @federating do
|
if @federating do
|
||||||
if @allow_relay do
|
|
||||||
scope "/relay", Pleroma.Web.ActivityPub do
|
scope "/relay", Pleroma.Web.ActivityPub do
|
||||||
pipe_through(:ap_relay)
|
pipe_through(:ap_relay)
|
||||||
get("/", ActivityPubController, :relay)
|
get("/", ActivityPubController, :relay)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
scope "/", Pleroma.Web.ActivityPub do
|
scope "/", Pleroma.Web.ActivityPub do
|
||||||
pipe_through(:activitypub)
|
pipe_through(:activitypub)
|
||||||
|
|
10
test/config_test.exs
Normal file
10
test/config_test.exs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
defmodule Pleroma.ConfigTest do
|
||||||
|
use Pleroma.DataCase
|
||||||
|
alias Pleroma.Config
|
||||||
|
|
||||||
|
test "get returns the item at the path if there is one" do
|
||||||
|
Config.put([:instance, :name], "Plemora")
|
||||||
|
assert Config.get([:instance, :name]) == "Plemora"
|
||||||
|
assert Config.get([:unknown]) == nil
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,6 +4,29 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
|
||||||
alias Pleroma.Web.ActivityPub.{UserView, ObjectView}
|
alias Pleroma.Web.ActivityPub.{UserView, ObjectView}
|
||||||
alias Pleroma.{Repo, User}
|
alias Pleroma.{Repo, User}
|
||||||
alias Pleroma.Activity
|
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
|
describe "/users/:nickname" do
|
||||||
test "it returns a json representation of the user", %{conn: conn} do
|
test "it returns a json representation of the user", %{conn: conn} do
|
||||||
|
|
11
test/web/activity_pub/relay_test.exs
Normal file
11
test/web/activity_pub/relay_test.exs
Normal file
|
@ -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
|
|
@ -1,6 +1,10 @@
|
||||||
defmodule Pleroma.Web.FederatorTest do
|
defmodule Pleroma.Web.FederatorTest do
|
||||||
alias Pleroma.Web.Federator
|
alias Pleroma.Web.Federator
|
||||||
|
alias Pleroma.Web.CommonAPI
|
||||||
|
alias Pleroma.Config
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
import Pleroma.Factory
|
||||||
|
import Mock
|
||||||
|
|
||||||
test "enqueues an element according to priority" do
|
test "enqueues an element according to priority" do
|
||||||
queue = [%{item: 1, priority: 2}]
|
queue = [%{item: 1, priority: 2}]
|
||||||
|
@ -17,4 +21,45 @@ test "pop first item" do
|
||||||
|
|
||||||
assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
|
assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue