From cf96c4005743c61d44e17c9d37c6427eaf69c152 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 22 Jan 2020 21:10:17 +0300 Subject: [#1505] Added Mastodon-compatible `replies` collection to Note federated representation. --- test/web/activity_pub/transmogrifier_test.exs | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'test') diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 5da358c43..418b8a1ca 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -2027,4 +2027,51 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do } end end + + describe "set_replies/1" do + clear_config([:mastodon_compatibility, :federated_note_replies_limit]) do + Pleroma.Config.put([:mastodon_compatibility, :federated_note_replies_limit], 2) + end + + test "returns unmodified object if activity doesn't have self-replies" do + data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json")) + assert Transmogrifier.set_replies(data) == data + end + + test "sets `replies` collection with a limited number of self-replies" do + [user, another_user] = insert_list(2, :user) + + {:ok, %{id: id1} = activity} = CommonAPI.post(user, %{"status" => "1"}) + + {:ok, %{id: id2} = self_reply1} = + CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => id1}) + + {:ok, self_reply2} = + CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1}) + + # Assuming to _not_ be present in `replies` due to :federated_note_replies_limit is set to 2 + {:ok, _} = + CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1}) + + {:ok, _} = + CommonAPI.post(user, %{ + "status" => "self-reply to self-reply", + "in_reply_to_status_id" => id2 + }) + + {:ok, _} = + CommonAPI.post(another_user, %{ + "status" => "another user's reply", + "in_reply_to_status_id" => id1 + }) + + object = Object.normalize(activity) + replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.data["id"] end) + + assert %{ + "type" => "Collection", + "first" => %{"type" => "Collection", "items" => ^replies_uris} + } = Transmogrifier.set_replies(object.data)["replies"] + end + end end -- cgit v1.2.3 From 86e4d23acb640efea8cbc879ddbeadfa0e04f9c8 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Sat, 25 Jan 2020 10:47:30 +0300 Subject: [#1505] Background fetching of incoming activities' `replies` collections. --- test/support/oban_helpers.ex | 4 +++ test/web/activity_pub/transmogrifier_test.exs | 48 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) (limited to 'test') diff --git a/test/support/oban_helpers.ex b/test/support/oban_helpers.ex index 72792c064..0e3b654df 100644 --- a/test/support/oban_helpers.ex +++ b/test/support/oban_helpers.ex @@ -9,6 +9,10 @@ defmodule Pleroma.Tests.ObanHelpers do alias Pleroma.Repo + def wipe_all do + Repo.delete_all(Oban.Job) + end + def perform_all do Oban.Job |> Repo.all() diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 418b8a1ca..0fefb60da 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -3,7 +3,9 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do + use Oban.Testing, repo: Pleroma.Repo use Pleroma.DataCase + alias Pleroma.Activity alias Pleroma.Object alias Pleroma.Object.Fetcher @@ -1329,6 +1331,52 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end + describe "handle_incoming:`replies` handling" do + setup do + data = + File.read!("test/fixtures/mastodon-post-activity.json") + |> Poison.decode!() + + items = ["https://shitposter.club/notice/2827873", "https://shitposter.club/notice/7387606"] + collection = %{"items" => items} + %{data: data, items: items, collection: collection} + end + + test "it schedules background fetching of wrapped `replies` collection items", %{ + data: data, + items: items, + collection: collection + } do + replies = %{"first" => collection} + + object = Map.put(data["object"], "replies", replies) + data = Map.put(data, "object", object) + {:ok, _activity} = Transmogrifier.handle_incoming(data) + + for id <- items do + job_args = %{"op" => "fetch_remote", "id" => id} + assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args) + end + end + + test "it schedules background fetching of unwrapped `replies` collection items", %{ + data: data, + items: items, + collection: collection + } do + replies = collection + + object = Map.put(data["object"], "replies", replies) + data = Map.put(data, "object", object) + {:ok, _activity} = Transmogrifier.handle_incoming(data) + + for id <- items do + job_args = %{"op" => "fetch_remote", "id" => id} + assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args) + end + end + end + describe "prepare outgoing" do test "it inlines private announced objects" do user = insert(:user) -- cgit v1.2.3 From d458f4fdcafe847a7db8b1c663cfd945019816b7 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Sat, 8 Feb 2020 19:58:02 +0300 Subject: [#1505] Added tests, changelog entry, tweaked config settings related to replies output on outgoing federation. --- test/web/activity_pub/transmogrifier_test.exs | 10 +++++----- test/web/activity_pub/views/object_view_test.exs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 194b314a3..729594ded 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -1350,7 +1350,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end - describe "handle_incoming:`replies` handling" do + describe "`replies` handling in handle_incoming/2" do setup do data = File.read!("test/fixtures/mastodon-post-activity.json") @@ -1361,7 +1361,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do %{data: data, items: items, collection: collection} end - test "it schedules background fetching of wrapped `replies` collection items", %{ + test "with wrapped `replies` collection, it schedules background fetching of items", %{ data: data, items: items, collection: collection @@ -2096,8 +2096,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end describe "set_replies/1" do - clear_config([:mastodon_compatibility, :federated_note_replies_limit]) do - Pleroma.Config.put([:mastodon_compatibility, :federated_note_replies_limit], 2) + clear_config([:activitypub, :note_replies_output_limit]) do + Pleroma.Config.put([:activitypub, :note_replies_output_limit], 2) end test "returns unmodified object if activity doesn't have self-replies" do @@ -2116,7 +2116,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do {:ok, self_reply2} = CommonAPI.post(user, %{"status" => "self-reply 2", "in_reply_to_status_id" => id1}) - # Assuming to _not_ be present in `replies` due to :federated_note_replies_limit is set to 2 + # Assuming to _not_ be present in `replies` due to :note_replies_output_limit is set to 2 {:ok, _} = CommonAPI.post(user, %{"status" => "self-reply 3", "in_reply_to_status_id" => id1}) diff --git a/test/web/activity_pub/views/object_view_test.exs b/test/web/activity_pub/views/object_view_test.exs index 13447dc29..6784788cc 100644 --- a/test/web/activity_pub/views/object_view_test.exs +++ b/test/web/activity_pub/views/object_view_test.exs @@ -36,6 +36,28 @@ defmodule Pleroma.Web.ActivityPub.ObjectViewTest do assert result["@context"] end + describe "note activity's `replies` collection rendering" do + clear_config([:activitypub, :note_replies_output_limit]) do + Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5) + end + + test "renders `replies` collection for a note activity" do + user = insert(:user) + activity = insert(:note_activity, user: user) + + {:ok, self_reply1} = + CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => activity.id}) + + result = ObjectView.render("object.json", %{object: refresh_record(activity)}) + replies_uris = [self_reply1.data["id"]] + + assert %{ + "type" => "Collection", + "first" => %{"type" => "Collection", "items" => ^replies_uris} + } = get_in(result, ["object", "replies"]) + end + end + test "renders a like activity" do note = insert(:note_activity) object = Object.normalize(note) -- cgit v1.2.3 From 7c3991f59eccc47551257dfe41817e71d0eb6717 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Sun, 9 Feb 2020 10:17:21 +0300 Subject: [#1505] Fixed `replies` serialization (included objects' ids instead of activities' ids). --- test/web/activity_pub/transmogrifier_test.exs | 2 +- test/web/activity_pub/views/object_view_test.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 3720dda2a..d373762ea 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -2133,7 +2133,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do }) object = Object.normalize(activity) - replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.data["id"] end) + replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end) assert %{ "type" => "Collection", diff --git a/test/web/activity_pub/views/object_view_test.exs b/test/web/activity_pub/views/object_view_test.exs index 6784788cc..a9197b0c5 100644 --- a/test/web/activity_pub/views/object_view_test.exs +++ b/test/web/activity_pub/views/object_view_test.exs @@ -48,8 +48,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectViewTest do {:ok, self_reply1} = CommonAPI.post(user, %{"status" => "self-reply 1", "in_reply_to_status_id" => activity.id}) + replies_uris = [self_reply1.object.data["id"]] result = ObjectView.render("object.json", %{object: refresh_record(activity)}) - replies_uris = [self_reply1.data["id"]] assert %{ "type" => "Collection", -- cgit v1.2.3 From 24e49d14f287b0daf8c2977f2228be09139e4bf3 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Sun, 9 Feb 2020 17:34:48 +0300 Subject: [#1505] Removed wrapping of reply URIs into `first` element, added comments to transmogrifier tests. --- test/web/activity_pub/transmogrifier_test.exs | 8 ++++---- test/web/activity_pub/views/object_view_test.exs | 6 ++---- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index d373762ea..7d9828d38 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -1361,6 +1361,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do %{data: data, items: items, collection: collection} end + # Mastodon wraps reply URIs in `replies->first->items` test "with wrapped `replies` collection, it schedules background fetching of items", %{ data: data, items: items, @@ -1378,6 +1379,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end + # Pleroma outputs reply URIs as `replies->items` test "it schedules background fetching of unwrapped `replies` collection items", %{ data: data, items: items, @@ -2135,10 +2137,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do object = Object.normalize(activity) replies_uris = Enum.map([self_reply1, self_reply2], fn a -> a.object.data["id"] end) - assert %{ - "type" => "Collection", - "first" => %{"type" => "Collection", "items" => ^replies_uris} - } = Transmogrifier.set_replies(object.data)["replies"] + assert %{"type" => "Collection", "items" => ^replies_uris} = + Transmogrifier.set_replies(object.data)["replies"] end end end diff --git a/test/web/activity_pub/views/object_view_test.exs b/test/web/activity_pub/views/object_view_test.exs index a9197b0c5..acc855b98 100644 --- a/test/web/activity_pub/views/object_view_test.exs +++ b/test/web/activity_pub/views/object_view_test.exs @@ -51,10 +51,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectViewTest do replies_uris = [self_reply1.object.data["id"]] result = ObjectView.render("object.json", %{object: refresh_record(activity)}) - assert %{ - "type" => "Collection", - "first" => %{"type" => "Collection", "items" => ^replies_uris} - } = get_in(result, ["object", "replies"]) + assert %{"type" => "Collection", "items" => ^replies_uris} = + get_in(result, ["object", "replies"]) end end -- cgit v1.2.3 From b95dd5e217e7e1477b53deb9992b65f20b5649ac Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Mon, 10 Feb 2020 11:46:16 +0300 Subject: [#1505] Improved replies-handling tests: updated Mastodon message fixture, used exact Pleroma federation message. --- test/fixtures/mastodon-post-activity.json | 13 ++++++ test/web/activity_pub/transmogrifier_test.exs | 57 ++++++++++++++------------- 2 files changed, 42 insertions(+), 28 deletions(-) (limited to 'test') diff --git a/test/fixtures/mastodon-post-activity.json b/test/fixtures/mastodon-post-activity.json index b91263431..5c3d22722 100644 --- a/test/fixtures/mastodon-post-activity.json +++ b/test/fixtures/mastodon-post-activity.json @@ -35,6 +35,19 @@ "inReplyTo": null, "inReplyToAtomUri": null, "published": "2018-02-12T14:08:20Z", + "replies": { + "id": "http://mastodon.example.org/users/admin/statuses/99512778738411822/replies", + "type": "Collection", + "first": { + "type": "CollectionPage", + "next": "http://mastodon.example.org/users/admin/statuses/99512778738411822/replies?min_id=99512778738411824&page=true", + "partOf": "http://mastodon.example.org/users/admin/statuses/99512778738411822/replies", + "items": [ + "http://mastodon.example.org/users/admin/statuses/99512778738411823", + "http://mastodon.example.org/users/admin/statuses/99512778738411824" + ] + } + }, "sensitive": true, "summary": "cw", "tag": [ diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 7d9828d38..bb68809b0 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -1350,27 +1350,20 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end - describe "`replies` handling in handle_incoming/2" do - setup do + describe "handle_incoming/2: `replies` handling:" do + clear_config([:activitypub, :note_replies_output_limit]) do + Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5) + end + + test "with Mastodon-formatted `replies` collection, it schedules background fetching of items" do data = - File.read!("test/fixtures/mastodon-post-activity.json") + "test/fixtures/mastodon-post-activity.json" + |> File.read!() |> Poison.decode!() - items = ["https://shitposter.club/notice/2827873", "https://shitposter.club/notice/7387606"] - collection = %{"items" => items} - %{data: data, items: items, collection: collection} - end + items = get_in(data, ["object", "replies", "first", "items"]) + assert length(items) > 0 - # Mastodon wraps reply URIs in `replies->first->items` - test "with wrapped `replies` collection, it schedules background fetching of items", %{ - data: data, - items: items, - collection: collection - } do - replies = %{"first" => collection} - - object = Map.put(data["object"], "replies", replies) - data = Map.put(data, "object", object) {:ok, _activity} = Transmogrifier.handle_incoming(data) for id <- items do @@ -1379,19 +1372,27 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end - # Pleroma outputs reply URIs as `replies->items` - test "it schedules background fetching of unwrapped `replies` collection items", %{ - data: data, - items: items, - collection: collection - } do - replies = collection + test "with Pleroma-formatted `replies` collection, it schedules background fetching of items" do + user = insert(:user) - object = Map.put(data["object"], "replies", replies) - data = Map.put(data, "object", object) - {:ok, _activity} = Transmogrifier.handle_incoming(data) + {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"}) - for id <- items do + {:ok, reply1} = + CommonAPI.post(user, %{"status" => "reply1", "in_reply_to_status_id" => activity.id}) + + {:ok, reply2} = + CommonAPI.post(user, %{"status" => "reply2", "in_reply_to_status_id" => activity.id}) + + replies_uris = Enum.map([reply1, reply2], fn a -> a.object.data["id"] end) + + {:ok, federation_output} = Transmogrifier.prepare_outgoing(activity.data) + + Repo.delete(activity.object) + Repo.delete(activity) + + {:ok, _activity} = Transmogrifier.handle_incoming(federation_output) + + for id <- replies_uris do job_args = %{"op" => "fetch_remote", "id" => id} assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args) end -- cgit v1.2.3 From 269d592181bff8601f6545b85158ee1c222ff20d Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Sat, 15 Feb 2020 20:41:38 +0300 Subject: [#1505] Restricted max thread distance for fetching replies on incoming federation (in addition to reply-to depth restriction). --- test/object/fetcher_test.exs | 25 +++++++++++ test/web/activity_pub/transmogrifier_test.exs | 62 +++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/object/fetcher_test.exs b/test/object/fetcher_test.exs index 2aad7a588..3afd35648 100644 --- a/test/object/fetcher_test.exs +++ b/test/object/fetcher_test.exs @@ -26,6 +26,31 @@ defmodule Pleroma.Object.FetcherTest do :ok end + describe "max thread distance restriction" do + @ap_id "http://mastodon.example.org/@admin/99541947525187367" + + clear_config([:instance, :federation_incoming_replies_max_depth]) + + test "it returns thread depth exceeded error if thread depth is exceeded" do + Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0) + + assert {:error, "Max thread distance exceeded."} = + Fetcher.fetch_object_from_id(@ap_id, depth: 1) + end + + test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do + Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0) + + assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id) + end + + test "it fetches object if requested depth does not exceed max thread depth" do + Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10) + + assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10) + end + end + describe "actor origin containment" do test "it rejects objects with a bogus origin" do {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json") diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index bb68809b0..937f78cbe 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -42,7 +42,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end @tag capture_log: true - test "it fetches replied-to activities if we don't have them" do + test "it fetches reply-to activities if we don't have them" do data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() @@ -63,7 +63,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert returned_object.data["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873" end - test "it does not fetch replied-to activities beyond max_replies_depth" do + test "it does not fetch reply-to activities beyond max replies depth limit" do data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() @@ -75,7 +75,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do data = Map.put(data, "object", object) with_mock Pleroma.Web.Federator, - allowed_incoming_reply_depth?: fn _ -> false end do + allowed_thread_distance?: fn _ -> false end do {:ok, returned_activity} = Transmogrifier.handle_incoming(data) returned_object = Object.normalize(returned_activity, false) @@ -1350,12 +1350,14 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end end - describe "handle_incoming/2: `replies` handling:" do + describe "`handle_incoming/2`, Mastodon format `replies` handling" do clear_config([:activitypub, :note_replies_output_limit]) do Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5) end - test "with Mastodon-formatted `replies` collection, it schedules background fetching of items" do + clear_config([:instance, :federation_incoming_replies_max_depth]) + + setup do data = "test/fixtures/mastodon-post-activity.json" |> File.read!() @@ -1364,15 +1366,41 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do items = get_in(data, ["object", "replies", "first", "items"]) assert length(items) > 0 + %{data: data, items: items} + end + + test "schedules background fetching of `replies` items if max thread depth limit allows", %{ + data: data, + items: items + } do + Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10) + {:ok, _activity} = Transmogrifier.handle_incoming(data) for id <- items do - job_args = %{"op" => "fetch_remote", "id" => id} + job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1} assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args) end end - test "with Pleroma-formatted `replies` collection, it schedules background fetching of items" do + test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows", + %{data: data} do + Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0) + + {:ok, _activity} = Transmogrifier.handle_incoming(data) + + assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == [] + end + end + + describe "`handle_incoming/2`, Pleroma format `replies` handling" do + clear_config([:activitypub, :note_replies_output_limit]) do + Pleroma.Config.put([:activitypub, :note_replies_output_limit], 5) + end + + clear_config([:instance, :federation_incoming_replies_max_depth]) + + setup do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "post1"}) @@ -1390,13 +1418,31 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do Repo.delete(activity.object) Repo.delete(activity) + %{federation_output: federation_output, replies_uris: replies_uris} + end + + test "schedules background fetching of `replies` items if max thread depth limit allows", %{ + federation_output: federation_output, + replies_uris: replies_uris + } do + Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1) + {:ok, _activity} = Transmogrifier.handle_incoming(federation_output) for id <- replies_uris do - job_args = %{"op" => "fetch_remote", "id" => id} + job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1} assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args) end end + + test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows", + %{federation_output: federation_output} do + Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0) + + {:ok, _activity} = Transmogrifier.handle_incoming(federation_output) + + assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == [] + end end describe "prepare outgoing" do -- cgit v1.2.3 From ee57663d3480de77f35fbc2d475a137f1d592f35 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Mon, 24 Feb 2020 01:27:16 +0100 Subject: captcha_test.exs: Use the same testing logic in Kocaptcha and native --- test/captcha_test.exs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/captcha_test.exs b/test/captcha_test.exs index 393c8219e..b6f231bdb 100644 --- a/test/captcha_test.exs +++ b/test/captcha_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors +# Copyright © 2017-2020 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.CaptchaTest do @@ -31,17 +31,18 @@ defmodule Pleroma.CaptchaTest do test "new and validate" do new = Kocaptcha.new() - assert new[:type] == :kocaptcha - assert new[:token] == "afa1815e14e29355e6c8f6b143a39fa2" - assert new[:url] == - "https://captcha.kotobank.ch/captchas/afa1815e14e29355e6c8f6b143a39fa2.png" + token = "afa1815e14e29355e6c8f6b143a39fa2" + url = "https://captcha.kotobank.ch/captchas/afa1815e14e29355e6c8f6b143a39fa2.png" - assert Kocaptcha.validate( - new[:token], - "7oEy8c", - new[:answer_data] - ) == :ok + assert %{ + answer_data: answer, + token: ^token, + url: ^url, + type: :kocaptcha + } = new + + assert Kocaptcha.validate(token, "7oEy8c", answer) == :ok end end -- cgit v1.2.3 From f9fe6a9e30c49374d3f7cd8414ed2fd8585970cd Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Mon, 24 Feb 2020 02:41:48 +0100 Subject: Captcha: return invalid when answer_data is nil --- test/captcha_test.exs | 53 +++++++++++++++++++++++++++++++++++++++++++- test/support/captcha_mock.ex | 15 ++++++++++--- 2 files changed, 64 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/captcha_test.exs b/test/captcha_test.exs index b6f231bdb..5e29b48b0 100644 --- a/test/captcha_test.exs +++ b/test/captcha_test.exs @@ -3,15 +3,18 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.CaptchaTest do - use ExUnit.Case + use Pleroma.DataCase import Tesla.Mock + alias Pleroma.Captcha alias Pleroma.Captcha.Kocaptcha alias Pleroma.Captcha.Native @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}] + clear_config([Pleroma.Captcha, :enabled]) + describe "Kocaptcha" do setup do ets_name = Kocaptcha.Ets @@ -62,4 +65,52 @@ defmodule Pleroma.CaptchaTest do assert {:error, "Invalid CAPTCHA"} == Native.validate(token, answer, answer <> "foobar") end end + + describe "Captcha Wrapper" do + test "validate" do + Pleroma.Config.put([Pleroma.Captcha, :enabled], true) + + new = Captcha.new() + + assert %{ + answer_data: answer, + token: token + } = new + + assert is_binary(answer) + assert :ok = Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer) + end + + test "doesn't validate invalid answer" do + Pleroma.Config.put([Pleroma.Captcha, :enabled], true) + + new = Captcha.new() + + assert %{ + answer_data: answer, + token: token + } = new + + assert is_binary(answer) + + assert {:error, "Invalid answer data"} = + Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", answer <> "foobar") + end + + test "nil answer_data" do + Pleroma.Config.put([Pleroma.Captcha, :enabled], true) + + new = Captcha.new() + + assert %{ + answer_data: answer, + token: token + } = new + + assert is_binary(answer) + + assert {:error, "Invalid answer data"} = + Captcha.validate(token, "63615261b77f5354fb8c4e4986477555", nil) + end + end end diff --git a/test/support/captcha_mock.ex b/test/support/captcha_mock.ex index 65ca6b3bd..6dae94edf 100644 --- a/test/support/captcha_mock.ex +++ b/test/support/captcha_mock.ex @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors +# Copyright © 2017-2020 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Captcha.Mock do @@ -7,8 +7,17 @@ defmodule Pleroma.Captcha.Mock do @behaviour Service @impl Service - def new, do: %{type: :mock} + def new, + do: %{ + type: :mock, + token: "afa1815e14e29355e6c8f6b143a39fa2", + answer_data: "63615261b77f5354fb8c4e4986477555", + url: "https://example.org/captcha.png" + } @impl Service - def validate(_token, _captcha, _data), do: :ok + def validate(_token, captcha, captcha) when not is_nil(captcha), do: :ok + + def validate(_token, captcha, answer), + do: {:error, "Invalid CAPTCHA captcha: #{inspect(captcha)} ; answer: #{inspect(answer)}"} end -- cgit v1.2.3 From 7ad5c51f23102e89c491a2ef731e108873a09d71 Mon Sep 17 00:00:00 2001 From: eugenijm Date: Thu, 9 Jan 2020 22:18:55 +0300 Subject: Admin API: `GET /api/pleroma/admin/stats` to get status count by visibility scope --- test/stat_test.exs | 70 ++++++++++++++++++++++++ test/tasks/refresh_counter_cache_test.exs | 43 +++++++++++++++ test/web/admin_api/admin_api_controller_test.exs | 19 +++++++ 3 files changed, 132 insertions(+) create mode 100644 test/stat_test.exs create mode 100644 test/tasks/refresh_counter_cache_test.exs (limited to 'test') diff --git a/test/stat_test.exs b/test/stat_test.exs new file mode 100644 index 000000000..1f0c6199a --- /dev/null +++ b/test/stat_test.exs @@ -0,0 +1,70 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.StateTest do + use Pleroma.DataCase + import Pleroma.Factory + alias Pleroma.Web.CommonAPI + + describe "status visibility count" do + test "on new status" do + user = insert(:user) + other_user = insert(:user) + + CommonAPI.post(user, %{"visibility" => "public", "status" => "hey"}) + + Enum.each(0..1, fn _ -> + CommonAPI.post(user, %{ + "visibility" => "unlisted", + "status" => "hey" + }) + end) + + Enum.each(0..2, fn _ -> + CommonAPI.post(user, %{ + "visibility" => "direct", + "status" => "hey @#{other_user.nickname}" + }) + end) + + Enum.each(0..3, fn _ -> + CommonAPI.post(user, %{ + "visibility" => "private", + "status" => "hey" + }) + end) + + assert %{direct: 3, private: 4, public: 1, unlisted: 2} = + Pleroma.Stats.get_status_visibility_count() + end + + test "on status delete" do + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"visibility" => "public", "status" => "hey"}) + assert %{public: 1} = Pleroma.Stats.get_status_visibility_count() + CommonAPI.delete(activity.id, user) + assert %{public: 0} = Pleroma.Stats.get_status_visibility_count() + end + + test "on status visibility update" do + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"visibility" => "public", "status" => "hey"}) + assert %{public: 1, private: 0} = Pleroma.Stats.get_status_visibility_count() + {:ok, _} = CommonAPI.update_activity_scope(activity.id, %{"visibility" => "private"}) + assert %{public: 0, private: 1} = Pleroma.Stats.get_status_visibility_count() + end + + test "doesn't count unrelated activities" do + user = insert(:user) + other_user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"visibility" => "public", "status" => "hey"}) + _ = CommonAPI.follow(user, other_user) + CommonAPI.favorite(activity.id, other_user) + CommonAPI.repeat(activity.id, other_user) + + assert %{direct: 0, private: 0, public: 1, unlisted: 0} = + Pleroma.Stats.get_status_visibility_count() + end + end +end diff --git a/test/tasks/refresh_counter_cache_test.exs b/test/tasks/refresh_counter_cache_test.exs new file mode 100644 index 000000000..47367af94 --- /dev/null +++ b/test/tasks/refresh_counter_cache_test.exs @@ -0,0 +1,43 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Mix.Tasks.Pleroma.RefreshCounterCacheTest do + use Pleroma.DataCase + alias Pleroma.Web.CommonAPI + import ExUnit.CaptureIO, only: [capture_io: 1] + import Pleroma.Factory + + test "counts statuses" do + user = insert(:user) + other_user = insert(:user) + + CommonAPI.post(user, %{"visibility" => "public", "status" => "hey"}) + + Enum.each(0..1, fn _ -> + CommonAPI.post(user, %{ + "visibility" => "unlisted", + "status" => "hey" + }) + end) + + Enum.each(0..2, fn _ -> + CommonAPI.post(user, %{ + "visibility" => "direct", + "status" => "hey @#{other_user.nickname}" + }) + end) + + Enum.each(0..3, fn _ -> + CommonAPI.post(user, %{ + "visibility" => "private", + "status" => "hey" + }) + end) + + assert capture_io(fn -> Mix.Tasks.Pleroma.RefreshCounterCache.run([]) end) =~ "Done\n" + + assert %{direct: 3, private: 4, public: 1, unlisted: 2} = + Pleroma.Stats.get_status_visibility_count() + end +end diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 908ef4d37..0b79e4c5c 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -3545,6 +3545,25 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do assert String.starts_with?(child["group"], ":") assert child["description"] end + + describe "/api/pleroma/admin/stats" do + test "status visibility count", %{conn: conn} do + admin = insert(:user, is_admin: true) + user = insert(:user) + CommonAPI.post(user, %{"visibility" => "public", "status" => "hey"}) + CommonAPI.post(user, %{"visibility" => "unlisted", "status" => "hey"}) + CommonAPI.post(user, %{"visibility" => "unlisted", "status" => "hey"}) + + response = + conn + |> assign(:user, admin) + |> get("/api/pleroma/admin/stats") + |> json_response(200) + + assert %{"direct" => 0, "private" => 0, "public" => 1, "unlisted" => 2} = + response["status_visibility"] + end + end end # Needed for testing -- cgit v1.2.3 From 5e4fe0e8f7fcfefc037885421d41de3a4328a881 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Mon, 24 Feb 2020 22:52:38 +0300 Subject: instance.gen task: fix crash when using custom static directory Closes #1082 --- test/tasks/instance_test.exs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/tasks/instance_test.exs b/test/tasks/instance_test.exs index d69275726..61f5833fc 100644 --- a/test/tasks/instance_test.exs +++ b/test/tasks/instance_test.exs @@ -3,9 +3,10 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.InstanceTest do - use ExUnit.Case, async: true + use ExUnit.Case setup do + static_dir = Pleroma.Config.get([:instance, :static_dir]) File.mkdir_p!(tmp_path()) on_exit(fn -> @@ -15,6 +16,8 @@ defmodule Pleroma.InstanceTest do if File.exists?(static_dir) do File.rm_rf(Path.join(static_dir, "robots.txt")) end + + Pleroma.Config.put([:instance, :static_dir], static_dir) end) :ok -- cgit v1.2.3 From 3eccdd15aa5f2ad5fc48fdfc2f039427aa6698f0 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 25 Feb 2020 15:19:52 +0300 Subject: compile fix --- test/tasks/instance_test.exs | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/tasks/instance_test.exs b/test/tasks/instance_test.exs index 61f5833fc..a0cc5d7c7 100644 --- a/test/tasks/instance_test.exs +++ b/test/tasks/instance_test.exs @@ -6,7 +6,6 @@ defmodule Pleroma.InstanceTest do use ExUnit.Case setup do - static_dir = Pleroma.Config.get([:instance, :static_dir]) File.mkdir_p!(tmp_path()) on_exit(fn -> -- cgit v1.2.3 From c495e6d387e929977317a039021417934b3328b9 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 25 Feb 2020 18:04:28 +0400 Subject: Add a test to ensure OAuth tokens are tied to Push subscriptions --- test/web/push/impl_test.exs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/web/push/impl_test.exs b/test/web/push/impl_test.exs index acae7a734..089d55577 100644 --- a/test/web/push/impl_test.exs +++ b/test/web/push/impl_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors +# Copyright © 2017-2020 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Push.ImplTest do @@ -98,6 +98,14 @@ defmodule Pleroma.Web.Push.ImplTest do refute Pleroma.Repo.get(Subscription, subscription.id) end + test "deletes subscription when token has been deleted" do + subscription = insert(:push_subscription) + + Pleroma.Repo.delete(subscription.token) + + refute Pleroma.Repo.get(Subscription, subscription.id) + end + test "renders title and body for create activity" do user = insert(:user, nickname: "Bob") -- cgit v1.2.3 From e2a6a403677ff4c879c398e46efb814866a3cd04 Mon Sep 17 00:00:00 2001 From: eugenijm Date: Mon, 10 Feb 2020 14:32:38 +0300 Subject: Admin API: `GET /api/pleroma/admin/statuses` - list all statuses (accepts `godmode` and `local_only`) --- test/web/admin_api/admin_api_controller_test.exs | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'test') diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 0b79e4c5c..faa584390 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -3065,6 +3065,52 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do end end + describe "GET /api/pleroma/admin/statuses" do + test "returns all public, unlisted, and direct statuses", %{conn: conn, admin: admin} do + blocked = insert(:user) + user = insert(:user) + User.block(admin, blocked) + + {:ok, _} = + CommonAPI.post(user, %{"status" => "@#{admin.nickname}", "visibility" => "direct"}) + + {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"}) + {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"}) + {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"}) + {:ok, _} = CommonAPI.post(blocked, %{"status" => ".", "visibility" => "public"}) + + response = + conn + |> get("/api/pleroma/admin/statuses") + |> json_response(200) + + refute "private" in Enum.map(response, & &1["visibility"]) + assert length(response) == 4 + end + + test "returns only local statuses with local_only on", %{conn: conn} do + user = insert(:user) + remote_user = insert(:user, local: false, nickname: "archaeme@archae.me") + insert(:note_activity, user: user, local: true) + insert(:note_activity, user: remote_user, local: false) + + response = + conn + |> get("/api/pleroma/admin/statuses?local_only=true") + |> json_response(200) + + assert length(response) == 1 + end + + test "returns private statuses with godmode on", %{conn: conn} do + user = insert(:user) + {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"}) + {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"}) + conn = get(conn, "/api/pleroma/admin/statuses?godmode=true") + assert json_response(conn, 200) |> length() == 2 + end + end + describe "GET /api/pleroma/admin/users/:nickname/statuses" do setup do user = insert(:user) -- cgit v1.2.3 From 4ab07cf0d53c24c8770bd3a47892b23344b103f6 Mon Sep 17 00:00:00 2001 From: eugenijm Date: Wed, 26 Feb 2020 14:47:19 +0300 Subject: Admin API: Exclude boosts from `GET /api/pleroma/admin/users/:nickname/statuses` and `GET /api/pleroma/admin/instance/:instance/statuses` --- test/web/admin_api/admin_api_controller_test.exs | 26 +++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 0b79e4c5c..7c33a51f8 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -3115,6 +3115,20 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do assert json_response(conn, 200) |> length() == 5 end + + test "excludes reblogs by default", %{conn: conn, user: user} do + other_user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"status" => "."}) + {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, other_user) + + conn_res = get(conn, "/api/pleroma/admin/users/#{other_user.nickname}/statuses") + assert json_response(conn_res, 200) |> length() == 0 + + conn_res = + get(conn, "/api/pleroma/admin/users/#{other_user.nickname}/statuses?with_reblogs=true") + + assert json_response(conn_res, 200) |> length() == 1 + end end describe "GET /api/pleroma/admin/moderation_log" do @@ -3397,7 +3411,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do user = insert(:user, local: false, nickname: "archaeme@archae.me") user2 = insert(:user, local: false, nickname: "test@test.com") insert_pair(:note_activity, user: user) - insert(:note_activity, user: user2) + activity = insert(:note_activity, user: user2) ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses") @@ -3416,6 +3430,16 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do response = json_response(ret_conn, 200) assert Enum.empty?(response) + + CommonAPI.repeat(activity.id, user) + + ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses") + response = json_response(ret_conn, 200) + assert length(response) == 2 + + ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true") + response = json_response(ret_conn, 200) + assert length(response) == 3 end end -- cgit v1.2.3