From 72cd3e688d705e17c5dfed9a10c45156c7d502a0 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 17 Nov 2020 15:45:42 +0100 Subject: [PATCH] Add tests for metadata Signed-off-by: Thomas Citharel --- test/service/metadata/instance_test.exs | 24 ++++ test/service/metadata/metadata_test.exs | 143 ++++++++++++++++++++++++ test/service/metadata/utils_test.exs | 60 ++++++++++ 3 files changed, 227 insertions(+) create mode 100644 test/service/metadata/instance_test.exs create mode 100644 test/service/metadata/metadata_test.exs create mode 100644 test/service/metadata/utils_test.exs diff --git a/test/service/metadata/instance_test.exs b/test/service/metadata/instance_test.exs new file mode 100644 index 000000000..8e7033e80 --- /dev/null +++ b/test/service/metadata/instance_test.exs @@ -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}\n" + end + end +end diff --git a/test/service/metadata/metadata_test.exs b/test/service/metadata/metadata_test.exs new file mode 100644 index 000000000..478d2f0a5 --- /dev/null +++ b/test/service/metadata/metadata_test.exs @@ -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() == + "" + + assert group + |> Map.put(:avatar, nil) + |> Metadata.build_tags() + |> Metadata.Utils.stringify_tags() == + "" + 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() == + "#{event.title} - Mobilizon" + + assert event + |> Map.put(:picture, nil) + |> Metadata.build_tags() + |> Metadata.Utils.stringify_tags() == + "#{event.title} - Mobilizon" + 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() == + "" + 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() == + "" + 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 diff --git a/test/service/metadata/utils_test.exs b/test/service/metadata/utils_test.exs new file mode 100644 index 000000000..9ba6742ae --- /dev/null +++ b/test/service/metadata/utils_test.exs @@ -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("

This is my biography

") == "This is my biography" + end + + test "process_description/3 cuts after a limit" do + assert Utils.process_description("

This is my biography

", "fr", 10) == + "This is my…" + end + + test "process_description/3 cuts after the default limit" do + assert Utils.process_description( + "

Biography

It all started when someone wanted a very long string 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's difficult to invent things to write when you've got nothing to say. Anyway, what'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 = "" + tag_3 = Tag.tag(:meta, property: "og:url", content: "three") + + assert Utils.stringify_tags([tag_1, tag_2, tag_3]) == + "" + end + end + + describe "strip_tags/1" do + test "removes tags from string" do + assert Utils.strip_tags("

Hello

How are you

") == "Hello How are you" + end + end +end