2020-01-22 01:14:42 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.UtilsTest do
|
2018-12-14 16:41:55 +00:00
|
|
|
use Mobilizon.DataCase
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
import Mobilizon.Factory
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2021-08-10 18:32:50 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Utils
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
describe "make" do
|
|
|
|
test "comment data from struct" do
|
|
|
|
comment = insert(:comment)
|
2019-10-25 15:43:37 +00:00
|
|
|
tag = insert(:tag, title: "MyTag")
|
2020-07-09 15:24:28 +00:00
|
|
|
reply = insert(:comment, in_reply_to_comment: comment, tags: [tag], attributed_to: nil)
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
assert %{
|
|
|
|
"type" => "Note",
|
|
|
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
2019-10-25 15:43:37 +00:00
|
|
|
"cc" => [],
|
|
|
|
"tag" => [
|
|
|
|
%{
|
|
|
|
"href" => "http://mobilizon.test/tags/#{tag.slug}",
|
|
|
|
"name" => "#MyTag",
|
|
|
|
"type" => "Hashtag"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"content" => "My Comment",
|
2018-12-14 16:41:55 +00:00
|
|
|
"actor" => reply.actor.url,
|
|
|
|
"uuid" => reply.uuid,
|
2019-04-25 17:05:05 +00:00
|
|
|
"id" => Routes.page_url(Endpoint, :comment, reply.uuid),
|
2018-12-14 16:41:55 +00:00
|
|
|
"inReplyTo" => comment.url,
|
2019-12-03 10:29:51 +00:00
|
|
|
"attributedTo" => reply.actor.url,
|
2020-08-14 09:32:23 +00:00
|
|
|
"mediaType" => "text/html",
|
2021-06-03 16:58:47 +00:00
|
|
|
"published" => reply.published_at |> DateTime.to_iso8601(),
|
|
|
|
"isAnnouncement" => false
|
2019-09-22 16:29:13 +00:00
|
|
|
} == Converter.Comment.model_to_as(reply)
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "comment data from map" do
|
2020-07-09 15:24:28 +00:00
|
|
|
comment = insert(:comment, attributed_to: nil)
|
|
|
|
reply = insert(:comment, in_reply_to_comment: comment, attributed_to: nil)
|
2018-12-14 16:41:55 +00:00
|
|
|
to = ["https://www.w3.org/ns/activitystreams#Public"]
|
2019-12-03 10:29:51 +00:00
|
|
|
comment_data = Converter.Comment.model_to_as(reply)
|
2018-12-14 16:41:55 +00:00
|
|
|
assert comment_data["type"] == "Note"
|
|
|
|
assert comment_data["to"] == to
|
|
|
|
assert comment_data["content"] == reply.text
|
|
|
|
assert comment_data["actor"] == reply.actor.url
|
|
|
|
assert comment_data["inReplyTo"] == comment.url
|
|
|
|
end
|
|
|
|
end
|
2021-08-10 18:32:50 +00:00
|
|
|
|
|
|
|
describe "origin_check?" do
|
|
|
|
test "origin_check? with a tombstone" do
|
|
|
|
assert Utils.origin_check?("http://an_uri", %{
|
|
|
|
"type" => "Tombstone",
|
|
|
|
"id" => "http://an_uri"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2021-11-24 15:14:09 +00:00
|
|
|
|
|
|
|
describe "get_actor/1" do
|
|
|
|
test "with a string" do
|
|
|
|
assert Utils.get_actor(%{"actor" => "https://somewhere.tld/@someone"}) ==
|
|
|
|
"https://somewhere.tld/@someone"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with an object" do
|
|
|
|
assert Utils.get_actor(%{
|
|
|
|
"actor" => %{"id" => "https://somewhere.tld/@someone", "type" => "Person"}
|
|
|
|
}) ==
|
|
|
|
"https://somewhere.tld/@someone"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with an invalid object" do
|
|
|
|
assert_raise ArgumentError,
|
|
|
|
"Object contains an actor object with invalid type: \"Else\"",
|
|
|
|
fn ->
|
|
|
|
Utils.get_actor(%{
|
|
|
|
"actor" => %{"id" => "https://somewhere.tld/@someone", "type" => "Else"}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list" do
|
|
|
|
assert Utils.get_actor(%{
|
|
|
|
"actor" => ["https://somewhere.tld/@someone", "https://somewhere.else/@other"]
|
|
|
|
}) ==
|
|
|
|
"https://somewhere.tld/@someone"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of objects" do
|
|
|
|
assert Utils.get_actor(%{
|
|
|
|
"actor" => [
|
|
|
|
%{"type" => "Person", "id" => "https://somewhere.tld/@someone"},
|
|
|
|
"https://somewhere.else/@other"
|
|
|
|
]
|
|
|
|
}) ==
|
|
|
|
"https://somewhere.tld/@someone"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of objects containing an invalid one" do
|
|
|
|
assert Utils.get_actor(%{
|
|
|
|
"actor" => [
|
|
|
|
%{"type" => "Else", "id" => "https://somewhere.tld/@someone"},
|
|
|
|
"https://somewhere.else/@other"
|
|
|
|
]
|
|
|
|
}) ==
|
|
|
|
"https://somewhere.else/@other"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with an empty list" do
|
|
|
|
assert_raise ArgumentError,
|
|
|
|
"Object contains not actor information",
|
|
|
|
fn ->
|
|
|
|
Utils.get_actor(%{
|
|
|
|
"actor" => []
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fallbacks to attributed_to" do
|
|
|
|
assert Utils.get_actor(%{
|
|
|
|
"actor" => nil,
|
|
|
|
"attributedTo" => "https://somewhere.tld/@someone"
|
|
|
|
}) == "https://somewhere.tld/@someone"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with no actor information" do
|
|
|
|
assert_raise ArgumentError,
|
|
|
|
"Object contains both actor and attributedTo fields being null",
|
|
|
|
fn ->
|
|
|
|
Utils.get_actor(%{
|
|
|
|
"actor" => nil,
|
|
|
|
"attributedTo" => nil
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|