diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/fixtures/mastodon-post-activity.json | 13 | ||||
| -rw-r--r-- | test/object/fetcher_test.exs | 25 | ||||
| -rw-r--r-- | test/support/oban_helpers.ex | 4 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier_test.exs | 148 | ||||
| -rw-r--r-- | test/web/activity_pub/views/object_view_test.exs | 20 | 
5 files changed, 207 insertions, 3 deletions
| 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/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/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 1b12ee3a9..937f78cbe 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 @@ -40,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!() @@ -61,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!() @@ -73,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) @@ -1348,6 +1350,101 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do      end    end +  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 + +    clear_config([:instance, :federation_incoming_replies_max_depth]) + +    setup do +      data = +        "test/fixtures/mastodon-post-activity.json" +        |> File.read!() +        |> Poison.decode!() + +      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, "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", +         %{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"}) + +      {: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) + +      %{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, "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      test "it inlines private announced objects" do        user = insert(:user) @@ -2046,4 +2143,49 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do               }      end    end + +  describe "set_replies/1" do +    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 +      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 :note_replies_output_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.object.data["id"] end) + +      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 13447dc29..acc855b98 100644 --- a/test/web/activity_pub/views/object_view_test.exs +++ b/test/web/activity_pub/views/object_view_test.exs @@ -36,6 +36,26 @@ 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}) + +      replies_uris = [self_reply1.object.data["id"]] +      result = ObjectView.render("object.json", %{object: refresh_record(activity)}) + +      assert %{"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) | 
