Add tests for metadata

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-11-17 15:45:42 +01:00
parent 15a82c7bce
commit 72cd3e688d
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 227 additions and 0 deletions

View file

@ -0,0 +1,24 @@
defmodule Mobilizon.Service.Metadata.InstanceTest do
alias Mobilizon.Config
alias Mobilizon.Service.Metadata.{Instance, Utils}
alias Mobilizon.Web.Endpoint
use Mobilizon.DataCase
describe "build_tags/0 for the instance" do
test "gives tags" do
title = "#{Config.instance_name()} - Mobilizon"
description = Utils.process_description(Config.instance_description())
assert Instance.build_tags() |> Utils.stringify_tags() ==
"<title>#{title}</title><meta content=\"#{description}\" name=\"description\"><meta content=\"#{
title
}\" property=\"og:title\"><meta content=\"#{Endpoint.url()}\" property=\"og:url\"><meta content=\"#{
description
}\" property=\"og:description\"><meta content=\"website\" property=\"og:type\"><script type=\"application/ld+json\">{\n\"@context\": \"http://schema.org\",\n\"@type\": \"WebSite\",\n\"name\": \"#{
title
}\",\n\"url\": \"#{Endpoint.url()}\",\n\"potentialAction\": {\n\"@type\": \"SearchAction\",\n\"target\": \"#{
Endpoint.url()
}/search?term={search_term}\",\n\"query-input\": \"required name=search_term\"\n}\n}</script>\n"
end
end
end

View file

@ -0,0 +1,143 @@
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

View file

@ -0,0 +1,60 @@
defmodule Mobilizon.Service.Metadata.UtilsTest do
alias Mobilizon.Service.Metadata.Utils
use Mobilizon.DataCase
describe "process_description/3" do
test "process_description/3 strip tags" do
assert Utils.process_description("<p>This is my biography</p>") == "This is my biography"
end
test "process_description/3 cuts after a limit" do
assert Utils.process_description("<p>This is my biography</p>", "fr", 10) ==
"This is my…"
end
test "process_description/3 cuts after the default limit" do
assert Utils.process_description(
"<h1>Biography</h1><p>It all started when someone wanted a <b>very long string</b> to be cut. However it's difficult to invent things to write when you've got nothing to say. Anyway, what's the deal here. We just need to reach 200 characters.",
"fr"
) ==
"Biography It all started when someone wanted a very long string to be cut. However it&#39;s difficult to invent things to write when you&#39;ve got nothing to say. Anyway, what&#39;s the deal here. We…"
end
test "process_description/3 returns default if no description is provided" do
assert Utils.process_description(nil) ==
"The event organizer didn't add any description."
assert Utils.process_description("", "en") ==
"The event organizer didn't add any description."
end
end
describe "default_description/1" do
test "returns default description with a correct locale" do
assert Utils.default_description("en") == "The event organizer didn't add any description."
end
test "returns default description with no locale provided" do
assert Utils.default_description() == "The event organizer didn't add any description."
end
end
describe "stringify_tags/1" do
test "converts tags to string" do
alias Phoenix.HTML.Tag
tag_1 = Tag.tag(:meta, property: "og:url", content: "one")
tag_2 = "<meta content=\"two\" property=\"og:url\">"
tag_3 = Tag.tag(:meta, property: "og:url", content: "three")
assert Utils.stringify_tags([tag_1, tag_2, tag_3]) ==
"<meta content=\"one\" property=\"og:url\"><meta content=\"two\" property=\"og:url\"><meta content=\"three\" property=\"og:url\">"
end
end
describe "strip_tags/1" do
test "removes tags from string" do
assert Utils.strip_tags("<h1>Hello</h1><p>How are you</p>") == "Hello How are you"
end
end
end