2018-12-27 11:24:04 +01: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
|
|
|
|
|
|
|
|
defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do
|
2018-10-11 17:37:39 +02:00
|
|
|
use Mobilizon.DataCase
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
import Mobilizon.Factory
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events
|
2019-09-04 18:24:31 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Actors
|
2019-07-30 16:40:59 +02:00
|
|
|
alias Mobilizon.Service.HTTPSignatures.Signature
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-11-13 12:23:37 +01:00
|
|
|
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
|
|
|
|
|
|
|
setup_all do
|
|
|
|
HTTPoison.start()
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-12-07 10:47:31 +01:00
|
|
|
describe "setting HTTP signature" do
|
|
|
|
test "set http signature header" do
|
|
|
|
actor = insert(:actor)
|
|
|
|
|
|
|
|
signature =
|
2019-07-30 16:40:59 +02:00
|
|
|
Signature.sign(actor, %{
|
2018-12-07 10:47:31 +01:00
|
|
|
host: "example.com",
|
|
|
|
"content-length": 15,
|
2019-07-30 16:40:59 +02:00
|
|
|
digest: Jason.encode!(%{id: "my_id"}) |> Signature.build_digest(),
|
|
|
|
"(request-target)": Signature.generate_request_target("POST", "/inbox"),
|
|
|
|
date: Signature.generate_date_header()
|
2018-12-07 10:47:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
assert signature =~ "headers=\"(request-target) content-length date digest host\""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
describe "fetching actor from it's url" do
|
2018-08-24 11:34:00 +02:00
|
|
|
test "returns an actor from nickname" do
|
2018-11-13 12:23:37 +01: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 11:32:23 +02:00
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
test "returns an actor from url" do
|
2018-11-13 12:23:37 +01:00
|
|
|
use_cassette "activity_pub/fetch_framapiaf.org_users_tcit" do
|
|
|
|
assert {:ok, %Actor{preferred_username: "tcit", domain: "framapiaf.org"}} =
|
|
|
|
Actors.get_or_fetch_by_url("https://framapiaf.org/users/tcit")
|
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "create activities" do
|
2019-07-30 16:40:59 +02: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 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
describe "fetching an" do
|
2018-11-12 18:17:53 +01:00
|
|
|
test "object by url" do
|
2019-05-22 14:12:11 +02:00
|
|
|
use_cassette "activity_pub/fetch_framapiaf_framasoft_status" do
|
2018-11-13 12:23:37 +01:00
|
|
|
{:ok, object} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
2019-05-22 14:12:11 +02:00
|
|
|
"https://framapiaf.org/users/Framasoft/statuses/102093631881522097"
|
2018-11-13 12:23:37 +01:00
|
|
|
)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-11-13 12:23:37 +01:00
|
|
|
{:ok, object_again} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
2019-05-22 14:12:11 +02:00
|
|
|
"https://framapiaf.org/users/Framasoft/statuses/102093631881522097"
|
2018-11-13 12:23:37 +01:00
|
|
|
)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
assert object.id == object_again.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "object reply by url" do
|
2019-05-22 14:12:11 +02:00
|
|
|
use_cassette "activity_pub/fetch_framasoft_framapiaf_reply" do
|
2018-12-14 17:41:55 +01:00
|
|
|
{:ok, object} =
|
2019-05-22 14:12:11 +02:00
|
|
|
ActivityPub.fetch_object_from_url("https://mamot.fr/@imacrea/102094441327423790")
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
|
|
assert object.in_reply_to_comment.url ==
|
2019-05-22 14:12:11 +02:00
|
|
|
"https://framapiaf.org/users/Framasoft/statuses/102093632302210150"
|
2018-12-14 17:41:55 +01: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 14:12:11 +02:00
|
|
|
"https://diaspodon.fr/users/dada/statuses/100820008426311925"
|
2018-12-14 17:41:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert object.in_reply_to_comment == nil
|
2018-11-13 12:23:37 +01:00
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "deletion" do
|
2019-07-30 16:40:59 +02:00
|
|
|
# TODO: The delete activity it relayed and fetched once again (and then not found /o\)
|
2018-05-17 11:32:23 +02:00
|
|
|
test "it creates a delete activity and deletes the original event" do
|
|
|
|
event = insert(:event)
|
|
|
|
event = Events.get_event_full_by_url!(event.url)
|
2019-07-30 10:35:29 +02:00
|
|
|
{:ok, delete, _} = ActivityPub.delete(event)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
assert delete.data["type"] == "Delete"
|
2018-06-14 17:25:55 +02:00
|
|
|
assert delete.data["actor"] == event.organizer_actor.url
|
2018-05-17 11:32:23 +02:00
|
|
|
assert delete.data["object"] == event.url
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
assert Events.get_event_by_url(event.url) == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it creates a delete activity and deletes the original comment" do
|
|
|
|
comment = insert(:comment)
|
|
|
|
comment = Events.get_comment_full_from_url!(comment.url)
|
2019-07-30 10:35:29 +02:00
|
|
|
{:ok, delete, _} = ActivityPub.delete(comment)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
assert delete.data["type"] == "Delete"
|
|
|
|
assert delete.data["actor"] == comment.actor.url
|
|
|
|
assert delete.data["object"] == comment.url
|
|
|
|
|
|
|
|
assert Events.get_comment_from_url(comment.url) == nil
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "update" do
|
2019-09-04 18:24:31 +02:00
|
|
|
@updated_actor_summary "This is an updated actor"
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
test "it creates an update activity with the new actor data" do
|
|
|
|
actor = insert(:actor)
|
2018-10-11 17:37:39 +02:00
|
|
|
actor_data = MobilizonWeb.ActivityPub.ActorView.render("actor.json", %{actor: actor})
|
2019-09-04 18:24:31 +02:00
|
|
|
actor_data = Map.put(actor_data, "summary", @updated_actor_summary)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2019-09-04 18:24:31 +02:00
|
|
|
{:ok, update, updated_actor} =
|
2018-05-17 11:32:23 +02:00
|
|
|
ActivityPub.update(%{
|
2018-06-14 17:25:55 +02:00
|
|
|
actor: actor_data["url"],
|
|
|
|
to: [actor.url <> "/followers"],
|
2018-05-17 11:32:23 +02:00
|
|
|
cc: [],
|
2018-06-14 17:25:55 +02:00
|
|
|
object: actor_data
|
2018-05-17 11:32:23 +02:00
|
|
|
})
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
assert update.data["actor"] == actor.url
|
|
|
|
assert update.data["to"] == [actor.url <> "/followers"]
|
|
|
|
assert update.data["object"]["id"] == actor_data["id"]
|
|
|
|
assert update.data["object"]["type"] == actor_data["type"]
|
2019-09-04 18:24:31 +02:00
|
|
|
assert update.data["object"]["summary"] == @updated_actor_summary
|
|
|
|
|
|
|
|
refute updated_actor.summary == actor.summary
|
|
|
|
|
|
|
|
{:ok, %Actor{} = database_actor} = Mobilizon.Actors.get_actor_by_url(actor.url)
|
|
|
|
assert database_actor.summary == @updated_actor_summary
|
|
|
|
assert database_actor.preferred_username == actor.preferred_username
|
|
|
|
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)
|
|
|
|
event_data = Mobilizon.Service.ActivityPub.Converters.Event.model_to_as(event)
|
|
|
|
event_data = Map.put(event_data, "startTime", @updated_start_time)
|
|
|
|
|
|
|
|
{:ok, update, updated_event} =
|
|
|
|
ActivityPub.update(%{
|
|
|
|
actor: actor.url,
|
|
|
|
to: [actor.url <> "/followers"],
|
|
|
|
cc: [],
|
|
|
|
object: event_data
|
|
|
|
})
|
|
|
|
|
|
|
|
assert update.data["actor"] == actor.url
|
|
|
|
assert update.data["to"] == [actor.url <> "/followers"]
|
|
|
|
assert update.data["object"]["id"] == event_data["id"]
|
|
|
|
assert update.data["object"]["type"] == event_data["type"]
|
|
|
|
assert update.data["object"]["startTime"] == @updated_start_time
|
|
|
|
|
|
|
|
refute updated_event.begins_on == event.begins_on
|
|
|
|
|
|
|
|
%Event{} = database_event = Mobilizon.Events.get_event_by_url(event.url)
|
|
|
|
assert database_event.begins_on == @updated_start_time
|
|
|
|
assert database_event.title == event.title
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|