From 4745a41393cddd9bbc5a14affa77595204488b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 10 Aug 2023 23:03:19 +0200 Subject: Allow to specify post language MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../transmogrifier/note_handling_test.exs | 111 +++++++++++++++++++++ .../web/activity_pub/transmogrifier_test.exs | 12 +++ test/pleroma/web/activity_pub/utils_test.exs | 34 +++++-- .../web/mastodon_api/views/status_view_test.exs | 10 ++ 4 files changed, 157 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs index a9ad3e9c8..8abc8a903 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs @@ -221,6 +221,36 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do "

@lain

" end + test "it only uses contentMap if content is not present" do + user = insert(:user) + + message = %{ + "@context" => "https://www.w3.org/ns/activitystreams", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "type" => "Create", + "object" => %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "id" => Utils.generate_object_id(), + "type" => "Note", + "content" => "Hi", + "contentMap" => %{ + "de" => "Hallo", + "uk" => "Привіт" + }, + "inReplyTo" => nil, + "attributedTo" => user.ap_id + }, + "actor" => user.ap_id + } + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message) + object = Object.normalize(data["object"], fetch: false) + + assert object.data["content"] == "Hi" + end + test "it works for incoming notices with to/cc not being an array (kroeg)" do data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!() @@ -358,6 +388,87 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do end end + test "it detects language from context" do + user = insert(:user) + + message = %{ + "@context" => ["https://www.w3.org/ns/activitystreams", %{"@language" => "pl"}], + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "type" => "Create", + "object" => %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "id" => Utils.generate_object_id(), + "type" => "Note", + "content" => "Szczęść Boże", + "attributedTo" => user.ap_id + }, + "actor" => user.ap_id + } + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message) + object = Object.normalize(data["object"], fetch: false) + + assert object.data["language"] == "pl" + end + + test "it detects language from contentMap" do + user = insert(:user) + + message = %{ + "@context" => "https://www.w3.org/ns/activitystreams", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "type" => "Create", + "object" => %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "id" => Utils.generate_object_id(), + "type" => "Note", + "content" => "Szczęść Boże", + "contentMap" => %{ + "de" => "Gott segne", + "pl" => "Szczęść Boże" + }, + "attributedTo" => user.ap_id + }, + "actor" => user.ap_id + } + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message) + object = Object.normalize(data["object"], fetch: false) + + assert object.data["language"] == "pl" + end + + test "it detects language from content" do + clear_config([Pleroma.Language.LanguageDetector, :provider], LanguageDetectorMock) + + user = insert(:user) + + message = %{ + "@context" => ["https://www.w3.org/ns/activitystreams"], + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "type" => "Create", + "object" => %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "id" => Utils.generate_object_id(), + "type" => "Note", + "content" => "Dieu vous bénisse, Fédivers.", + "attributedTo" => user.ap_id + }, + "actor" => user.ap_id + } + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message) + object = Object.normalize(data["object"], fetch: false) + + assert object.data["language"] == "fr" + end + describe "`handle_incoming/2`, Mastodon format `replies` handling" do setup do: clear_config([:activitypub, :note_replies_output_limit], 5) setup do: clear_config([:instance, :federation_incoming_replies_max_depth]) diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index 3e0c8dc65..a72edf79c 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -352,6 +352,18 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end + test "it adds contentMap if language is specified" do + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{status: "тест", language: "uk"}) + + {:ok, prepared} = Transmogrifier.prepare_outgoing(activity.data) + + assert prepared["object"]["contentMap"] == %{ + "uk" => "тест" + } + end + describe "actor rewriting" do test "it fixes the actor URL property to be a proper URI" do data = %{ diff --git a/test/pleroma/web/activity_pub/utils_test.exs b/test/pleroma/web/activity_pub/utils_test.exs index 3f93c872b..525bdb032 100644 --- a/test/pleroma/web/activity_pub/utils_test.exs +++ b/test/pleroma/web/activity_pub/utils_test.exs @@ -138,16 +138,30 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do end end - test "make_json_ld_header/0" do - assert Utils.make_json_ld_header() == %{ - "@context" => [ - "https://www.w3.org/ns/activitystreams", - "http://localhost:4001/schemas/litepub-0.1.jsonld", - %{ - "@language" => "und" - } - ] - } + describe "make_json_ld_header/1" do + test "makes jsonld header" do + assert Utils.make_json_ld_header() == %{ + "@context" => [ + "https://www.w3.org/ns/activitystreams", + "http://localhost:4001/schemas/litepub-0.1.jsonld", + %{ + "@language" => "und" + } + ] + } + end + + test "includes language if specified" do + assert Utils.make_json_ld_header(%{"language" => "pl"}) == %{ + "@context" => [ + "https://www.w3.org/ns/activitystreams", + "http://localhost:4001/schemas/litepub-0.1.jsonld", + %{ + "@language" => "pl" + } + ] + } + end end describe "get_existing_votes" do diff --git a/test/pleroma/web/mastodon_api/views/status_view_test.exs b/test/pleroma/web/mastodon_api/views/status_view_test.exs index b93335190..d6884ac2c 100644 --- a/test/pleroma/web/mastodon_api/views/status_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/status_view_test.exs @@ -770,6 +770,16 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do assert status.edited_at end + test "it shows post language" do + user = insert(:user) + + {:ok, post} = CommonAPI.post(user, %{status: "Szczęść Boże", language: "pl"}) + + status = StatusView.render("show.json", activity: post) + + assert status.language == "pl" + end + test "with a source object" do note = insert(:note, -- cgit v1.2.3 From 04c8f6b4d1e2a9a30f66b0ffb99d7a17a1510a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 11 Aug 2023 13:44:30 +0200 Subject: Add ObjectValidators.LanguageCode type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../object_validators/bare_uri_test.ex | 25 ------------------- .../object_validators/bare_uri_test.exs | 25 +++++++++++++++++++ .../object_validators/language_code.exs | 28 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 25 deletions(-) delete mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex create mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.exs create mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs (limited to 'test') diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex b/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex deleted file mode 100644 index 226383c3c..000000000 --- a/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex +++ /dev/null @@ -1,25 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2023 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.BareUriTest do - use Pleroma.DataCase, async: true - - alias Pleroma.EctoType.ActivityPub.ObjectValidators.BareUri - - test "diaspora://" do - text = "diaspora://alice@fediverse.example/post/deadbeefdeadbeefdeadbeefdeadbeef" - assert {:ok, text} = BareUri.cast(text) - end - - test "nostr:" do - text = "nostr:note1gwdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" - assert {:ok, text} = BareUri.cast(text) - end - - test "errors for non-URIs" do - assert :error == SafeText.cast(1) - assert :error == SafeText.cast("foo") - assert :error == SafeText.cast("foo bar") - end -end diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.exs b/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.exs new file mode 100644 index 000000000..760ecb465 --- /dev/null +++ b/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.exs @@ -0,0 +1,25 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.BareUriTest do + use Pleroma.DataCase, async: true + + alias Pleroma.EctoType.ActivityPub.ObjectValidators.BareUri + + test "diaspora://" do + text = "diaspora://alice@fediverse.example/post/deadbeefdeadbeefdeadbeefdeadbeef" + assert {:ok, ^text} = BareUri.cast(text) + end + + test "nostr:" do + text = "nostr:note1gwdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" + assert {:ok, ^text} = BareUri.cast(text) + end + + test "errors for non-URIs" do + assert :error == BareUri.cast(1) + assert :error == BareUri.cast("foo") + assert :error == BareUri.cast("foo bar") + end +end diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs b/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs new file mode 100644 index 000000000..2261cc209 --- /dev/null +++ b/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs @@ -0,0 +1,28 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCodeTest do + use Pleroma.DataCase, async: true + + alias Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode + + test "it accepts language code" do + text = "pl" + assert {:ok, ^text} = LanguageCode.cast(text) + end + + test "it accepts language code with region" do + text = "pl-PL" + assert {:ok, ^text} = LanguageCode.cast(text) + end + + test "errors for invalid language code" do + assert {:error, :invalid_language} = LanguageCode.cast("ru_RU") + assert {:error, :invalid_language} = LanguageCode.cast(" ") + end + + test "errors for non-text" do + assert :error == LanguageCode.cast(42) + end +end -- cgit v1.2.3 From 366559c5a33c30de181782418cd1b52f65d0ca5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 11 Aug 2023 14:59:58 +0200 Subject: Make status.language == nil for 'und' value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- test/pleroma/web/mastodon_api/views/status_view_test.exs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test') diff --git a/test/pleroma/web/mastodon_api/views/status_view_test.exs b/test/pleroma/web/mastodon_api/views/status_view_test.exs index d6884ac2c..8c53e6567 100644 --- a/test/pleroma/web/mastodon_api/views/status_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/status_view_test.exs @@ -780,6 +780,16 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do assert status.language == "pl" end + test "doesn't show post language if it's 'und'" do + user = insert(:user) + + {:ok, post} = CommonAPI.post(user, %{status: "sdifjogijodfg", language: "und"}) + + status = StatusView.render("show.json", activity: post) + + assert status.language == nil + end + test "with a source object" do note = insert(:note, -- cgit v1.2.3 From 69d53a62388270a9807cfe1f96f3819287bc477a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 11 Aug 2023 16:45:26 +0200 Subject: Rename test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../object_validators/language_code.exs | 28 ---------------------- .../object_validators/language_code_test.exs | 28 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 28 deletions(-) delete mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs create mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs (limited to 'test') diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs b/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs deleted file mode 100644 index 2261cc209..000000000 --- a/test/pleroma/ecto_type/activity_pub/object_validators/language_code.exs +++ /dev/null @@ -1,28 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2023 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCodeTest do - use Pleroma.DataCase, async: true - - alias Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode - - test "it accepts language code" do - text = "pl" - assert {:ok, ^text} = LanguageCode.cast(text) - end - - test "it accepts language code with region" do - text = "pl-PL" - assert {:ok, ^text} = LanguageCode.cast(text) - end - - test "errors for invalid language code" do - assert {:error, :invalid_language} = LanguageCode.cast("ru_RU") - assert {:error, :invalid_language} = LanguageCode.cast(" ") - end - - test "errors for non-text" do - assert :error == LanguageCode.cast(42) - end -end diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs b/test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs new file mode 100644 index 000000000..2261cc209 --- /dev/null +++ b/test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs @@ -0,0 +1,28 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCodeTest do + use Pleroma.DataCase, async: true + + alias Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode + + test "it accepts language code" do + text = "pl" + assert {:ok, ^text} = LanguageCode.cast(text) + end + + test "it accepts language code with region" do + text = "pl-PL" + assert {:ok, ^text} = LanguageCode.cast(text) + end + + test "errors for invalid language code" do + assert {:error, :invalid_language} = LanguageCode.cast("ru_RU") + assert {:error, :invalid_language} = LanguageCode.cast(" ") + end + + test "errors for non-text" do + assert :error == LanguageCode.cast(42) + end +end -- cgit v1.2.3 From 47ba7d346f5d5b45b5bbcecb7a549ee303ee8089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 11 Aug 2023 18:10:58 +0200 Subject: Remove test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../transmogrifier/note_handling_test.exs | 27 ---------------------- 1 file changed, 27 deletions(-) (limited to 'test') diff --git a/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs index 8abc8a903..70283ac5a 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs @@ -442,33 +442,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do assert object.data["language"] == "pl" end - test "it detects language from content" do - clear_config([Pleroma.Language.LanguageDetector, :provider], LanguageDetectorMock) - - user = insert(:user) - - message = %{ - "@context" => ["https://www.w3.org/ns/activitystreams"], - "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "cc" => [], - "type" => "Create", - "object" => %{ - "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "cc" => [], - "id" => Utils.generate_object_id(), - "type" => "Note", - "content" => "Dieu vous bénisse, Fédivers.", - "attributedTo" => user.ap_id - }, - "actor" => user.ap_id - } - - {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message) - object = Object.normalize(data["object"], fetch: false) - - assert object.data["language"] == "fr" - end - describe "`handle_incoming/2`, Mastodon format `replies` handling" do setup do: clear_config([:activitypub, :note_replies_output_limit], 5) setup do: clear_config([:instance, :federation_incoming_replies_max_depth]) -- cgit v1.2.3 From 62340b50b57eeab0b7ab4093e07d05080991bfc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 19 Aug 2023 19:03:25 +0200 Subject: Move maybe_add_content_map out of Transmogrifier, use code from tusooa's branch for MapOfString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../object_validators/map_of_string_test.exs | 55 +++++++++++++++++ .../article_note_page_validator_test.exs | 70 ++++++++++++++++++++++ .../transmogrifier/note_handling_test.exs | 54 ----------------- 3 files changed, 125 insertions(+), 54 deletions(-) create mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs (limited to 'test') diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs b/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs new file mode 100644 index 000000000..4ee179dc8 --- /dev/null +++ b/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs @@ -0,0 +1,55 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfStringTest do + alias Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfString + use Pleroma.DataCase, async: true + + test "it validates" do + data = %{ + "en-US" => "mew mew", + "en-GB" => "meow meow" + } + + assert {:ok, ^data} = MapOfString.cast(data) + end + + test "it validates empty strings" do + data = %{ + "en-US" => "mew mew", + "en-GB" => "" + } + + assert {:ok, ^data} = MapOfString.cast(data) + end + + test "it ignores non-strings within the map" do + data = %{ + "en-US" => "mew mew", + "en-GB" => 123 + } + + assert {:ok, validated_data} = MapOfString.cast(data) + + assert validated_data == %{"en-US" => "mew mew"} + end + + test "it ignores bad locale codes" do + data = %{ + "en-US" => "mew mew", + "en_GB" => "meow meow", + "en<<#@!$#!@%!GB" => "meow meow" + } + + assert {:ok, validated_data} = MapOfString.cast(data) + + assert validated_data == %{"en-US" => "mew mew"} + end + + test "it complains with non-map data" do + assert :error = MapOfString.cast("mew") + assert :error = MapOfString.cast(["mew"]) + assert :error = MapOfString.cast([%{"en-US" => "mew"}]) + 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 c7a62be18..a0c50b10d 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 @@ -116,4 +116,74 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note) end + + describe "Note language" do + test "it detects language from context" do + user = insert(:user) + + note_activity = %{ + "@context" => ["https://www.w3.org/ns/activitystreams", %{"@language" => "pl"}], + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "type" => "Create", + "object" => %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "id" => Utils.generate_object_id(), + "type" => "Note", + "content" => "Szczęść Boże", + "attributedTo" => user.ap_id + }, + "actor" => user.ap_id + } + + {:ok, object} = + ArticleNotePageValidator.cast_and_apply(note_activity["object"], + activity_data: note_activity + ) + + assert object.language == "pl" + end + + test "it detects language from contentMap" do + user = insert(:user) + + note = %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "id" => Utils.generate_object_id(), + "type" => "Note", + "content" => "Szczęść Boże", + "contentMap" => %{ + "de" => "Gott segne", + "pl" => "Szczęść Boże" + }, + "attributedTo" => user.ap_id + } + + {:ok, object} = ArticleNotePageValidator.cast_and_apply(note) + + assert object.language == "pl" + end + + test "it adds contentMap if language is specified" do + user = insert(:user) + + note = %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "cc" => [], + "id" => Utils.generate_object_id(), + "type" => "Note", + "content" => "тест", + "language" => "uk", + "attributedTo" => user.ap_id + } + + {:ok, object} = ArticleNotePageValidator.cast_and_apply(note) + + assert object.contentMap == %{ + "uk" => "тест" + } + end + end end diff --git a/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs index 70283ac5a..1e57ebabe 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/note_handling_test.exs @@ -388,60 +388,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do end end - test "it detects language from context" do - user = insert(:user) - - message = %{ - "@context" => ["https://www.w3.org/ns/activitystreams", %{"@language" => "pl"}], - "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "cc" => [], - "type" => "Create", - "object" => %{ - "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "cc" => [], - "id" => Utils.generate_object_id(), - "type" => "Note", - "content" => "Szczęść Boże", - "attributedTo" => user.ap_id - }, - "actor" => user.ap_id - } - - {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message) - object = Object.normalize(data["object"], fetch: false) - - assert object.data["language"] == "pl" - end - - test "it detects language from contentMap" do - user = insert(:user) - - message = %{ - "@context" => "https://www.w3.org/ns/activitystreams", - "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "cc" => [], - "type" => "Create", - "object" => %{ - "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "cc" => [], - "id" => Utils.generate_object_id(), - "type" => "Note", - "content" => "Szczęść Boże", - "contentMap" => %{ - "de" => "Gott segne", - "pl" => "Szczęść Boże" - }, - "attributedTo" => user.ap_id - }, - "actor" => user.ap_id - } - - {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message) - object = Object.normalize(data["object"], fetch: false) - - assert object.data["language"] == "pl" - end - describe "`handle_incoming/2`, Mastodon format `replies` handling" do setup do: clear_config([:activitypub, :note_replies_output_limit], 5) setup do: clear_config([:instance, :federation_incoming_replies_max_depth]) -- cgit v1.2.3 From c160ef7b6a4c8d214a7abbb5054993341ee66b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 19 Aug 2023 20:33:42 +0200 Subject: Remove test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../activity_pub/object_validators/map_of_string_test.exs | 5 +++-- test/pleroma/web/activity_pub/transmogrifier_test.exs | 12 ------------ 2 files changed, 3 insertions(+), 14 deletions(-) (limited to 'test') diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs b/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs index 4ee179dc8..941199ce8 100644 --- a/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs +++ b/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs @@ -1,11 +1,12 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2022 Pleroma Authors +# Copyright © 2017-2023 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfStringTest do - alias Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfString use Pleroma.DataCase, async: true + alias Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfString + test "it validates" do data = %{ "en-US" => "mew mew", diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index a72edf79c..3e0c8dc65 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -352,18 +352,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end - test "it adds contentMap if language is specified" do - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{status: "тест", language: "uk"}) - - {:ok, prepared} = Transmogrifier.prepare_outgoing(activity.data) - - assert prepared["object"]["contentMap"] == %{ - "uk" => "тест" - } - end - describe "actor rewriting" do test "it fixes the actor URL property to be a proper URI" do data = %{ -- cgit v1.2.3 From c5ed684273fa329bc955c59dbc7beed9804fb0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 7 Sep 2023 15:12:15 +0200 Subject: Rename MapOfString to ContentLanguageMap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../object_validators/map_of_string_test.exs | 56 ---------------------- 1 file changed, 56 deletions(-) delete mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs (limited to 'test') diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs b/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs deleted file mode 100644 index 941199ce8..000000000 --- a/test/pleroma/ecto_type/activity_pub/object_validators/map_of_string_test.exs +++ /dev/null @@ -1,56 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2023 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfStringTest do - use Pleroma.DataCase, async: true - - alias Pleroma.EctoType.ActivityPub.ObjectValidators.MapOfString - - test "it validates" do - data = %{ - "en-US" => "mew mew", - "en-GB" => "meow meow" - } - - assert {:ok, ^data} = MapOfString.cast(data) - end - - test "it validates empty strings" do - data = %{ - "en-US" => "mew mew", - "en-GB" => "" - } - - assert {:ok, ^data} = MapOfString.cast(data) - end - - test "it ignores non-strings within the map" do - data = %{ - "en-US" => "mew mew", - "en-GB" => 123 - } - - assert {:ok, validated_data} = MapOfString.cast(data) - - assert validated_data == %{"en-US" => "mew mew"} - end - - test "it ignores bad locale codes" do - data = %{ - "en-US" => "mew mew", - "en_GB" => "meow meow", - "en<<#@!$#!@%!GB" => "meow meow" - } - - assert {:ok, validated_data} = MapOfString.cast(data) - - assert validated_data == %{"en-US" => "mew mew"} - end - - test "it complains with non-map data" do - assert :error = MapOfString.cast("mew") - assert :error = MapOfString.cast(["mew"]) - assert :error = MapOfString.cast([%{"en-US" => "mew"}]) - end -end -- cgit v1.2.3 From a3b17dac0bf5da57cbd08335379ddfe4f8919bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 7 Sep 2023 15:14:18 +0200 Subject: Rename test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../content_language_map_test.exs | 56 ++++++++++++++++++++++ .../article_note_page_validator_test.exs | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/pleroma/ecto_type/activity_pub/object_validators/content_language_map_test.exs (limited to 'test') diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/content_language_map_test.exs b/test/pleroma/ecto_type/activity_pub/object_validators/content_language_map_test.exs new file mode 100644 index 000000000..a05871a6f --- /dev/null +++ b/test/pleroma/ecto_type/activity_pub/object_validators/content_language_map_test.exs @@ -0,0 +1,56 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.ContentLanguageMapTest do + use Pleroma.DataCase, async: true + + alias Pleroma.EctoType.ActivityPub.ObjectValidators.ContentLanguageMap + + test "it validates" do + data = %{ + "en-US" => "mew mew", + "en-GB" => "meow meow" + } + + assert {:ok, ^data} = ContentLanguageMap.cast(data) + end + + test "it validates empty strings" do + data = %{ + "en-US" => "mew mew", + "en-GB" => "" + } + + assert {:ok, ^data} = ContentLanguageMap.cast(data) + end + + test "it ignores non-strings within the map" do + data = %{ + "en-US" => "mew mew", + "en-GB" => 123 + } + + assert {:ok, validated_data} = ContentLanguageMap.cast(data) + + assert validated_data == %{"en-US" => "mew mew"} + end + + test "it ignores bad locale codes" do + data = %{ + "en-US" => "mew mew", + "en_GB" => "meow meow", + "en<<#@!$#!@%!GB" => "meow meow" + } + + assert {:ok, validated_data} = ContentLanguageMap.cast(data) + + assert validated_data == %{"en-US" => "mew mew"} + end + + test "it complains with non-map data" do + assert :error = ContentLanguageMap.cast("mew") + assert :error = ContentLanguageMap.cast(["mew"]) + assert :error = ContentLanguageMap.cast([%{"en-US" => "mew"}]) + 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 a0c50b10d..c4e2aa838 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 @@ -118,7 +118,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest end describe "Note language" do - test "it detects language from context" do + test "it detects language from JSON-LD context" do user = insert(:user) note_activity = %{ -- cgit v1.2.3 From 51aef6b78dcf709872de32a02533e943f08858d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 28 Dec 2023 15:52:59 +0100 Subject: Add language from activity context in ObjectValidator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../object_validators/article_note_page_validator_test.exs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'test') 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 25e29c878..611d0bcd0 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 @@ -186,12 +186,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest "actor" => user.ap_id } - {:ok, object} = - ArticleNotePageValidator.cast_and_apply(note_activity["object"], - activity_data: note_activity - ) + {:ok, _create_activity, meta} = ObjectValidator.validate(note_activity, []) |> IO.inspect() - assert object.language == "pl" + assert meta[:object_data]["language"] == "pl" end test "it detects language from contentMap" do -- cgit v1.2.3 From a6e066a77d5ce65b034cd62775614d5902d29d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 7 Mar 2024 14:05:45 +0100 Subject: Fix adding language to json ld header, add transmogrifier test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- test/pleroma/web/activity_pub/transmogrifier_test.exs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test') diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index a49e459a6..8fbcf15f1 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -384,6 +384,24 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert modified["object"]["quoteUrl"] == quote_id assert modified["object"]["quoteUri"] == quote_id end + + test "it adds language of the object to its json-ld context" do + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{status: "Cześć", language: "pl"}) + {:ok, modified} = Transmogrifier.prepare_outgoing(activity.object.data) + + assert [_, _, %{"@language" => "pl"}] = modified["@context"] + end + + test "it adds language of the object to Create activity json-ld context" do + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{status: "Cześć", language: "pl"}) + {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) + + assert [_, _, %{"@language" => "pl"}] = modified["@context"] + end end describe "actor rewriting" do -- cgit v1.2.3 From a40bf5d24fb75b246b9e11908b24cdcedabcb3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 28 Jul 2024 13:44:17 +0200 Subject: Fix good_locale_code?/1 regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../ecto_type/activity_pub/object_validators/language_code_test.exs | 1 + 1 file changed, 1 insertion(+) (limited to 'test') diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs b/test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs index 2261cc209..086bb3e97 100644 --- a/test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs +++ b/test/pleroma/ecto_type/activity_pub/object_validators/language_code_test.exs @@ -20,6 +20,7 @@ defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCodeTest do test "errors for invalid language code" do assert {:error, :invalid_language} = LanguageCode.cast("ru_RU") assert {:error, :invalid_language} = LanguageCode.cast(" ") + assert {:error, :invalid_language} = LanguageCode.cast("en-US\n") end test "errors for non-text" do -- cgit v1.2.3 From c94c6eac22663a46d8c2822953e3b8b959a3d1fb Mon Sep 17 00:00:00 2001 From: floatingghost Date: Mon, 5 Dec 2022 12:58:48 +0000 Subject: Remerge of hashtag following (#341) this time with less idiot Co-authored-by: FloatingGhost Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/341 Signed-off-by: mkljczk --- test/pleroma/user_test.exs | 70 ++++++++++++++++ .../pleroma/web/activity_pub/activity_pub_test.exs | 27 ++++++ .../controllers/tag_controller_test.exs | 97 ++++++++++++++++++++++ test/pleroma/web/streamer_test.exs | 30 +++++++ test/support/factory.ex | 7 ++ 5 files changed, 231 insertions(+) create mode 100644 test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs (limited to 'test') diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 06afc0709..4a3d6bacc 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -2919,4 +2919,74 @@ defmodule Pleroma.UserTest do assert [%{"verified_at" => ^verified_at}] = user.fields end + + describe "follow_hashtag/2" do + test "should follow a hashtag" do + user = insert(:user) + hashtag = insert(:hashtag) + + assert {:ok, _} = user |> User.follow_hashtag(hashtag) + + user = User.get_cached_by_ap_id(user.ap_id) + + assert user.followed_hashtags |> Enum.count() == 1 + assert hashtag.name in Enum.map(user.followed_hashtags, fn %{name: name} -> name end) + end + + test "should not follow a hashtag twice" do + user = insert(:user) + hashtag = insert(:hashtag) + + assert {:ok, _} = user |> User.follow_hashtag(hashtag) + + assert {:ok, _} = user |> User.follow_hashtag(hashtag) + + user = User.get_cached_by_ap_id(user.ap_id) + + assert user.followed_hashtags |> Enum.count() == 1 + assert hashtag.name in Enum.map(user.followed_hashtags, fn %{name: name} -> name end) + end + + test "can follow multiple hashtags" do + user = insert(:user) + hashtag = insert(:hashtag) + other_hashtag = insert(:hashtag) + + assert {:ok, _} = user |> User.follow_hashtag(hashtag) + assert {:ok, _} = user |> User.follow_hashtag(other_hashtag) + + user = User.get_cached_by_ap_id(user.ap_id) + + assert user.followed_hashtags |> Enum.count() == 2 + assert hashtag.name in Enum.map(user.followed_hashtags, fn %{name: name} -> name end) + assert other_hashtag.name in Enum.map(user.followed_hashtags, fn %{name: name} -> name end) + end + end + + describe "unfollow_hashtag/2" do + test "should unfollow a hashtag" do + user = insert(:user) + hashtag = insert(:hashtag) + + assert {:ok, _} = user |> User.follow_hashtag(hashtag) + assert {:ok, _} = user |> User.unfollow_hashtag(hashtag) + + user = User.get_cached_by_ap_id(user.ap_id) + + assert user.followed_hashtags |> Enum.count() == 0 + end + + test "should not error when trying to unfollow a hashtag twice" do + user = insert(:user) + hashtag = insert(:hashtag) + + assert {:ok, _} = user |> User.follow_hashtag(hashtag) + assert {:ok, _} = user |> User.unfollow_hashtag(hashtag) + assert {:ok, _} = user |> User.unfollow_hashtag(hashtag) + + user = User.get_cached_by_ap_id(user.ap_id) + + assert user.followed_hashtags |> Enum.count() == 0 + end + end end diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index 72222ae88..c7adf6bba 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -867,6 +867,33 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end end + describe "fetch activities for followed hashtags" do + test "it should return public activities that reference a given hashtag" do + hashtag = insert(:hashtag, name: "tenshi") + user = insert(:user) + other_user = insert(:user) + + {:ok, normally_visible} = + CommonAPI.post(other_user, %{status: "hello :)", visibility: "public"}) + + {:ok, public} = CommonAPI.post(user, %{status: "maji #tenshi", visibility: "public"}) + {:ok, _unrelated} = CommonAPI.post(user, %{status: "dai #tensh", visibility: "public"}) + {:ok, unlisted} = CommonAPI.post(user, %{status: "maji #tenshi", visibility: "unlisted"}) + {:ok, _private} = CommonAPI.post(user, %{status: "maji #tenshi", visibility: "private"}) + + activities = + ActivityPub.fetch_activities([other_user.follower_address], %{ + followed_hashtags: [hashtag.id] + }) + + assert length(activities) == 3 + normal_id = normally_visible.id + public_id = public.id + unlisted_id = unlisted.id + assert [%{id: ^normal_id}, %{id: ^public_id}, %{id: ^unlisted_id}] = activities + end + end + describe "fetch activities in context" do test "retrieves activities that have a given context" do {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"}) diff --git a/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs new file mode 100644 index 000000000..a1b73ad78 --- /dev/null +++ b/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs @@ -0,0 +1,97 @@ +defmodule Pleroma.Web.MastodonAPI.TagControllerTest do + use Pleroma.Web.ConnCase + + import Pleroma.Factory + import Tesla.Mock + + alias Pleroma.User + + setup do + mock(fn env -> apply(HttpRequestMock, :request, [env]) end) + :ok + end + + describe "GET /api/v1/tags/:id" do + test "returns 200 with tag" do + %{user: user, conn: conn} = oauth_access(["read"]) + + tag = insert(:hashtag, name: "jubjub") + {:ok, _user} = User.follow_hashtag(user, tag) + + response = + conn + |> get("/api/v1/tags/jubjub") + |> json_response_and_validate_schema(200) + + assert %{ + "name" => "jubjub", + "url" => "http://localhost:4001/tags/jubjub", + "history" => [], + "following" => true + } = response + end + + test "returns 404 with unknown tag" do + %{conn: conn} = oauth_access(["read"]) + + conn + |> get("/api/v1/tags/jubjub") + |> json_response_and_validate_schema(404) + end + end + + describe "POST /api/v1/tags/:id/follow" do + test "should follow a hashtag" do + %{user: user, conn: conn} = oauth_access(["write:follows"]) + hashtag = insert(:hashtag, name: "jubjub") + + response = + conn + |> post("/api/v1/tags/jubjub/follow") + |> json_response_and_validate_schema(200) + + assert response["following"] == true + user = User.get_cached_by_ap_id(user.ap_id) + assert User.following_hashtag?(user, hashtag) + end + + test "should 404 if hashtag doesn't exist" do + %{conn: conn} = oauth_access(["write:follows"]) + + response = + conn + |> post("/api/v1/tags/rubrub/follow") + |> json_response_and_validate_schema(404) + + assert response["error"] == "Hashtag not found" + end + end + + describe "POST /api/v1/tags/:id/unfollow" do + test "should unfollow a hashtag" do + %{user: user, conn: conn} = oauth_access(["write:follows"]) + hashtag = insert(:hashtag, name: "jubjub") + {:ok, user} = User.follow_hashtag(user, hashtag) + + response = + conn + |> post("/api/v1/tags/jubjub/unfollow") + |> json_response_and_validate_schema(200) + + assert response["following"] == false + user = User.get_cached_by_ap_id(user.ap_id) + refute User.following_hashtag?(user, hashtag) + end + + test "should 404 if hashtag doesn't exist" do + %{conn: conn} = oauth_access(["write:follows"]) + + response = + conn + |> post("/api/v1/tags/rubrub/unfollow") + |> json_response_and_validate_schema(404) + + assert response["error"] == "Hashtag not found" + end + end +end diff --git a/test/pleroma/web/streamer_test.exs b/test/pleroma/web/streamer_test.exs index 262ff11d2..85978e824 100644 --- a/test/pleroma/web/streamer_test.exs +++ b/test/pleroma/web/streamer_test.exs @@ -558,6 +558,36 @@ defmodule Pleroma.Web.StreamerTest do assert_receive {:render_with_user, _, "status_update.json", ^create, _} refute Streamer.filtered_by_user?(user, edited) end + + test "it streams posts containing followed hashtags on the 'user' stream", %{ + user: user, + token: oauth_token + } do + hashtag = insert(:hashtag, %{name: "tenshi"}) + other_user = insert(:user) + {:ok, user} = User.follow_hashtag(user, hashtag) + + Streamer.get_topic_and_add_socket("user", user, oauth_token) + {:ok, activity} = CommonAPI.post(other_user, %{status: "hey #tenshi"}) + + assert_receive {:render_with_user, _, "update.json", ^activity, _} + end + + test "should not stream private posts containing followed hashtags on the 'user' stream", %{ + user: user, + token: oauth_token + } do + hashtag = insert(:hashtag, %{name: "tenshi"}) + other_user = insert(:user) + {:ok, user} = User.follow_hashtag(user, hashtag) + + Streamer.get_topic_and_add_socket("user", user, oauth_token) + + {:ok, activity} = + CommonAPI.post(other_user, %{status: "hey #tenshi", visibility: "private"}) + + refute_receive {:render_with_user, _, "update.json", ^activity, _} + end end describe "public streams" do diff --git a/test/support/factory.ex b/test/support/factory.ex index 91e5805c8..88c4ed8e5 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -668,4 +668,11 @@ defmodule Pleroma.Factory do |> Map.merge(params) |> Pleroma.Announcement.add_rendered_properties() end + + def hashtag_factory(params \\ %{}) do + %Pleroma.Hashtag{ + name: "test #{sequence(:hashtag_name, & &1)}" + } + |> Map.merge(params) + end end -- cgit v1.2.3 From bdb9f888d731e9ac59fe17457eacc49d81c2a54c Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sat, 31 Dec 2022 18:05:21 +0000 Subject: Add /api/v1/followed_tags Signed-off-by: mkljczk --- .../controllers/tag_controller_test.exs | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'test') diff --git a/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs index a1b73ad78..71c8e7fc0 100644 --- a/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs @@ -94,4 +94,66 @@ defmodule Pleroma.Web.MastodonAPI.TagControllerTest do assert response["error"] == "Hashtag not found" end end + + describe "GET /api/v1/followed_tags" do + test "should list followed tags" do + %{user: user, conn: conn} = oauth_access(["read:follows"]) + + response = + conn + |> get("/api/v1/followed_tags") + |> json_response_and_validate_schema(200) + + assert Enum.empty?(response) + + hashtag = insert(:hashtag, name: "jubjub") + {:ok, _user} = User.follow_hashtag(user, hashtag) + + response = + conn + |> get("/api/v1/followed_tags") + |> json_response_and_validate_schema(200) + + assert [%{"name" => "jubjub"}] = response + end + + test "should include a link header to paginate" do + %{user: user, conn: conn} = oauth_access(["read:follows"]) + + for i <- 1..21 do + hashtag = insert(:hashtag, name: "jubjub#{i}}") + {:ok, _user} = User.follow_hashtag(user, hashtag) + end + + response = + conn + |> get("/api/v1/followed_tags") + + json = json_response_and_validate_schema(response, 200) + assert Enum.count(json) == 20 + assert [link_header] = get_resp_header(response, "link") + assert link_header =~ "rel=\"next\"" + next_link = extract_next_link_header(link_header) + + response = + conn + |> get(next_link) + |> json_response_and_validate_schema(200) + + assert Enum.count(response) == 1 + end + + test "should refuse access without read:follows scope" do + %{conn: conn} = oauth_access(["write"]) + + conn + |> get("/api/v1/followed_tags") + |> json_response_and_validate_schema(403) + end + end + + defp extract_next_link_header(header) do + [_, next_link] = Regex.run(~r{<(?.*)>; rel="next"}, header) + next_link + end end -- cgit v1.2.3 From aa74c87443230921aadf6022b13eb9e44a031d95 Mon Sep 17 00:00:00 2001 From: mkljczk Date: Mon, 30 Dec 2024 22:41:53 +0100 Subject: fix tests Signed-off-by: mkljczk --- test/mix/tasks/pleroma/database_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/mix/tasks/pleroma/database_test.exs b/test/mix/tasks/pleroma/database_test.exs index 96a925528..38ed096ae 100644 --- a/test/mix/tasks/pleroma/database_test.exs +++ b/test/mix/tasks/pleroma/database_test.exs @@ -411,7 +411,7 @@ defmodule Mix.Tasks.Pleroma.DatabaseTest do ["scheduled_activities"], ["schema_migrations"], ["thread_mutes"], - # ["user_follows_hashtag"], # not in pleroma + ["user_follows_hashtag"], # ["user_frontend_setting_profiles"], # not in pleroma ["user_invite_tokens"], ["user_notes"], -- cgit v1.2.3 From 120fbbc97e4430fb87749ca9271d318889dba7ff Mon Sep 17 00:00:00 2001 From: mkljczk Date: Mon, 17 Feb 2025 17:55:03 +0100 Subject: Include contentMap in outgoing posts Signed-off-by: mkljczk --- test/pleroma/web/activity_pub/transmogrifier_test.exs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test') diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index ebf70b3e6..a25c6fe1b 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -639,5 +639,14 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do processed = Transmogrifier.prepare_object(original) assert processed["formerRepresentations"] == original["formerRepresentations"] end + + test "it uses contentMap to specify post language" do + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{status: "Cześć", language: "pl"}) + {:ok, modified} = Transmogrifier.prepare_object(activity.object.data) + + assert %{"contentMap" => %{"pl" => "Cześć"}} = modified["object"] + end end end -- cgit v1.2.3 From ce4c07cc2b16d429eaabf324407e7aafd93843a9 Mon Sep 17 00:00:00 2001 From: mkljczk Date: Mon, 17 Feb 2025 19:21:08 +0100 Subject: update test Signed-off-by: mkljczk --- test/pleroma/web/activity_pub/transmogrifier_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index a25c6fe1b..fcb8d65d1 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -644,9 +644,9 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{status: "Cześć", language: "pl"}) - {:ok, modified} = Transmogrifier.prepare_object(activity.object.data) + object = Transmogrifier.prepare_object(activity.object.data) - assert %{"contentMap" => %{"pl" => "Cześć"}} = modified["object"] + assert %{"contentMap" => %{"pl" => "Cześć"}} = object end end end -- cgit v1.2.3