logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

like_validation_test.exs (3584B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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, async: true
  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 "Add object actor from 'to' field if it doesn't owns the like", %{valid_like: valid_like} do
  34. user = insert(:user)
  35. object_actor = valid_like["actor"]
  36. valid_like =
  37. valid_like
  38. |> Map.put("actor", user.ap_id)
  39. |> Map.put("to", [])
  40. {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
  41. assert object_actor in object["to"]
  42. end
  43. test "Removes object actor from 'to' field if it owns the like", %{
  44. valid_like: valid_like,
  45. user: user
  46. } do
  47. valid_like =
  48. valid_like
  49. |> Map.put("to", [user.ap_id])
  50. {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
  51. refute user.ap_id in object["to"]
  52. end
  53. test "sets the context field to the context of the object if no context is given", %{
  54. valid_like: valid_like,
  55. post_activity: post_activity
  56. } do
  57. without_context =
  58. valid_like
  59. |> Map.delete("context")
  60. {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
  61. assert object["context"] == post_activity.data["context"]
  62. end
  63. test "it errors when the object is missing or not known", %{valid_like: valid_like} do
  64. without_object = Map.delete(valid_like, "object")
  65. refute LikeValidator.cast_and_validate(without_object).valid?
  66. with_invalid_object = Map.put(valid_like, "object", "invalidobject")
  67. refute LikeValidator.cast_and_validate(with_invalid_object).valid?
  68. end
  69. test "it errors when the actor has already like the object", %{
  70. valid_like: valid_like,
  71. user: user,
  72. post_activity: post_activity
  73. } do
  74. _like = CommonAPI.favorite(post_activity.id, user)
  75. refute LikeValidator.cast_and_validate(valid_like).valid?
  76. end
  77. test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
  78. wrapped_like =
  79. valid_like
  80. |> Map.put("actor", %{"id" => valid_like["actor"]})
  81. |> Map.put("object", %{"id" => valid_like["object"]})
  82. validated = LikeValidator.cast_and_validate(wrapped_like)
  83. assert validated.valid?
  84. assert {:actor, valid_like["actor"]} in validated.changes
  85. assert {:object, valid_like["object"]} in validated.changes
  86. end
  87. end
  88. end