2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Service.Activitypub.ActivitypubTest do
|
|
|
|
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
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Actors
|
2018-12-07 10:47:31 +01:00
|
|
|
alias Mobilizon.Service.HTTPSignatures
|
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 =
|
|
|
|
HTTPSignatures.sign(actor, %{
|
|
|
|
host: "example.com",
|
|
|
|
"content-length": 15,
|
|
|
|
digest: Jason.encode!(%{id: "my_id"}) |> HTTPSignatures.build_digest(),
|
|
|
|
"(request-target)": HTTPSignatures.generate_request_target("POST", "/inbox"),
|
|
|
|
date: HTTPSignatures.generate_date_header()
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
test "removes doubled 'to' recipients" do
|
2018-06-14 17:25:55 +02:00
|
|
|
actor = insert(:actor)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
ActivityPub.create(%{
|
|
|
|
to: ["user1", "user1", "user2"],
|
2018-06-14 17:25:55 +02:00
|
|
|
actor: actor,
|
2018-05-17 11:32:23 +02:00
|
|
|
context: "",
|
|
|
|
object: %{}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert activity.data["to"] == ["user1", "user2"]
|
2018-06-14 17:25:55 +02:00
|
|
|
assert activity.actor == actor.url
|
2018-05-17 11:32:23 +02:00
|
|
|
assert activity.recipients == ["user1", "user2"]
|
|
|
|
end
|
|
|
|
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
|
2018-11-13 12:23:37 +01:00
|
|
|
use_cassette "activity_pub/fetch_social_tcit_fr_status" do
|
|
|
|
{:ok, object} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
|
|
|
"https://social.tcit.fr/users/tcit/statuses/99908779444618462"
|
|
|
|
)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-11-13 12:23:37 +01:00
|
|
|
{:ok, object_again} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
|
|
|
"https://social.tcit.fr/users/tcit/statuses/99908779444618462"
|
|
|
|
)
|
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
|
|
|
|
use_cassette "activity_pub/fetch_social_tcit_fr_reply" do
|
|
|
|
{:ok, object} =
|
|
|
|
ActivityPub.fetch_object_from_url(
|
|
|
|
"https://social.tcit.fr/users/tcit/statuses/101160654038714030"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert object.in_reply_to_comment.url ==
|
|
|
|
"https://social.tcit.fr/users/tcit/statuses/101160195754333819"
|
|
|
|
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(
|
|
|
|
"https://framapiaf.org/@troisiemelobe/101156292125317651"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
test "it creates a delete activity and deletes the original event" do
|
|
|
|
event = insert(:event)
|
|
|
|
event = Events.get_event_full_by_url!(event.url)
|
|
|
|
{:ok, delete} = ActivityPub.delete(event)
|
|
|
|
|
|
|
|
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)
|
|
|
|
{:ok, delete} = ActivityPub.delete(comment)
|
|
|
|
|
|
|
|
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
|
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})
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
{:ok, update} =
|
|
|
|
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"]
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|