logo

pleroma

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

announce_validation_test.exs (3498B)


  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.AnnouncValidationTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Object
  7. alias Pleroma.Web.ActivityPub.Builder
  8. alias Pleroma.Web.ActivityPub.ObjectValidator
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. describe "announces" do
  12. setup do
  13. user = insert(:user)
  14. announcer = insert(:user)
  15. {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
  16. object = Object.normalize(post_activity, false)
  17. {:ok, valid_announce, []} = Builder.announce(announcer, object)
  18. %{
  19. valid_announce: valid_announce,
  20. user: user,
  21. post_activity: post_activity,
  22. announcer: announcer
  23. }
  24. end
  25. test "returns ok for a valid announce", %{valid_announce: valid_announce} do
  26. assert {:ok, _object, _meta} = ObjectValidator.validate(valid_announce, [])
  27. end
  28. test "returns an error if the object can't be found", %{valid_announce: valid_announce} do
  29. without_object =
  30. valid_announce
  31. |> Map.delete("object")
  32. {:error, cng} = ObjectValidator.validate(without_object, [])
  33. assert {:object, {"can't be blank", [validation: :required]}} in cng.errors
  34. nonexisting_object =
  35. valid_announce
  36. |> Map.put("object", "https://gensokyo.2hu/objects/99999999")
  37. {:error, cng} = ObjectValidator.validate(nonexisting_object, [])
  38. assert {:object, {"can't find object", []}} in cng.errors
  39. end
  40. test "returns an error if we don't have the actor", %{valid_announce: valid_announce} do
  41. nonexisting_actor =
  42. valid_announce
  43. |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
  44. {:error, cng} = ObjectValidator.validate(nonexisting_actor, [])
  45. assert {:actor, {"can't find user", []}} in cng.errors
  46. end
  47. test "returns an error if the actor already announced the object", %{
  48. valid_announce: valid_announce,
  49. announcer: announcer,
  50. post_activity: post_activity
  51. } do
  52. _announce = CommonAPI.repeat(post_activity.id, announcer)
  53. {:error, cng} = ObjectValidator.validate(valid_announce, [])
  54. assert {:actor, {"already announced this object", []}} in cng.errors
  55. assert {:object, {"already announced by this actor", []}} in cng.errors
  56. end
  57. test "returns an error if the actor can't announce the object", %{
  58. announcer: announcer,
  59. user: user
  60. } do
  61. {:ok, post_activity} =
  62. CommonAPI.post(user, %{status: "a secret post", visibility: "private"})
  63. object = Object.normalize(post_activity, false)
  64. # Another user can't announce it
  65. {:ok, announce, []} = Builder.announce(announcer, object, public: false)
  66. {:error, cng} = ObjectValidator.validate(announce, [])
  67. assert {:actor, {"can not announce this object", []}} in cng.errors
  68. # The actor of the object can announce it
  69. {:ok, announce, []} = Builder.announce(user, object, public: false)
  70. assert {:ok, _, _} = ObjectValidator.validate(announce, [])
  71. # The actor of the object can not announce it publicly
  72. {:ok, announce, []} = Builder.announce(user, object, public: true)
  73. {:error, cng} = ObjectValidator.validate(announce, [])
  74. assert {:actor, {"can not announce this object publicly", []}} in cng.errors
  75. end
  76. end
  77. end