From ff5793198ff9068c49de12e4dfb229b9de8d38f8 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Wed, 11 Jan 2023 15:42:13 +0000 Subject: [PATCH] add inbound language test --- .../article_note_page_validator.ex | 3 +- .../fixtures/mastodon/note_with_language.json | 38 +++++++++++++++++++ test/pleroma/iso639_test.exs | 11 ++++++ .../article_note_page_validator_test.exs | 8 ++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/mastodon/note_with_language.json create mode 100644 test/pleroma/iso639_test.exs diff --git a/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex b/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex index 056c48f86..09b68c977 100644 --- a/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex @@ -30,7 +30,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do field(:replies, {:array, ObjectValidators.ObjectID}, default: []) field(:source, :map) - field(:content_map, :map) + field(:contentMap, :map) end def cast_and_apply(data) do @@ -155,6 +155,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do |> Enum.reject(fn {lang, _content} -> !Pleroma.ISO639.valid_alpha2?(lang) end) + |> Enum.into(%{}) Map.put(object, "contentMap", content_map) end diff --git a/test/fixtures/mastodon/note_with_language.json b/test/fixtures/mastodon/note_with_language.json new file mode 100644 index 000000000..04266708f --- /dev/null +++ b/test/fixtures/mastodon/note_with_language.json @@ -0,0 +1,38 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + { + "ostatus": "http://ostatus.org#", + "atomUri": "ostatus:atomUri", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "sensitive": "as:sensitive", + "toot": "http://joinmastodon.org/ns#", + "votersCount": "toot:votersCount" + } + ], + "id": "https://mastodon.social/users/akkoma_ap_integration_tester/statuses/109671288784583764", + "type": "Note", + "summary": null, + "inReplyTo": null, + "published": "2023-01-11T15:31:01Z", + "url": "https://mastodon.social/@akkoma_ap_integration_tester/109671288784583764", + "attributedTo": "https://mastodon.social/users/akkoma_ap_integration_tester", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://mastodon.social/users/akkoma_ap_integration_tester/followers" + ], + "sensitive": false, + "atomUri": "https://mastodon.social/users/akkoma_ap_integration_tester/statuses/109671288784583764", + "inReplyToAtomUri": null, + "conversation": "tag:mastodon.social,2023-01-11:objectId=376794415:objectType=Conversation", + "content": "

tag

", + "contentMap": { + "ja": "

tag

" + }, + "attachment": [], + "tag": [], + "replies": [] +} diff --git a/test/pleroma/iso639_test.exs b/test/pleroma/iso639_test.exs new file mode 100644 index 000000000..f05635b8c --- /dev/null +++ b/test/pleroma/iso639_test.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.ISO639Test do + use Pleroma.DataCase + + describe "ISO639 validation" do + test "should validate a language" do + assert Pleroma.ISO639.valid_alpha2?("en") + assert Pleroma.ISO639.valid_alpha2?("ja") + refute Pleroma.ISO639.valid_alpha2?("xx") + end + end +end diff --git a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs index 5b95ebc51..ade2256c3 100644 --- a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs @@ -39,6 +39,14 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note) end + test "a note with a language validates" do + user = insert(:user, %{ap_id: "https://mastodon.social/users/akkoma_ap_integration_tester"}) + note = File.read!("test/fixtures/mastodon/note_with_language.json") |> Jason.decode!() + %{valid?: true, changes: %{ contentMap: %{ + "ja" => "

tag

", + }}} = ArticleNotePageValidator.cast_and_validate(note) + end + test "a note from factory validates" do note = insert(:note) %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note.data)