2021-04-22 12:17:56 +02:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.ActorTest do
|
|
|
|
use Mobilizon.DataCase
|
2021-11-13 18:45:01 +01:00
|
|
|
import Mox
|
2021-04-22 12:17:56 +02:00
|
|
|
import Mock
|
|
|
|
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2021-11-13 18:45:01 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
|
|
|
alias Mobilizon.Service.HTTP.ActivityPub.Mock
|
2021-11-14 16:27:53 +01:00
|
|
|
alias Mobilizon.Service.HTTP.HostMetaClient.Mock, as: HostMetaClientMock
|
|
|
|
alias Mobilizon.Service.HTTP.WebfingerClient.Mock, as: WebfingerClientMock
|
2021-04-22 12:17:56 +02:00
|
|
|
|
|
|
|
describe "fetching actor from its url" do
|
2021-11-13 18:45:01 +01:00
|
|
|
@actor_url "https://framapiaf.org/users/tcit"
|
2021-04-22 12:17:56 +02:00
|
|
|
test "returns an actor from nickname" do
|
2021-11-13 18:45:01 +01:00
|
|
|
actor_data =
|
|
|
|
File.read!("test/fixtures/mastodon-actor.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|> Map.put("id", @actor_url)
|
|
|
|
|> Map.put("preferredUsername", "tcit")
|
|
|
|
|> Map.put("discoverable", true)
|
2021-04-22 12:17:56 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: @actor_url}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
|
|
|
end)
|
|
|
|
|
2021-11-14 16:27:53 +01:00
|
|
|
HostMetaClientMock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: "http://framapiaf.org/.well-known/host-meta"}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 404, body: ""}}
|
|
|
|
end)
|
|
|
|
|
|
|
|
webfinger_data =
|
|
|
|
File.read!("test/fixtures/webfinger/mastodon-webfinger.json")
|
|
|
|
|> String.replace("social.tcit.fr", "framapiaf.org")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
WebfingerClientMock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: "http://framapiaf.org/.well-known/webfinger?resource=acct:tcit@framapiaf.org"
|
|
|
|
},
|
|
|
|
_opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: webfinger_data}}
|
|
|
|
end)
|
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
assert {:ok,
|
|
|
|
%Actor{preferred_username: "tcit", domain: "framapiaf.org", visibility: :public} =
|
|
|
|
_actor} = ActivityPubActor.make_actor_from_nickname("tcit@framapiaf.org")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns an actor from nickname when not discoverable" do
|
|
|
|
actor_data =
|
|
|
|
File.read!("test/fixtures/mastodon-actor.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|> Map.put("id", @actor_url)
|
|
|
|
|> Map.put("preferredUsername", "tcit")
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: @actor_url}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
|
|
|
end)
|
|
|
|
|
2021-11-14 16:27:53 +01:00
|
|
|
HostMetaClientMock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: "http://framapiaf.org/.well-known/host-meta"}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 404, body: ""}}
|
|
|
|
end)
|
|
|
|
|
|
|
|
webfinger_data =
|
|
|
|
File.read!("test/fixtures/webfinger/mastodon-webfinger.json")
|
|
|
|
|> String.replace("social.tcit.fr", "framapiaf.org")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|
|
|
|
WebfingerClientMock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: "http://framapiaf.org/.well-known/webfinger?resource=acct:tcit@framapiaf.org"
|
|
|
|
},
|
|
|
|
_opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: webfinger_data}}
|
|
|
|
end)
|
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
assert {:ok,
|
|
|
|
%Actor{preferred_username: "tcit", domain: "framapiaf.org", visibility: :unlisted} =
|
|
|
|
_actor} = ActivityPubActor.make_actor_from_nickname("tcit@framapiaf.org")
|
2021-04-22 12:17:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns an actor from url" do
|
2021-11-13 18:45:01 +01:00
|
|
|
actor_data =
|
|
|
|
File.read!("test/fixtures/mastodon-actor.json")
|
|
|
|
|> Jason.decode!()
|
|
|
|
|> Map.put("id", @actor_url)
|
|
|
|
|> Map.put("preferredUsername", "tcit")
|
|
|
|
|
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: @actor_url}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
|
|
|
end)
|
|
|
|
|
2021-04-22 12:17:56 +02:00
|
|
|
# Initial fetch
|
2021-11-13 18:45:01 +01:00
|
|
|
# Unlisted because discoverable is not present in the JSON payload
|
|
|
|
assert {:ok,
|
|
|
|
%Actor{preferred_username: "tcit", domain: "framapiaf.org", visibility: :unlisted}} =
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(@actor_url)
|
2021-04-22 12:17:56 +02:00
|
|
|
|
|
|
|
# Fetch uses cache if Actors.needs_update? returns false
|
|
|
|
with_mocks([
|
|
|
|
{Actors, [:passthrough],
|
|
|
|
[
|
|
|
|
get_actor_by_url: fn @actor_url, false ->
|
|
|
|
{:ok,
|
|
|
|
%Actor{
|
|
|
|
preferred_username: "tcit",
|
|
|
|
domain: "framapiaf.org"
|
|
|
|
}}
|
|
|
|
end,
|
|
|
|
needs_update?: fn _ -> false end
|
|
|
|
]},
|
|
|
|
{ActivityPubActor, [:passthrough],
|
2021-11-29 10:29:26 +01:00
|
|
|
make_actor_from_url: fn @actor_url, [] ->
|
2021-04-22 12:17:56 +02:00
|
|
|
{:ok,
|
|
|
|
%Actor{
|
|
|
|
preferred_username: "tcit",
|
|
|
|
domain: "framapiaf.org"
|
|
|
|
}}
|
|
|
|
end}
|
|
|
|
]) do
|
|
|
|
assert {:ok, %Actor{preferred_username: "tcit", domain: "framapiaf.org"}} =
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(@actor_url)
|
|
|
|
|
|
|
|
assert_called(Actors.needs_update?(:_))
|
2021-11-29 10:29:26 +01:00
|
|
|
refute called(ActivityPubActor.make_actor_from_url(@actor_url, []))
|
2021-04-22 12:17:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Fetch doesn't use cache if Actors.needs_update? returns true
|
|
|
|
with_mocks([
|
|
|
|
{Actors, [:passthrough],
|
|
|
|
[
|
|
|
|
get_actor_by_url: fn @actor_url, false ->
|
|
|
|
{:ok,
|
|
|
|
%Actor{
|
|
|
|
preferred_username: "tcit",
|
|
|
|
domain: "framapiaf.org"
|
|
|
|
}}
|
|
|
|
end,
|
|
|
|
needs_update?: fn _ -> true end
|
|
|
|
]},
|
|
|
|
{ActivityPubActor, [:passthrough],
|
2021-11-29 10:29:26 +01:00
|
|
|
make_actor_from_url: fn @actor_url, [] ->
|
2021-04-22 12:17:56 +02:00
|
|
|
{:ok,
|
|
|
|
%Actor{
|
|
|
|
preferred_username: "tcit",
|
|
|
|
domain: "framapiaf.org"
|
|
|
|
}}
|
|
|
|
end}
|
|
|
|
]) do
|
|
|
|
assert {:ok, %Actor{preferred_username: "tcit", domain: "framapiaf.org"}} =
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(@actor_url)
|
|
|
|
|
|
|
|
assert_called(ActivityPubActor.get_or_fetch_actor_by_url(@actor_url))
|
|
|
|
assert_called(Actors.get_actor_by_url(@actor_url, false))
|
|
|
|
assert_called(Actors.needs_update?(:_))
|
2021-11-29 10:29:26 +01:00
|
|
|
assert_called(ActivityPubActor.make_actor_from_url(@actor_url, []))
|
2021-04-22 12:17:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "handles remote actor being deleted" do
|
2021-11-13 18:45:01 +01:00
|
|
|
Mock
|
|
|
|
|> expect(:call, fn
|
|
|
|
%{method: :get, url: @actor_url}, _opts ->
|
|
|
|
{:ok, %Tesla.Env{status: 410, body: ""}}
|
|
|
|
end)
|
2021-04-22 12:17:56 +02:00
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
assert match?(
|
|
|
|
{:error, :actor_deleted},
|
2021-11-29 10:29:26 +01:00
|
|
|
ActivityPubActor.make_actor_from_url(@actor_url, [])
|
2021-11-13 18:45:01 +01:00
|
|
|
)
|
2021-04-22 12:17:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@public_url "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
test "activitystreams#Public uri returns Relay actor" do
|
|
|
|
assert ActivityPubActor.get_or_fetch_actor_by_url(@public_url) == {:ok, Relay.get_actor()}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|