diff options
| author | Ivan Tashkinov <ivantashkinov@gmail.com> | 2020-08-11 15:13:29 +0300 | 
|---|---|---|
| committer | Ivan Tashkinov <ivantashkinov@gmail.com> | 2020-08-11 15:13:29 +0300 | 
| commit | 2def3cbf417075146da5e54201e9b969e6aa3eca (patch) | |
| tree | 93cdf149a1874f56d2808598f8aeee15e60c8a10 /test/web/activity_pub | |
| parent | 56ddf20208657487bf0298409cf91b11dac346ff (diff) | |
| parent | 686002164a5ab63ccb22cff015faf87f8f7fc9ec (diff) | |
| download | pleroma-2def3cbf417075146da5e54201e9b969e6aa3eca.tar.gz pleroma-2def3cbf417075146da5e54201e9b969e6aa3eca.zip | |
Merge remote-tracking branch 'remotes/origin/develop' into 2168-media-preview-proxy
# Conflicts:
#	config/config.exs
#	mix.lock
Diffstat (limited to 'test/web/activity_pub')
4 files changed, 224 insertions, 79 deletions
| diff --git a/test/web/activity_pub/object_validators/delete_validation_test.exs b/test/web/activity_pub/object_validators/delete_validation_test.exs index 42cd18298..02683b899 100644 --- a/test/web/activity_pub/object_validators/delete_validation_test.exs +++ b/test/web/activity_pub/object_validators/delete_validation_test.exs @@ -87,7 +87,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidationTest do        {:error, cng} = ObjectValidator.validate(invalid_other_actor, []) -      assert {:actor, {"is not allowed to delete object", []}} in cng.errors +      assert {:actor, {"is not allowed to modify object", []}} in cng.errors      end      test "it's valid if the actor of the object is a local superuser", diff --git a/test/web/activity_pub/transmogrifier/answer_handling_test.exs b/test/web/activity_pub/transmogrifier/answer_handling_test.exs new file mode 100644 index 000000000..0f6605c3f --- /dev/null +++ b/test/web/activity_pub/transmogrifier/answer_handling_test.exs @@ -0,0 +1,78 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.Transmogrifier.AnswerHandlingTest do +  use Pleroma.DataCase + +  alias Pleroma.Activity +  alias Pleroma.Object +  alias Pleroma.Web.ActivityPub.Transmogrifier +  alias Pleroma.Web.CommonAPI + +  import Pleroma.Factory + +  setup_all do +    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) +    :ok +  end + +  test "incoming, rewrites Note to Answer and increments vote counters" do +    user = insert(:user) + +    {:ok, activity} = +      CommonAPI.post(user, %{ +        status: "suya...", +        poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10} +      }) + +    object = Object.normalize(activity) + +    data = +      File.read!("test/fixtures/mastodon-vote.json") +      |> Poison.decode!() +      |> Kernel.put_in(["to"], user.ap_id) +      |> Kernel.put_in(["object", "inReplyTo"], object.data["id"]) +      |> Kernel.put_in(["object", "to"], user.ap_id) + +    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) +    answer_object = Object.normalize(activity) +    assert answer_object.data["type"] == "Answer" +    assert answer_object.data["inReplyTo"] == object.data["id"] + +    new_object = Object.get_by_ap_id(object.data["id"]) +    assert new_object.data["replies_count"] == object.data["replies_count"] + +    assert Enum.any?( +             new_object.data["oneOf"], +             fn +               %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true +               _ -> false +             end +           ) +  end + +  test "outgoing, rewrites Answer to Note" do +    user = insert(:user) + +    {:ok, poll_activity} = +      CommonAPI.post(user, %{ +        status: "suya...", +        poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10} +      }) + +    poll_object = Object.normalize(poll_activity) +    # TODO: Replace with CommonAPI vote creation when implemented +    data = +      File.read!("test/fixtures/mastodon-vote.json") +      |> Poison.decode!() +      |> Kernel.put_in(["to"], user.ap_id) +      |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"]) +      |> Kernel.put_in(["object", "to"], user.ap_id) + +    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) +    {:ok, data} = Transmogrifier.prepare_outgoing(activity.data) + +    assert data["object"]["type"] == "Note" +  end +end diff --git a/test/web/activity_pub/transmogrifier/question_handling_test.exs b/test/web/activity_pub/transmogrifier/question_handling_test.exs new file mode 100644 index 000000000..9fb965d7f --- /dev/null +++ b/test/web/activity_pub/transmogrifier/question_handling_test.exs @@ -0,0 +1,123 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.Transmogrifier.QuestionHandlingTest do +  use Pleroma.DataCase + +  alias Pleroma.Activity +  alias Pleroma.Object +  alias Pleroma.Web.ActivityPub.Transmogrifier +  alias Pleroma.Web.CommonAPI + +  import Pleroma.Factory + +  setup_all do +    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) +    :ok +  end + +  test "Mastodon Question activity" do +    data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!() + +    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) + +    object = Object.normalize(activity, false) + +    assert object.data["closed"] == "2019-05-11T09:03:36Z" + +    assert object.data["context"] == activity.data["context"] + +    assert object.data["context"] == +             "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation" + +    assert object.data["context_id"] + +    assert object.data["anyOf"] == [] + +    assert Enum.sort(object.data["oneOf"]) == +             Enum.sort([ +               %{ +                 "name" => "25 char limit is dumb", +                 "replies" => %{"totalItems" => 0, "type" => "Collection"}, +                 "type" => "Note" +               }, +               %{ +                 "name" => "Dunno", +                 "replies" => %{"totalItems" => 0, "type" => "Collection"}, +                 "type" => "Note" +               }, +               %{ +                 "name" => "Everyone knows that!", +                 "replies" => %{"totalItems" => 1, "type" => "Collection"}, +                 "type" => "Note" +               }, +               %{ +                 "name" => "I can't even fit a funny", +                 "replies" => %{"totalItems" => 1, "type" => "Collection"}, +                 "type" => "Note" +               } +             ]) + +    user = insert(:user) + +    {:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id}) + +    reply_object = Object.normalize(reply_activity, false) + +    assert reply_object.data["context"] == object.data["context"] +    assert reply_object.data["context_id"] == object.data["context_id"] +  end + +  test "Mastodon Question activity with HTML tags in plaintext" do +    options = [ +      %{ +        "type" => "Note", +        "name" => "<input type=\"date\">", +        "replies" => %{"totalItems" => 0, "type" => "Collection"} +      }, +      %{ +        "type" => "Note", +        "name" => "<input type=\"date\"/>", +        "replies" => %{"totalItems" => 0, "type" => "Collection"} +      }, +      %{ +        "type" => "Note", +        "name" => "<input type=\"date\" />", +        "replies" => %{"totalItems" => 1, "type" => "Collection"} +      }, +      %{ +        "type" => "Note", +        "name" => "<input type=\"date\"></input>", +        "replies" => %{"totalItems" => 1, "type" => "Collection"} +      } +    ] + +    data = +      File.read!("test/fixtures/mastodon-question-activity.json") +      |> Poison.decode!() +      |> Kernel.put_in(["object", "oneOf"], options) + +    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) +    object = Object.normalize(activity, false) + +    assert Enum.sort(object.data["oneOf"]) == Enum.sort(options) +  end + +  test "returns an error if received a second time" do +    data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!() + +    assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) + +    assert {:error, {:validate_object, {:error, _}}} = Transmogrifier.handle_incoming(data) +  end + +  test "accepts a Question with no content" do +    data = +      File.read!("test/fixtures/mastodon-question-activity.json") +      |> Poison.decode!() +      |> Kernel.put_in(["object", "content"], "") + +    assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data) +  end +end diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 828964a36..6dd9a3fec 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -225,23 +225,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert Enum.at(object.data["tag"], 2) == "moo"      end -    test "it works for incoming questions" do -      data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!() - -      {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) - -      object = Object.normalize(activity) - -      assert Enum.all?(object.data["oneOf"], fn choice -> -               choice["name"] in [ -                 "Dunno", -                 "Everyone knows that!", -                 "25 char limit is dumb", -                 "I can't even fit a funny" -               ] -             end) -    end -      test "it works for incoming listens" do        data = %{          "@context" => "https://www.w3.org/ns/activitystreams", @@ -271,38 +254,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert object.data["length"] == 180_000      end -    test "it rewrites Note votes to Answers and increments vote counters on question activities" do -      user = insert(:user) - -      {:ok, activity} = -        CommonAPI.post(user, %{ -          status: "suya...", -          poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10} -        }) - -      object = Object.normalize(activity) - -      data = -        File.read!("test/fixtures/mastodon-vote.json") -        |> Poison.decode!() -        |> Kernel.put_in(["to"], user.ap_id) -        |> Kernel.put_in(["object", "inReplyTo"], object.data["id"]) -        |> Kernel.put_in(["object", "to"], user.ap_id) - -      {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) -      answer_object = Object.normalize(activity) -      assert answer_object.data["type"] == "Answer" -      object = Object.get_by_ap_id(object.data["id"]) - -      assert Enum.any?( -               object.data["oneOf"], -               fn -                 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true -                 _ -> false -               end -             ) -    end -      test "it works for incoming notices with contentMap" do        data =          File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!() @@ -677,7 +628,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do                     %{                       "href" =>                         "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", -                     "mediaType" => "video/mp4" +                     "mediaType" => "video/mp4", +                     "type" => "Link"                     }                   ]                 } @@ -696,7 +648,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do                     %{                       "href" =>                         "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4", -                     "mediaType" => "video/mp4" +                     "mediaType" => "video/mp4", +                     "type" => "Link"                     }                   ]                 } @@ -1269,30 +1222,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do      end    end -  test "Rewrites Answers to Notes" do -    user = insert(:user) - -    {:ok, poll_activity} = -      CommonAPI.post(user, %{ -        status: "suya...", -        poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10} -      }) - -    poll_object = Object.normalize(poll_activity) -    # TODO: Replace with CommonAPI vote creation when implemented -    data = -      File.read!("test/fixtures/mastodon-vote.json") -      |> Poison.decode!() -      |> Kernel.put_in(["to"], user.ap_id) -      |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"]) -      |> Kernel.put_in(["object", "to"], user.ap_id) - -    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data) -    {:ok, data} = Transmogrifier.prepare_outgoing(activity.data) - -    assert data["object"]["type"] == "Note" -  end -    describe "fix_explicit_addressing" do      setup do        user = insert(:user) @@ -1540,8 +1469,13 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do                 "attachment" => [                   %{                     "mediaType" => "video/mp4", +                   "type" => "Document",                     "url" => [ -                     %{"href" => "https://peertube.moe/stat-480.mp4", "mediaType" => "video/mp4"} +                     %{ +                       "href" => "https://peertube.moe/stat-480.mp4", +                       "mediaType" => "video/mp4", +                       "type" => "Link" +                     }                     ]                   }                 ] @@ -1558,14 +1492,24 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do                 "attachment" => [                   %{                     "mediaType" => "video/mp4", +                   "type" => "Document",                     "url" => [ -                     %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"} +                     %{ +                       "href" => "https://pe.er/stat-480.mp4", +                       "mediaType" => "video/mp4", +                       "type" => "Link" +                     }                     ]                   },                   %{                     "mediaType" => "video/mp4", +                   "type" => "Document",                     "url" => [ -                     %{"href" => "https://pe.er/stat-480.mp4", "mediaType" => "video/mp4"} +                     %{ +                       "href" => "https://pe.er/stat-480.mp4", +                       "mediaType" => "video/mp4", +                       "type" => "Link" +                     }                     ]                   }                 ] | 
