diff options
Diffstat (limited to 'test/web/activity_pub')
| -rw-r--r-- | test/web/activity_pub/activity_pub_test.exs | 16 | ||||
| -rw-r--r-- | test/web/activity_pub/object_validator_test.exs | 83 | ||||
| -rw-r--r-- | test/web/activity_pub/object_validators/note_validator_test.exs | 35 | ||||
| -rw-r--r-- | test/web/activity_pub/object_validators/types/date_time_test.exs | 32 | ||||
| -rw-r--r-- | test/web/activity_pub/pipeline_test.exs | 87 | ||||
| -rw-r--r-- | test/web/activity_pub/side_effects_test.exs | 34 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier_test.exs | 4 | ||||
| -rw-r--r-- | test/web/activity_pub/views/object_view_test.exs | 2 | 
8 files changed, 283 insertions, 10 deletions
diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index d86c8260e..2ea03bbf9 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1894,14 +1894,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        {:ok, a4} = CommonAPI.post(user2, %{"status" => "Agent Smith "})        {:ok, a5} = CommonAPI.post(user1, %{"status" => "Red or Blue "}) -      {:ok, _, _} = CommonAPI.favorite(a4.id, user) -      {:ok, _, _} = CommonAPI.favorite(a3.id, other_user) -      {:ok, _, _} = CommonAPI.favorite(a3.id, user) -      {:ok, _, _} = CommonAPI.favorite(a5.id, other_user) -      {:ok, _, _} = CommonAPI.favorite(a5.id, user) -      {:ok, _, _} = CommonAPI.favorite(a4.id, other_user) -      {:ok, _, _} = CommonAPI.favorite(a1.id, user) -      {:ok, _, _} = CommonAPI.favorite(a1.id, other_user) +      {:ok, _} = CommonAPI.favorite(user, a4.id) +      {:ok, _} = CommonAPI.favorite(other_user, a3.id) +      {:ok, _} = CommonAPI.favorite(user, a3.id) +      {:ok, _} = CommonAPI.favorite(other_user, a5.id) +      {:ok, _} = CommonAPI.favorite(user, a5.id) +      {:ok, _} = CommonAPI.favorite(other_user, a4.id) +      {:ok, _} = CommonAPI.favorite(user, a1.id) +      {:ok, _} = CommonAPI.favorite(other_user, a1.id)        result = ActivityPub.fetch_favourites(user)        assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id] diff --git a/test/web/activity_pub/object_validator_test.exs b/test/web/activity_pub/object_validator_test.exs new file mode 100644 index 000000000..3c5c3696e --- /dev/null +++ b/test/web/activity_pub/object_validator_test.exs @@ -0,0 +1,83 @@ +defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do +  use Pleroma.DataCase + +  alias Pleroma.Web.ActivityPub.ObjectValidator +  alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator +  alias Pleroma.Web.ActivityPub.Utils +  alias Pleroma.Web.CommonAPI + +  import Pleroma.Factory + +  describe "likes" do +    setup do +      user = insert(:user) +      {:ok, post_activity} = CommonAPI.post(user, %{"status" => "uguu"}) + +      valid_like = %{ +        "to" => [user.ap_id], +        "cc" => [], +        "type" => "Like", +        "id" => Utils.generate_activity_id(), +        "object" => post_activity.data["object"], +        "actor" => user.ap_id, +        "context" => "a context" +      } + +      %{valid_like: valid_like, user: user, post_activity: post_activity} +    end + +    test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do +      {:ok, object, _meta} = ObjectValidator.validate(valid_like, []) + +      assert "id" in Map.keys(object) +    end + +    test "is valid for a valid object", %{valid_like: valid_like} do +      assert LikeValidator.cast_and_validate(valid_like).valid? +    end + +    test "it errors when the actor is missing or not known", %{valid_like: valid_like} do +      without_actor = Map.delete(valid_like, "actor") + +      refute LikeValidator.cast_and_validate(without_actor).valid? + +      with_invalid_actor = Map.put(valid_like, "actor", "invalidactor") + +      refute LikeValidator.cast_and_validate(with_invalid_actor).valid? +    end + +    test "it errors when the object is missing or not known", %{valid_like: valid_like} do +      without_object = Map.delete(valid_like, "object") + +      refute LikeValidator.cast_and_validate(without_object).valid? + +      with_invalid_object = Map.put(valid_like, "object", "invalidobject") + +      refute LikeValidator.cast_and_validate(with_invalid_object).valid? +    end + +    test "it errors when the actor has already like the object", %{ +      valid_like: valid_like, +      user: user, +      post_activity: post_activity +    } do +      _like = CommonAPI.favorite(user, post_activity.id) + +      refute LikeValidator.cast_and_validate(valid_like).valid? +    end + +    test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do +      wrapped_like = +        valid_like +        |> Map.put("actor", %{"id" => valid_like["actor"]}) +        |> Map.put("object", %{"id" => valid_like["object"]}) + +      validated = LikeValidator.cast_and_validate(wrapped_like) + +      assert validated.valid? + +      assert {:actor, valid_like["actor"]} in validated.changes +      assert {:object, valid_like["object"]} in validated.changes +    end +  end +end diff --git a/test/web/activity_pub/object_validators/note_validator_test.exs b/test/web/activity_pub/object_validators/note_validator_test.exs new file mode 100644 index 000000000..2bcd75e25 --- /dev/null +++ b/test/web/activity_pub/object_validators/note_validator_test.exs @@ -0,0 +1,35 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.ObjectValidators.NoteValidatorTest do +  use Pleroma.DataCase + +  alias Pleroma.Web.ActivityPub.ObjectValidators.NoteValidator +  alias Pleroma.Web.ActivityPub.Utils + +  import Pleroma.Factory + +  describe "Notes" do +    setup do +      user = insert(:user) + +      note = %{ +        "id" => Utils.generate_activity_id(), +        "type" => "Note", +        "actor" => user.ap_id, +        "to" => [user.follower_address], +        "cc" => [], +        "content" => "Hellow this is content.", +        "context" => "xxx", +        "summary" => "a post" +      } + +      %{user: user, note: note} +    end + +    test "a basic note validates", %{note: note} do +      %{valid?: true} = NoteValidator.cast_and_validate(note) +    end +  end +end diff --git a/test/web/activity_pub/object_validators/types/date_time_test.exs b/test/web/activity_pub/object_validators/types/date_time_test.exs new file mode 100644 index 000000000..3e17a9497 --- /dev/null +++ b/test/web/activity_pub/object_validators/types/date_time_test.exs @@ -0,0 +1,32 @@ +defmodule Pleroma.Web.ActivityPub.ObjectValidators.Types.DateTimeTest do +  alias Pleroma.Web.ActivityPub.ObjectValidators.Types.DateTime +  use Pleroma.DataCase + +  test "it validates an xsd:Datetime" do +    valid_strings = [ +      "2004-04-12T13:20:00", +      "2004-04-12T13:20:15.5", +      "2004-04-12T13:20:00-05:00", +      "2004-04-12T13:20:00Z" +    ] + +    invalid_strings = [ +      "2004-04-12T13:00", +      "2004-04-1213:20:00", +      "99-04-12T13:00", +      "2004-04-12" +    ] + +    assert {:ok, "2004-04-01T12:00:00Z"} == DateTime.cast("2004-04-01T12:00:00Z") + +    Enum.each(valid_strings, fn date_time -> +      result = DateTime.cast(date_time) +      assert {:ok, _} = result +    end) + +    Enum.each(invalid_strings, fn date_time -> +      result = DateTime.cast(date_time) +      assert :error == result +    end) +  end +end diff --git a/test/web/activity_pub/pipeline_test.exs b/test/web/activity_pub/pipeline_test.exs new file mode 100644 index 000000000..318d306af --- /dev/null +++ b/test/web/activity_pub/pipeline_test.exs @@ -0,0 +1,87 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.PipelineTest do +  use Pleroma.DataCase + +  import Mock +  import Pleroma.Factory + +  describe "common_pipeline/2" do +    test "it goes through validation, filtering, persisting, side effects and federation for local activities" do +      activity = insert(:note_activity) +      meta = [local: true] + +      with_mocks([ +        {Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]}, +        { +          Pleroma.Web.ActivityPub.MRF, +          [], +          [filter: fn o -> {:ok, o} end] +        }, +        { +          Pleroma.Web.ActivityPub.ActivityPub, +          [], +          [persist: fn o, m -> {:ok, o, m} end] +        }, +        { +          Pleroma.Web.ActivityPub.SideEffects, +          [], +          [handle: fn o, m -> {:ok, o, m} end] +        }, +        { +          Pleroma.Web.Federator, +          [], +          [publish: fn _o -> :ok end] +        } +      ]) do +        assert {:ok, ^activity, ^meta} = +                 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta) + +        assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta)) +        assert_called(Pleroma.Web.ActivityPub.MRF.filter(activity)) +        assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta)) +        assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta)) +        assert_called(Pleroma.Web.Federator.publish(activity)) +      end +    end + +    test "it goes through validation, filtering, persisting, side effects without federation for remote activities" do +      activity = insert(:note_activity) +      meta = [local: false] + +      with_mocks([ +        {Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]}, +        { +          Pleroma.Web.ActivityPub.MRF, +          [], +          [filter: fn o -> {:ok, o} end] +        }, +        { +          Pleroma.Web.ActivityPub.ActivityPub, +          [], +          [persist: fn o, m -> {:ok, o, m} end] +        }, +        { +          Pleroma.Web.ActivityPub.SideEffects, +          [], +          [handle: fn o, m -> {:ok, o, m} end] +        }, +        { +          Pleroma.Web.Federator, +          [], +          [] +        } +      ]) do +        assert {:ok, ^activity, ^meta} = +                 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta) + +        assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta)) +        assert_called(Pleroma.Web.ActivityPub.MRF.filter(activity)) +        assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta)) +        assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta)) +      end +    end +  end +end diff --git a/test/web/activity_pub/side_effects_test.exs b/test/web/activity_pub/side_effects_test.exs new file mode 100644 index 000000000..ef91954ae --- /dev/null +++ b/test/web/activity_pub/side_effects_test.exs @@ -0,0 +1,34 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.SideEffectsTest do +  use Pleroma.DataCase + +  alias Pleroma.Object +  alias Pleroma.Web.ActivityPub.ActivityPub +  alias Pleroma.Web.ActivityPub.Builder +  alias Pleroma.Web.ActivityPub.SideEffects +  alias Pleroma.Web.CommonAPI + +  import Pleroma.Factory + +  describe "like objects" do +    setup do +      user = insert(:user) +      {:ok, post} = CommonAPI.post(user, %{"status" => "hey"}) + +      {:ok, like_data, _meta} = Builder.like(user, post.object) +      {:ok, like, _meta} = ActivityPub.persist(like_data, local: true) + +      %{like: like, user: user} +    end + +    test "add the like to the original object", %{like: like, user: user} do +      {:ok, like, _} = SideEffects.handle(like) +      object = Object.get_by_ap_id(like.data["object"]) +      assert object.data["like_count"] == 1 +      assert user.ap_id in object.data["likes"] +    end +  end +end diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index efbca82f6..83372ec7e 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -334,7 +334,9 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do          |> Poison.decode!()          |> Map.put("object", activity.data["object"]) -      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) +      {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data) + +      refute Enum.empty?(activity.recipients)        assert data["actor"] == "http://mastodon.example.org/users/admin"        assert data["type"] == "Like" diff --git a/test/web/activity_pub/views/object_view_test.exs b/test/web/activity_pub/views/object_view_test.exs index 09866e99b..f6796ad4a 100644 --- a/test/web/activity_pub/views/object_view_test.exs +++ b/test/web/activity_pub/views/object_view_test.exs @@ -61,7 +61,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectViewTest do      object = Object.normalize(note)      user = insert(:user) -    {:ok, like_activity, _} = CommonAPI.favorite(note.id, user) +    {:ok, like_activity} = CommonAPI.favorite(user, note.id)      result = ObjectView.render("object.json", %{object: like_activity})  | 
