173
test/federation/activity_pub/transmogrifier/announces_test.exs
Normal file
173
test/federation/activity_pub/transmogrifier/announces_test.exs
Normal file
@@ -0,0 +1,173 @@
|
||||
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.AnnouncesTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
import Mox
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Discussions
|
||||
alias Mobilizon.Discussions.{Comment, Discussion}
|
||||
alias Mobilizon.Federation.ActivityPub.Transmogrifier
|
||||
alias Mobilizon.Federation.ActivityStream.Convertible
|
||||
alias Mobilizon.Service.HTTP.ActivityPub.Mock
|
||||
alias Mobilizon.Tombstone
|
||||
|
||||
@comment_text "my comment"
|
||||
|
||||
describe "incoming announces for discussion creation" do
|
||||
setup :verify_on_exit!
|
||||
|
||||
test "by group member works" do
|
||||
actor = insert(:actor)
|
||||
group = insert(:group)
|
||||
insert(:member, parent: group, actor: actor, role: :member)
|
||||
|
||||
%Comment{url: comment_url} =
|
||||
comment = build(:comment, actor: actor, attributed_to: group, event: nil)
|
||||
|
||||
comment_data = Convertible.model_to_as(comment)
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{method: :get, url: ^comment_url}, _opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: comment_data}}
|
||||
end)
|
||||
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-announce.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", group.url)
|
||||
|> Map.put("object", comment.url)
|
||||
|
||||
{:ok, _, %Comment{actor: %Actor{url: actor_url}, url: comment_url}} =
|
||||
Transmogrifier.handle_incoming(data)
|
||||
|
||||
assert actor_url == comment.actor.url
|
||||
|
||||
assert comment_url == comment.url
|
||||
end
|
||||
end
|
||||
|
||||
describe "handle incoming announces for discussion updates" do
|
||||
setup :verify_on_exit!
|
||||
|
||||
@updated_title "Updated title"
|
||||
|
||||
test "by group member works" do
|
||||
actor =
|
||||
insert(:actor,
|
||||
domain: "otherremoteinstance.tld",
|
||||
url: "http://otherremoteinstance.tld/@somemember"
|
||||
)
|
||||
|
||||
group =
|
||||
insert(:group,
|
||||
url: "http://remoteinstance.tld/@mygroup",
|
||||
domain: "remoteinstance.tld",
|
||||
members_url: "http://remoteinstance.tld/@mygroup/members"
|
||||
)
|
||||
|
||||
insert(:member, parent: group, actor: actor, role: :member)
|
||||
|
||||
%Comment{url: _comment_url} =
|
||||
comment =
|
||||
insert(:comment,
|
||||
actor: actor,
|
||||
attributed_to: group,
|
||||
text: @comment_text,
|
||||
url: "http://otherremoteinstance.tld/@somemember/uuid"
|
||||
)
|
||||
|
||||
%Discussion{url: discussion_url} =
|
||||
discussion =
|
||||
insert(:discussion,
|
||||
last_comment: comment,
|
||||
comments: [comment],
|
||||
creator: actor,
|
||||
actor: group,
|
||||
url: "http://otherremoteinstance.tld/@mygroup/c/talk-of-something-sh0rt-uu1d"
|
||||
)
|
||||
|
||||
discussion_updated = Map.put(discussion, :title, @updated_title)
|
||||
|
||||
discussion_updated_data = Convertible.model_to_as(discussion_updated)
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{url: ^discussion_url}, _opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: discussion_updated_data}}
|
||||
end)
|
||||
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-announce.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", group.url)
|
||||
|> Map.put("object", discussion_url)
|
||||
|
||||
assert {:ok, _, %Discussion{title: title}} = Transmogrifier.handle_incoming(data)
|
||||
assert title == @updated_title
|
||||
end
|
||||
end
|
||||
|
||||
describe "handle incoming announces for discussion deletion" do
|
||||
setup :verify_on_exit!
|
||||
|
||||
test "by group member works" do
|
||||
actor =
|
||||
insert(:actor,
|
||||
url: "http://otherremoteinstance.tld/@somemember",
|
||||
domain: "otherremoteinstance.tld"
|
||||
)
|
||||
|
||||
group =
|
||||
insert(:group,
|
||||
url: "http://remoteinstance.tld/@mygroup",
|
||||
domain: "remoteinstance.tld",
|
||||
members_url: "http://remoteinstance.tld/@mygroup/members"
|
||||
)
|
||||
|
||||
insert(:member, parent: group, actor: actor, role: :member)
|
||||
|
||||
%Comment{url: comment_url} =
|
||||
comment =
|
||||
insert(:comment,
|
||||
actor: actor,
|
||||
attributed_to: group,
|
||||
text: @comment_text,
|
||||
url: "http://otherremoteinstance.tld/comment/uuid"
|
||||
)
|
||||
|
||||
tombstone = build(:tombstone, uri: comment.url, actor: actor)
|
||||
tombstone_data = Convertible.model_to_as(tombstone)
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{url: ^comment_url}, _opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: tombstone_data}}
|
||||
end)
|
||||
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-announce.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", group.url)
|
||||
|> Map.put("object", comment.url)
|
||||
|
||||
%Comment{deleted_at: deleted_at, text: comment_text} =
|
||||
Discussions.get_comment_from_url(comment.url)
|
||||
|
||||
assert is_nil(deleted_at)
|
||||
assert comment_text == @comment_text
|
||||
|
||||
{:ok, _, %Comment{deleted_at: deleted_at, text: comment_text}} =
|
||||
Transmogrifier.handle_incoming(data)
|
||||
|
||||
refute is_nil(deleted_at)
|
||||
refute comment_text == @comment_text
|
||||
|
||||
%Tombstone{actor_id: _actor_id, uri: tombstone_uri} = Tombstone.find_tombstone(comment_url)
|
||||
|
||||
# assert actor_id == comment.actor.id
|
||||
|
||||
assert tombstone_uri == comment.url
|
||||
end
|
||||
end
|
||||
end
|
||||
155
test/federation/activity_pub/transmogrifier/comments_test.exs
Normal file
155
test/federation/activity_pub/transmogrifier/comments_test.exs
Normal file
@@ -0,0 +1,155 @@
|
||||
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.CommentsTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
import Mox
|
||||
import ExUnit.CaptureLog
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Discussions
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier}
|
||||
alias Mobilizon.Federation.ActivityStream.Convertible
|
||||
alias Mobilizon.Service.HTTP.ActivityPub.Mock
|
||||
|
||||
describe "handle incoming comments" do
|
||||
setup :verify_on_exit!
|
||||
|
||||
test "it ignores an incoming comment if we already have it" do
|
||||
comment = insert(:comment)
|
||||
comment = Repo.preload(comment, [:attributed_to])
|
||||
|
||||
activity = %{
|
||||
"type" => "Create",
|
||||
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
"actor" => comment.actor.url,
|
||||
"object" => Convertible.model_to_as(comment)
|
||||
}
|
||||
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-post-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("object", activity["object"])
|
||||
|
||||
assert {:ok, nil, _} = Transmogrifier.handle_incoming(data)
|
||||
end
|
||||
|
||||
test "it fetches replied-to activities if we don't have them" do
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-post-activity.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
reply_to_url = "https://blob.cat/objects/02fdea3d-932c-4348-9ecb-3f9eb3fbdd94"
|
||||
|
||||
object =
|
||||
data["object"]
|
||||
|> Map.put("inReplyTo", reply_to_url)
|
||||
|
||||
data =
|
||||
data
|
||||
|> Map.put("object", object)
|
||||
|
||||
reply_to_data =
|
||||
File.read!("test/fixtures/pleroma-comment-object.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{method: :get, url: ^reply_to_url}, _opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: reply_to_data}}
|
||||
end)
|
||||
|
||||
{:ok, returned_activity, _} = Transmogrifier.handle_incoming(data)
|
||||
|
||||
%Comment{} =
|
||||
origin_comment =
|
||||
Discussions.get_comment_from_url(
|
||||
"https://blob.cat/objects/02fdea3d-932c-4348-9ecb-3f9eb3fbdd94"
|
||||
)
|
||||
|
||||
assert returned_activity.data["object"]["inReplyTo"] ==
|
||||
"https://blob.cat/objects/02fdea3d-932c-4348-9ecb-3f9eb3fbdd94"
|
||||
|
||||
assert returned_activity.data["object"]["inReplyTo"] == origin_comment.url
|
||||
end
|
||||
|
||||
@url_404 "https://404.site/whatever"
|
||||
test "it does not crash if the object in inReplyTo can't be fetched" do
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-post-activity.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
object =
|
||||
data["object"]
|
||||
|> Map.put("inReplyTo", @url_404)
|
||||
|
||||
data =
|
||||
data
|
||||
|> Map.put("object", object)
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{method: :get, url: "https://404.site/whatever"}, _opts ->
|
||||
{:ok, %Tesla.Env{status: 404, body: "Not found"}}
|
||||
end)
|
||||
|
||||
assert capture_log([level: :warn], fn ->
|
||||
{:ok, _returned_activity, _entity} = Transmogrifier.handle_incoming(data)
|
||||
end) =~ "[warn] Parent object is something we don't handle"
|
||||
end
|
||||
|
||||
test "it works for incoming notices" do
|
||||
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
|
||||
|
||||
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(data)
|
||||
|
||||
assert data["id"] ==
|
||||
"https://framapiaf.org/users/admin/statuses/99512778738411822/activity"
|
||||
|
||||
assert data["to"] == [
|
||||
"https://www.w3.org/ns/activitystreams#Public",
|
||||
"https://framapiaf.org/users/tcit"
|
||||
]
|
||||
|
||||
# assert data["cc"] == [
|
||||
# "https://framapiaf.org/users/admin/followers",
|
||||
# "http://mobilizon.com/@tcit"
|
||||
# ]
|
||||
|
||||
assert data["actor"] == "https://framapiaf.org/users/admin"
|
||||
|
||||
object = data["object"]
|
||||
assert object["id"] == "https://framapiaf.org/users/admin/statuses/99512778738411822"
|
||||
|
||||
assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
|
||||
|
||||
# assert object["cc"] == [
|
||||
# "https://framapiaf.org/users/admin/followers",
|
||||
# "http://localtesting.pleroma.lol/users/lain"
|
||||
# ]
|
||||
|
||||
assert object["actor"] == "https://framapiaf.org/users/admin"
|
||||
assert object["attributedTo"] == "https://framapiaf.org/users/admin"
|
||||
|
||||
{:ok, %Actor{}} = Actors.get_actor_by_url(object["actor"])
|
||||
end
|
||||
|
||||
test "it works for incoming notices with hashtags" do
|
||||
data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Jason.decode!()
|
||||
|
||||
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(data)
|
||||
assert Enum.at(data["object"]["tag"], 0)["name"] == "@tcit@framapiaf.org"
|
||||
assert Enum.at(data["object"]["tag"], 1)["name"] == "#moo"
|
||||
end
|
||||
|
||||
test "it works for incoming notices with url not being a string (prismo)" do
|
||||
data = File.read!("test/fixtures/prismo-url-map.json") |> Jason.decode!()
|
||||
|
||||
assert {:error, :not_supported} == Transmogrifier.handle_incoming(data)
|
||||
# Pages without groups are not supported
|
||||
# {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
|
||||
|
||||
# assert data["object"]["url"] == "https://prismo.news/posts/83"
|
||||
end
|
||||
end
|
||||
end
|
||||
122
test/federation/activity_pub/transmogrifier/follow_test.exs
Normal file
122
test/federation/activity_pub/transmogrifier/follow_test.exs
Normal file
@@ -0,0 +1,122 @@
|
||||
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier}
|
||||
|
||||
describe "handle incoming follow accept activities" do
|
||||
test "it works for incoming accepts which were pre-accepted" do
|
||||
follower = insert(:actor)
|
||||
followed = insert(:actor)
|
||||
|
||||
refute Actors.is_following(follower, followed)
|
||||
|
||||
{:ok, follow_activity, _} = ActivityPub.follow(follower, followed)
|
||||
assert Actors.is_following(follower, followed)
|
||||
|
||||
accept_data =
|
||||
File.read!("test/fixtures/mastodon-accept-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", followed.url)
|
||||
|
||||
object =
|
||||
accept_data["object"]
|
||||
|> Map.put("actor", follower.url)
|
||||
|> Map.put("id", follow_activity.data["id"])
|
||||
|
||||
accept_data = Map.put(accept_data, "object", object)
|
||||
|
||||
{:ok, activity, _} = Transmogrifier.handle_incoming(accept_data)
|
||||
refute activity.local
|
||||
|
||||
assert activity.data["object"]["id"] == follow_activity.data["id"]
|
||||
|
||||
{:ok, follower} = Actors.get_actor_by_url(follower.url)
|
||||
|
||||
assert Actors.is_following(follower, followed)
|
||||
end
|
||||
|
||||
test "it works for incoming accepts which are referenced by IRI only" do
|
||||
follower = insert(:actor)
|
||||
followed = insert(:actor)
|
||||
|
||||
{:ok, follow_activity, _} = ActivityPub.follow(follower, followed)
|
||||
|
||||
accept_data =
|
||||
File.read!("test/fixtures/mastodon-accept-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", followed.url)
|
||||
|> Map.put("object", follow_activity.data["id"])
|
||||
|
||||
{:ok, activity, _} = Transmogrifier.handle_incoming(accept_data)
|
||||
assert activity.data["object"]["id"] == follow_activity.data["id"]
|
||||
assert activity.data["object"]["id"] =~ "/follow/"
|
||||
assert activity.data["id"] =~ "/accept/follow/"
|
||||
|
||||
{:ok, follower} = Actors.get_actor_by_url(follower.url)
|
||||
|
||||
assert Actors.is_following(follower, followed)
|
||||
end
|
||||
|
||||
test "it fails for incoming accepts which cannot be correlated" do
|
||||
follower = insert(:actor)
|
||||
followed = insert(:actor)
|
||||
|
||||
accept_data =
|
||||
File.read!("test/fixtures/mastodon-accept-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", followed.url)
|
||||
|
||||
accept_data =
|
||||
Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.url))
|
||||
|
||||
:error = Transmogrifier.handle_incoming(accept_data)
|
||||
|
||||
{:ok, follower} = Actors.get_actor_by_url(follower.url)
|
||||
|
||||
refute Actors.is_following(follower, followed)
|
||||
end
|
||||
end
|
||||
|
||||
describe "handle incoming follow reject activities" do
|
||||
test "it fails for incoming rejects which cannot be correlated" do
|
||||
follower = insert(:actor)
|
||||
followed = insert(:actor)
|
||||
|
||||
accept_data =
|
||||
File.read!("test/fixtures/mastodon-reject-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", followed.url)
|
||||
|
||||
accept_data =
|
||||
Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.url))
|
||||
|
||||
:error = Transmogrifier.handle_incoming(accept_data)
|
||||
|
||||
{:ok, follower} = Actors.get_actor_by_url(follower.url)
|
||||
|
||||
refute Actors.is_following(follower, followed)
|
||||
end
|
||||
|
||||
test "it works for incoming rejects which are referenced by IRI only" do
|
||||
follower = insert(:actor)
|
||||
followed = insert(:actor)
|
||||
|
||||
{:ok, follow_activity, _} = ActivityPub.follow(follower, followed)
|
||||
|
||||
assert Actors.is_following(follower, followed)
|
||||
|
||||
reject_data =
|
||||
File.read!("test/fixtures/mastodon-reject-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", followed.url)
|
||||
|> Map.put("object", follow_activity.data["id"])
|
||||
|
||||
{:ok, %Activity{data: _}, _} = Transmogrifier.handle_incoming(reject_data)
|
||||
|
||||
refute Actors.is_following(follower, followed)
|
||||
end
|
||||
end
|
||||
end
|
||||
60
test/federation/activity_pub/transmogrifier/invite_test.exs
Normal file
60
test/federation/activity_pub/transmogrifier/invite_test.exs
Normal file
@@ -0,0 +1,60 @@
|
||||
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.InviteTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.{Actor, Member}
|
||||
alias Mobilizon.Federation.ActivityPub.Transmogrifier
|
||||
|
||||
describe "handle Invite activities on group" do
|
||||
test "it accepts Invite activities" do
|
||||
%Actor{url: group_url, id: group_id} = group = insert(:group)
|
||||
%Actor{url: group_admin_url, id: group_admin_id} = group_admin = insert(:actor)
|
||||
|
||||
%Member{} =
|
||||
_group_admin_member =
|
||||
insert(:member, parent: group, actor: group_admin, role: :administrator)
|
||||
|
||||
%Actor{url: invitee_url, id: invitee_id} = _invitee = insert(:actor)
|
||||
|
||||
invite_data =
|
||||
File.read!("test/fixtures/mobilizon-invite-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", group_admin_url)
|
||||
|> Map.put("object", group_url)
|
||||
|> Map.put("target", invitee_url)
|
||||
|
||||
assert {:ok, activity, %Member{}} = Transmogrifier.handle_incoming(invite_data)
|
||||
assert %Member{} = member = Actors.get_member_by_url(invite_data["id"])
|
||||
assert member.actor.id == invitee_id
|
||||
assert member.parent.id == group_id
|
||||
assert member.role == :invited
|
||||
assert member.invited_by_id == group_admin_id
|
||||
end
|
||||
|
||||
test "it refuses Invite activities for " do
|
||||
%Actor{url: group_url, id: group_id} = group = insert(:group)
|
||||
%Actor{url: group_admin_url, id: group_admin_id} = group_admin = insert(:actor)
|
||||
|
||||
%Member{} =
|
||||
_group_admin_member =
|
||||
insert(:member, parent: group, actor: group_admin, role: :administrator)
|
||||
|
||||
%Actor{url: invitee_url, id: invitee_id} = _invitee = insert(:actor)
|
||||
|
||||
invite_data =
|
||||
File.read!("test/fixtures/mobilizon-invite-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", group_admin_url)
|
||||
|> Map.put("object", group_url)
|
||||
|> Map.put("target", invitee_url)
|
||||
|
||||
assert {:ok, activity, %Member{}} = Transmogrifier.handle_incoming(invite_data)
|
||||
assert %Member{} = member = Actors.get_member_by_url(invite_data["id"])
|
||||
assert member.actor.id == invitee_id
|
||||
assert member.parent.id == group_id
|
||||
assert member.role == :invited
|
||||
assert member.invited_by_id == group_admin_id
|
||||
end
|
||||
end
|
||||
end
|
||||
104
test/federation/activity_pub/transmogrifier/join_test.exs
Normal file
104
test/federation/activity_pub/transmogrifier/join_test.exs
Normal file
@@ -0,0 +1,104 @@
|
||||
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.JoinTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
import ExUnit.CaptureLog
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.{Event, Participant}
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Federation.ActivityPub.Transmogrifier
|
||||
|
||||
describe "handle incoming join activities" do
|
||||
@join_message "I want to get in!"
|
||||
test "it accepts Join activities" do
|
||||
%Actor{url: organizer_url} = organizer = insert(:actor)
|
||||
%Actor{url: participant_url} = _participant = insert(:actor)
|
||||
|
||||
%Event{url: event_url} = _event = insert(:event, organizer_actor: organizer)
|
||||
|
||||
join_data =
|
||||
File.read!("test/fixtures/mobilizon-join-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", participant_url)
|
||||
|> Map.put("object", event_url)
|
||||
|> Map.put("participationMessage", @join_message)
|
||||
|
||||
assert {:ok, activity, %Participant{} = participant} =
|
||||
Transmogrifier.handle_incoming(join_data)
|
||||
|
||||
assert participant.metadata.message == @join_message
|
||||
assert participant.role == :participant
|
||||
|
||||
assert activity.data["type"] == "Accept"
|
||||
assert activity.data["object"]["object"] == event_url
|
||||
assert activity.data["object"]["id"] =~ "/join/event/"
|
||||
assert activity.data["object"]["type"] =~ "Join"
|
||||
assert activity.data["object"]["participationMessage"] == @join_message
|
||||
assert activity.data["actor"] == organizer_url
|
||||
assert activity.data["id"] =~ "/accept/join/"
|
||||
end
|
||||
end
|
||||
|
||||
describe "handle incoming accept join activities" do
|
||||
test "it accepts Accept activities for Join activities" do
|
||||
%Actor{url: organizer_url} = organizer = insert(:actor)
|
||||
%Actor{} = participant_actor = insert(:actor)
|
||||
|
||||
%Event{} = event = insert(:event, organizer_actor: organizer, join_options: :restricted)
|
||||
|
||||
{:ok, join_activity, participation} =
|
||||
ActivityPub.join(event, participant_actor, false, %{metadata: %{role: :not_approved}})
|
||||
|
||||
accept_data =
|
||||
File.read!("test/fixtures/mastodon-accept-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", organizer_url)
|
||||
|> Map.put("object", participation.url)
|
||||
|
||||
{:ok, accept_activity, _} = Transmogrifier.handle_incoming(accept_data)
|
||||
assert accept_activity.data["object"]["id"] == join_activity.data["id"]
|
||||
assert accept_activity.data["object"]["id"] =~ "/join/"
|
||||
assert accept_activity.data["id"] =~ "/accept/join/"
|
||||
|
||||
# We don't accept already accepted Accept activities
|
||||
:error = Transmogrifier.handle_incoming(accept_data)
|
||||
end
|
||||
end
|
||||
|
||||
describe "handle incoming reject join activities" do
|
||||
test "it accepts Reject activities for Join activities" do
|
||||
%Actor{url: organizer_url} = organizer = insert(:actor)
|
||||
%Actor{} = participant_actor = insert(:actor)
|
||||
|
||||
%Event{} = event = insert(:event, organizer_actor: organizer, join_options: :restricted)
|
||||
|
||||
{:ok, join_activity, participation} = ActivityPub.join(event, participant_actor)
|
||||
|
||||
reject_data =
|
||||
File.read!("test/fixtures/mastodon-reject-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", organizer_url)
|
||||
|> Map.put("object", participation.url)
|
||||
|
||||
{:ok, reject_activity, _} = Transmogrifier.handle_incoming(reject_data)
|
||||
assert reject_activity.data["object"]["id"] == join_activity.data["id"]
|
||||
assert reject_activity.data["object"]["id"] =~ "/join/"
|
||||
assert reject_activity.data["id"] =~ "/reject/join/"
|
||||
|
||||
# We don't accept already rejected Reject activities
|
||||
assert capture_log([level: :warn], fn ->
|
||||
assert :error == Transmogrifier.handle_incoming(reject_data)
|
||||
end) =~
|
||||
"Unable to process Reject activity \"http://mastodon.example.org/users/admin#rejects/follows/4\". Object \"#{
|
||||
join_activity.data["id"]
|
||||
}\" wasn't found."
|
||||
|
||||
# Organiser is not present since we use factories directly
|
||||
assert event.id
|
||||
|> Events.list_participants_for_event()
|
||||
|> Map.get(:elements)
|
||||
|> Enum.map(& &1.role) == [:rejected]
|
||||
end
|
||||
end
|
||||
end
|
||||
60
test/federation/activity_pub/transmogrifier/leave_test.exs
Normal file
60
test/federation/activity_pub/transmogrifier/leave_test.exs
Normal file
@@ -0,0 +1,60 @@
|
||||
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.LeaveTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.{Event, Participant}
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Federation.ActivityPub.Transmogrifier
|
||||
|
||||
describe "handle incoming leave activities on events" do
|
||||
test "it accepts Leave activities" do
|
||||
%Actor{url: _organizer_url} = organizer = insert(:actor)
|
||||
%Actor{url: participant_url} = participant_actor = insert(:actor)
|
||||
|
||||
%Event{url: event_url} =
|
||||
event = insert(:event, organizer_actor: organizer, join_options: :restricted)
|
||||
|
||||
organizer_participation =
|
||||
%Participant{} = insert(:participant, event: event, actor: organizer, role: :creator)
|
||||
|
||||
{:ok, _join_activity, _participation} = ActivityPub.join(event, participant_actor)
|
||||
|
||||
join_data =
|
||||
File.read!("test/fixtures/mobilizon-leave-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", participant_url)
|
||||
|> Map.put("object", event_url)
|
||||
|
||||
assert {:ok, activity, _} = Transmogrifier.handle_incoming(join_data)
|
||||
|
||||
assert activity.data["object"] == event_url
|
||||
assert activity.data["actor"] == participant_url
|
||||
|
||||
# The only participant left is the organizer
|
||||
assert event.id
|
||||
|> Events.list_participants_for_event()
|
||||
|> Map.get(:elements)
|
||||
|> Enum.map(& &1.id) ==
|
||||
[organizer_participation.id]
|
||||
end
|
||||
|
||||
test "it refuses Leave activities when actor is the only organizer" do
|
||||
%Actor{url: organizer_url} = organizer = insert(:actor)
|
||||
|
||||
%Event{url: event_url} =
|
||||
event = insert(:event, organizer_actor: organizer, join_options: :restricted)
|
||||
|
||||
%Participant{} = insert(:participant, event: event, actor: organizer, role: :creator)
|
||||
|
||||
join_data =
|
||||
File.read!("test/fixtures/mobilizon-leave-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("actor", organizer_url)
|
||||
|> Map.put("object", event_url)
|
||||
|
||||
assert :error = Transmogrifier.handle_incoming(join_data)
|
||||
end
|
||||
end
|
||||
end
|
||||
85
test/federation/activity_pub/transmogrifier/undo_test.exs
Normal file
85
test/federation/activity_pub/transmogrifier/undo_test.exs
Normal file
@@ -0,0 +1,85 @@
|
||||
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.UndoTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
import Mox
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier}
|
||||
alias Mobilizon.Service.HTTP.ActivityPub.Mock
|
||||
|
||||
describe "handle incoming undo activities" do
|
||||
test "it works for incoming unannounces with an existing notice" do
|
||||
comment = insert(:comment)
|
||||
|
||||
announce_data =
|
||||
File.read!("test/fixtures/mastodon-announce.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("object", comment.url)
|
||||
|
||||
actor_data =
|
||||
File.read!("test/fixtures/mastodon-actor.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{method: :get, url: "https://framapiaf.org/users/Framasoft"}, _opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
||||
end)
|
||||
|
||||
{:ok, _, %Comment{}} = Transmogrifier.handle_incoming(announce_data)
|
||||
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-undo-announce.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("object", announce_data)
|
||||
|> Map.put("actor", announce_data["actor"])
|
||||
|
||||
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(data)
|
||||
|
||||
assert data["type"] == "Undo"
|
||||
assert data["object"]["type"] == "Announce"
|
||||
assert data["object"]["object"] == comment.url
|
||||
|
||||
assert data["object"]["id"] ==
|
||||
"https://framapiaf.org/users/peertube/statuses/104584600044284729/activity"
|
||||
end
|
||||
|
||||
test "it works for incomming unfollows with an existing follow" do
|
||||
actor = insert(:actor)
|
||||
|
||||
follow_data =
|
||||
File.read!("test/fixtures/mastodon-follow-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("object", actor.url)
|
||||
|
||||
actor_data =
|
||||
File.read!("test/fixtures/mastodon-actor.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("id", "https://social.tcit.fr/users/tcit")
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{method: :get, url: "https://social.tcit.fr/users/tcit"}, _opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: actor_data}}
|
||||
end)
|
||||
|
||||
{:ok, %Activity{data: _, local: false}, _} = Transmogrifier.handle_incoming(follow_data)
|
||||
|
||||
data =
|
||||
File.read!("test/fixtures/mastodon-unfollow-activity.json")
|
||||
|> Jason.decode!()
|
||||
|> Map.put("object", follow_data)
|
||||
|
||||
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(data)
|
||||
|
||||
assert data["type"] == "Undo"
|
||||
assert data["object"]["type"] == "Follow"
|
||||
assert data["object"]["object"] == actor.url
|
||||
assert data["actor"] == "https://social.tcit.fr/users/tcit"
|
||||
|
||||
{:ok, followed} = Actors.get_actor_by_url(data["actor"])
|
||||
refute Actors.is_following(followed, actor)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user