2018-12-27 10:24:04 +00:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/web/activity_pub/activity_pub_test.exs
|
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityPubTest do
|
2019-09-22 14:26:23 +00:00
|
|
|
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
2018-10-11 15:37:39 +00:00
|
|
|
use Mobilizon.DataCase
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
import Mock
|
2018-10-11 15:37:39 +00:00
|
|
|
import Mobilizon.Factory
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Events
|
2020-01-22 01:14:42 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.HTTPSignatures.Signature
|
2018-11-13 11:23:37 +00:00
|
|
|
|
2019-10-25 15:43:37 +00:00
|
|
|
@activity_pub_public_audience "https://www.w3.org/ns/activitystreams#Public"
|
2019-09-22 16:29:13 +00:00
|
|
|
|
2018-11-13 11:23:37 +00:00
|
|
|
setup_all do
|
|
|
|
HTTPoison.start()
|
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-12-07 09:47:31 +00:00
|
|
|
describe "setting HTTP signature" do
|
|
|
|
test "set http signature header" do
|
|
|
|
actor = insert(:actor)
|
|
|
|
|
|
|
|
signature =
|
2019-07-30 14:40:59 +00:00
|
|
|
Signature.sign(actor, %{
|
2018-12-07 09:47:31 +00:00
|
|
|
host: "example.com",
|
|
|
|
"content-length": 15,
|
2019-09-22 11:41:24 +00:00
|
|
|
digest: %{id: "my_id"} |> Jason.encode!() |> Signature.build_digest(),
|
2019-07-30 14:40:59 +00:00
|
|
|
"(request-target)": Signature.generate_request_target("POST", "/inbox"),
|
|
|
|
date: Signature.generate_date_header()
|
2018-12-07 09:47:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
assert signature =~ "headers=\"(request-target) content-length date digest host\""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-15 19:18:03 +00:00
|
|
|
describe "fetching actor from its url" do
|
2018-08-24 09:34:00 +00:00
|
|
|
test "returns an actor from nickname" do
|
2018-11-13 11:23:37 +00:00
|
|
|
use_cassette "activity_pub/fetch_tcit@framapiaf.org" do
|
|
|
|
assert {:ok, %Actor{preferred_username: "tcit", domain: "framapiaf.org"} = actor} =
|
|
|
|
ActivityPub.make_actor_from_nickname("tcit@framapiaf.org")
|
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
2018-08-24 09:34:00 +00:00
|
|
|
|
|
|
|
test "returns an actor from url" do
|
2018-11-13 11:23:37 +00:00
|
|
|
use_cassette "activity_pub/fetch_framapiaf.org_users_tcit" do
|
|
|
|
assert {:ok, %Actor{preferred_username: "tcit", domain: "framapiaf.org"}} =
|
2019-10-25 15:43:37 +00:00
|
|
|
ActivityPub.get_or_fetch_actor_by_url("https://framapiaf.org/users/tcit")
|
2018-11-13 11:23:37 +00:00
|
|
|
end
|
2018-08-24 09:34:00 +00:00
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "create activities" do
|
2019-07-30 14:40:59 +00:00
|
|
|
# test "removes doubled 'to' recipients" do
|
|
|
|
# actor = insert(:actor)
|
|
|
|
#
|
|
|
|
# {:ok, activity, _} =
|
|
|
|
# ActivityPub.create(%{
|
|
|
|
# to: ["user1", "user1", "user2"],
|
|
|
|
# actor: actor,
|
|
|
|
# context: "",
|
|
|
|
# object: %{}
|
|
|
|
# })
|
|
|
|
#
|
|
|
|
# assert activity.data["to"] == ["user1", "user2"]
|
|
|
|
# assert activity.actor == actor.url
|
|
|
|
# assert activity.recipients == ["user1", "user2"]
|
|
|
|
# end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2018-08-24 09:34:00 +00:00
|
|
|
describe "fetching an" do
|
2018-11-12 17:17:53 +00:00
|
|
|
test "object by url" do
|
2019-05-22 12:12:11 +00:00
|
|
|
use_cassette "activity_pub/fetch_framapiaf_framasoft_status" do
|
2018-11-13 11:23:37 +00:00
|
|
|
{:ok, object} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
2019-05-22 12:12:11 +00:00
|
|
|
"https://framapiaf.org/users/Framasoft/statuses/102093631881522097"
|
2018-11-13 11:23:37 +00:00
|
|
|
)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-11-13 11:23:37 +00:00
|
|
|
{:ok, object_again} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
2019-05-22 12:12:11 +00:00
|
|
|
"https://framapiaf.org/users/Framasoft/statuses/102093631881522097"
|
2018-11-13 11:23:37 +00:00
|
|
|
)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
assert object.id == object_again.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "object reply by url" do
|
2019-05-22 12:12:11 +00:00
|
|
|
use_cassette "activity_pub/fetch_framasoft_framapiaf_reply" do
|
2018-12-14 16:41:55 +00:00
|
|
|
{:ok, object} =
|
2019-05-22 12:12:11 +00:00
|
|
|
ActivityPub.fetch_object_from_url("https://mamot.fr/@imacrea/102094441327423790")
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
assert object.in_reply_to_comment.url ==
|
2019-05-22 12:12:11 +00:00
|
|
|
"https://framapiaf.org/users/Framasoft/statuses/102093632302210150"
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "object reply to a video by url" do
|
|
|
|
use_cassette "activity_pub/fetch_reply_to_framatube" do
|
|
|
|
{:ok, object} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
2019-05-22 12:12:11 +00:00
|
|
|
"https://diaspodon.fr/users/dada/statuses/100820008426311925"
|
2018-12-14 16:41:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert object.in_reply_to_comment == nil
|
2018-11-13 11:23:37 +00:00
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "deletion" do
|
|
|
|
test "it creates a delete activity and deletes the original event" do
|
|
|
|
event = insert(:event)
|
2019-09-16 00:07:44 +00:00
|
|
|
event = Events.get_public_event_by_url_with_preload!(event.url)
|
2019-07-30 08:35:29 +00:00
|
|
|
{:ok, delete, _} = ActivityPub.delete(event)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
assert delete.data["type"] == "Delete"
|
2018-06-14 15:25:55 +00:00
|
|
|
assert delete.data["actor"] == event.organizer_actor.url
|
2018-05-17 09:32:23 +00:00
|
|
|
assert delete.data["object"] == event.url
|
|
|
|
|
2018-08-24 09:34:00 +00:00
|
|
|
assert Events.get_event_by_url(event.url) == nil
|
|
|
|
end
|
|
|
|
|
2019-09-09 07:31:08 +00:00
|
|
|
test "it deletes the original event but only locally if needed" do
|
|
|
|
with_mock ActivityPub.Utils,
|
|
|
|
maybe_federate: fn _ -> :ok end,
|
|
|
|
lazy_put_activity_defaults: fn args -> args end do
|
|
|
|
event = insert(:event)
|
2019-09-21 21:59:07 +00:00
|
|
|
event = Events.get_public_event_by_url_with_preload!(event.url)
|
2019-09-09 07:31:08 +00:00
|
|
|
{:ok, delete, _} = ActivityPub.delete(event, false)
|
|
|
|
|
|
|
|
assert delete.data["type"] == "Delete"
|
|
|
|
assert delete.data["actor"] == event.organizer_actor.url
|
|
|
|
assert delete.data["object"] == event.url
|
|
|
|
assert delete.local == false
|
|
|
|
|
|
|
|
assert Events.get_event_by_url(event.url) == nil
|
|
|
|
|
|
|
|
assert_called(ActivityPub.Utils.maybe_federate(delete))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-24 09:34:00 +00:00
|
|
|
test "it creates a delete activity and deletes the original comment" do
|
|
|
|
comment = insert(:comment)
|
2019-09-16 00:07:44 +00:00
|
|
|
comment = Events.get_comment_from_url_with_preload!(comment.url)
|
2019-11-15 17:36:47 +00:00
|
|
|
assert is_nil(Events.get_comment_from_url(comment.url).deleted_at)
|
2019-07-30 08:35:29 +00:00
|
|
|
{:ok, delete, _} = ActivityPub.delete(comment)
|
2018-08-24 09:34:00 +00:00
|
|
|
|
|
|
|
assert delete.data["type"] == "Delete"
|
|
|
|
assert delete.data["actor"] == comment.actor.url
|
|
|
|
assert delete.data["object"] == comment.url
|
|
|
|
|
2019-11-15 17:36:47 +00:00
|
|
|
refute is_nil(Events.get_comment_from_url(comment.url).deleted_at)
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update" do
|
2019-09-04 16:24:31 +00:00
|
|
|
@updated_actor_summary "This is an updated actor"
|
|
|
|
|
2018-06-14 15:25:55 +00:00
|
|
|
test "it creates an update activity with the new actor data" do
|
|
|
|
actor = insert(:actor)
|
2019-10-25 15:43:37 +00:00
|
|
|
actor_data = %{summary: @updated_actor_summary}
|
|
|
|
|
|
|
|
{:ok, update, _} = ActivityPub.update(:actor, actor, actor_data, false)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-06-14 15:25:55 +00:00
|
|
|
assert update.data["actor"] == actor.url
|
2019-10-25 15:43:37 +00:00
|
|
|
assert update.data["to"] == [@activity_pub_public_audience]
|
|
|
|
assert update.data["object"]["id"] == actor.url
|
2019-12-03 10:29:51 +00:00
|
|
|
assert update.data["object"]["type"] == :Person
|
2019-09-04 16:24:31 +00:00
|
|
|
assert update.data["object"]["summary"] == @updated_actor_summary
|
|
|
|
end
|
|
|
|
|
|
|
|
@updated_start_time DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
|
|
|
|
test "it creates an update activity with the new event data" do
|
|
|
|
actor = insert(:actor)
|
|
|
|
event = insert(:event, organizer_actor: actor)
|
2019-10-25 15:43:37 +00:00
|
|
|
event_data = %{begins_on: @updated_start_time}
|
2019-09-04 16:24:31 +00:00
|
|
|
|
2019-10-25 15:43:37 +00:00
|
|
|
{:ok, update, _} = ActivityPub.update(:event, event, event_data)
|
2019-09-04 16:24:31 +00:00
|
|
|
|
2019-10-25 15:43:37 +00:00
|
|
|
assert update.data["actor"] == actor.url
|
|
|
|
assert update.data["to"] == [@activity_pub_public_audience]
|
|
|
|
assert update.data["object"]["id"] == event.url
|
|
|
|
assert update.data["object"]["type"] == "Event"
|
|
|
|
assert update.data["object"]["startTime"] == DateTime.to_iso8601(@updated_start_time)
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|