logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

like_validation_test.exs (3555B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Web.ActivityPub.ObjectValidator
  7. alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
  8. alias Pleroma.Web.ActivityPub.Utils
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. describe "likes" do
  12. setup do
  13. user = insert(:user)
  14. {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
  15. valid_like = %{
  16. "to" => [user.ap_id],
  17. "cc" => [],
  18. "type" => "Like",
  19. "id" => Utils.generate_activity_id(),
  20. "object" => post_activity.data["object"],
  21. "actor" => user.ap_id,
  22. "context" => "a context"
  23. }
  24. %{valid_like: valid_like, user: user, post_activity: post_activity}
  25. end
  26. test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
  27. {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
  28. assert "id" in Map.keys(object)
  29. end
  30. test "is valid for a valid object", %{valid_like: valid_like} do
  31. assert LikeValidator.cast_and_validate(valid_like).valid?
  32. end
  33. test "sets the 'to' field to the object actor if no recipients are given", %{
  34. valid_like: valid_like,
  35. user: user
  36. } do
  37. without_recipients =
  38. valid_like
  39. |> Map.delete("to")
  40. {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
  41. assert object["to"] == [user.ap_id]
  42. end
  43. test "sets the context field to the context of the object if no context is given", %{
  44. valid_like: valid_like,
  45. post_activity: post_activity
  46. } do
  47. without_context =
  48. valid_like
  49. |> Map.delete("context")
  50. {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
  51. assert object["context"] == post_activity.data["context"]
  52. end
  53. test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
  54. without_actor = Map.delete(valid_like, "actor")
  55. refute LikeValidator.cast_and_validate(without_actor).valid?
  56. with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
  57. refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
  58. end
  59. test "it errors when the object is missing or not known", %{valid_like: valid_like} do
  60. without_object = Map.delete(valid_like, "object")
  61. refute LikeValidator.cast_and_validate(without_object).valid?
  62. with_invalid_object = Map.put(valid_like, "object", "invalidobject")
  63. refute LikeValidator.cast_and_validate(with_invalid_object).valid?
  64. end
  65. test "it errors when the actor has already like the object", %{
  66. valid_like: valid_like,
  67. user: user,
  68. post_activity: post_activity
  69. } do
  70. _like = CommonAPI.favorite(user, post_activity.id)
  71. refute LikeValidator.cast_and_validate(valid_like).valid?
  72. end
  73. test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
  74. wrapped_like =
  75. valid_like
  76. |> Map.put("actor", %{"id" => valid_like["actor"]})
  77. |> Map.put("object", %{"id" => valid_like["object"]})
  78. validated = LikeValidator.cast_and_validate(wrapped_like)
  79. assert validated.valid?
  80. assert {:actor, valid_like["actor"]} in validated.changes
  81. assert {:object, valid_like["object"]} in validated.changes
  82. end
  83. end
  84. end