debianize-mobilizon/test/service/metadata/metadata_test.exs
Thomas Citharel 72cd3e688d
Add tests for metadata
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2020-11-17 15:45:42 +01:00

144 lines
8.1 KiB
Elixir

defmodule Mobilizon.Service.MetadataTest do
alias Mobilizon.Actors.Actor
alias Mobilizon.Discussions.Comment
alias Mobilizon.Events.Event
alias Mobilizon.Posts.Post
alias Mobilizon.Service.Metadata
alias Mobilizon.Tombstone
use Mobilizon.DataCase
import Mobilizon.Factory
describe "build_tags/2 for an actor" do
test "that is a group gives tags" do
%Actor{} = group = insert(:group, name: "My group")
assert group |> Metadata.build_tags() |> Metadata.Utils.stringify_tags() ==
"<meta content=\"#{group.name} (@#{group.preferred_username})\" property=\"og:title\"><meta content=\"#{
group.url
}\" property=\"og:url\"><meta content=\"The event organizer didn&#39;t add any description.\" property=\"og:description\"><meta content=\"profile\" property=\"og:type\"><meta content=\"#{
group.preferred_username
}\" property=\"profile:username\"><meta content=\"summary\" property=\"twitter:card\"><meta content=\"#{
group.avatar.url
}\" property=\"og:image\"><script type=\"application/ld+json\">{\"@context\":\"http://schema.org\",\"@type\":\"Organization\",\"address\":null,\"name\":\"#{
group.name
}\",\"url\":\"#{group.url}\"}</script>"
assert group
|> Map.put(:avatar, nil)
|> Metadata.build_tags()
|> Metadata.Utils.stringify_tags() ==
"<meta content=\"#{group.name} (@#{group.preferred_username})\" property=\"og:title\"><meta content=\"#{
group.url
}\" property=\"og:url\"><meta content=\"The event organizer didn&#39;t add any description.\" property=\"og:description\"><meta content=\"profile\" property=\"og:type\"><meta content=\"#{
group.preferred_username
}\" property=\"profile:username\"><meta content=\"summary\" property=\"twitter:card\"><script type=\"application/ld+json\">{\"@context\":\"http://schema.org\",\"@type\":\"Organization\",\"address\":null,\"name\":\"#{
group.name
}\",\"url\":\"#{group.url}\"}</script>"
end
test "that is not a group doesn't give anything" do
%Actor{} = person = insert(:actor)
assert person |> Metadata.build_tags() |> Metadata.Utils.stringify_tags() == ""
assert person |> Metadata.build_tags("fr") |> Metadata.Utils.stringify_tags() == ""
end
end
describe "build_tags/2 for an event" do
test "gives tags" do
alias Mobilizon.Web.Endpoint
%Event{} = event = insert(:event)
# Because the description in Schema.org data is double-escaped
a = "\n"
b = "\\n"
assert event
|> Metadata.build_tags()
|> Metadata.Utils.stringify_tags() ==
"<title>#{event.title} - Mobilizon</title><meta content=\"#{event.description}\" name=\"description\"><meta content=\"#{
event.title
}\" property=\"og:title\"><meta content=\"#{event.url}\" property=\"og:url\"><meta content=\"#{
event.description
}\" property=\"og:description\"><meta content=\"website\" property=\"og:type\"><link href=\"#{
event.url
}\" rel=\"canonical\"><meta content=\"#{event.picture.file.url}\" property=\"og:image\"><meta content=\"summary_large_image\" property=\"twitter:card\"><script type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"Event\",\"description\":\"#{
String.replace(event.description, a, b)
}\",\"endDate\":\"#{DateTime.to_iso8601(event.ends_on)}\",\"eventStatus\":\"https://schema.org/EventScheduled\",\"image\":[\"#{
event.picture.file.url
}\"],\"location\":{\"@type\":\"Place\",\"address\":{\"@type\":\"PostalAddress\",\"addressCountry\":\"My Country\",\"addressLocality\":\"My Locality\",\"addressRegion\":\"My Region\",\"postalCode\":\"My Postal Code\",\"streetAddress\":\"My Street Address\"},\"name\":\"#{
event.physical_address.description
}\"},\"name\":\"#{event.title}\",\"organizer\":{\"@type\":\"Person\",\"name\":\"#{
event.organizer_actor.preferred_username
}\"},\"performer\":{\"@type\":\"Person\",\"name\":\"#{
event.organizer_actor.preferred_username
}\"},\"startDate\":\"#{DateTime.to_iso8601(event.begins_on)}\"}</script>"
assert event
|> Map.put(:picture, nil)
|> Metadata.build_tags()
|> Metadata.Utils.stringify_tags() ==
"<title>#{event.title} - Mobilizon</title><meta content=\"#{event.description}\" name=\"description\"><meta content=\"#{
event.title
}\" property=\"og:title\"><meta content=\"#{event.url}\" property=\"og:url\"><meta content=\"#{
event.description
}\" property=\"og:description\"><meta content=\"website\" property=\"og:type\"><link href=\"#{
event.url
}\" rel=\"canonical\"><meta content=\"summary_large_image\" property=\"twitter:card\"><script type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"Event\",\"description\":\"#{
String.replace(event.description, a, b)
}\",\"endDate\":\"#{DateTime.to_iso8601(event.ends_on)}\",\"eventStatus\":\"https://schema.org/EventScheduled\",\"image\":[\"#{
"#{Endpoint.url()}/img/mobilizon_default_card.png"
}\"],\"location\":{\"@type\":\"Place\",\"address\":{\"@type\":\"PostalAddress\",\"addressCountry\":\"My Country\",\"addressLocality\":\"My Locality\",\"addressRegion\":\"My Region\",\"postalCode\":\"My Postal Code\",\"streetAddress\":\"My Street Address\"},\"name\":\"#{
event.physical_address.description
}\"},\"name\":\"#{event.title}\",\"organizer\":{\"@type\":\"Person\",\"name\":\"#{
event.organizer_actor.preferred_username
}\"},\"performer\":{\"@type\":\"Person\",\"name\":\"#{
event.organizer_actor.preferred_username
}\"},\"startDate\":\"#{DateTime.to_iso8601(event.begins_on)}\"}</script>"
end
end
describe "build_tags/2 for a post" do
test "gives tags" do
%Post{} = post = insert(:post)
assert post
|> Metadata.build_tags()
|> Metadata.Utils.stringify_tags() ==
"<meta content=\"#{post.title}\" property=\"og:title\"><meta content=\"#{post.url}\" property=\"og:url\"><meta content=\"#{
Metadata.Utils.process_description(post.body)
}\" property=\"og:description\"><meta content=\"article\" property=\"og:type\"><meta content=\"summary\" property=\"twitter:card\"><link href=\"#{
post.url
}\" rel=\"canonical\"><meta content=\"summary_large_image\" property=\"twitter:card\"><script type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"Article\",\"author\":{\"@type\":\"Organization\",\"name\":\"#{
post.attributed_to.preferred_username
}\"},\"dateModified\":\"#{DateTime.to_iso8601(post.updated_at)}\",\"datePublished\":\"#{
DateTime.to_iso8601(post.publish_at)
}\",\"name\":\"My Awesome article\"}</script>"
end
end
describe "build_tags/2 for a comment" do
test "gives tags" do
%Comment{} = comment = insert(:comment)
assert comment
|> Metadata.build_tags()
|> Metadata.Utils.stringify_tags() ==
"<meta content=\"#{comment.actor.preferred_username}\" property=\"og:title\"><meta content=\"#{
comment.url
}\" property=\"og:url\"><meta content=\"#{comment.text}\" property=\"og:description\"><meta content=\"website\" property=\"og:type\"><meta content=\"summary\" property=\"twitter:card\">"
end
end
describe "build_tags/2 for a tombstone" do
test "gives nothing" do
%Tombstone{} = tombstone = insert(:tombstone)
assert tombstone
|> Metadata.build_tags()
|> Metadata.Utils.stringify_tags() == ""
end
end
end