logo

pleroma

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

delete_validation_test.exs (3538B)


  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.DeleteValidationTest 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 "deletes" do
  12. setup do
  13. user = insert(:user)
  14. {:ok, post_activity} = CommonAPI.post(user, %{status: "cancel me daddy"})
  15. {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
  16. {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
  17. %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
  18. end
  19. test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
  20. {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
  21. assert valid_post_delete["deleted_activity_id"]
  22. end
  23. test "it is invalid if the object isn't in a list of certain types", %{
  24. valid_post_delete: valid_post_delete
  25. } do
  26. object = Object.get_by_ap_id(valid_post_delete["object"])
  27. data =
  28. object.data
  29. |> Map.put("type", "Like")
  30. {:ok, _object} =
  31. object
  32. |> Ecto.Changeset.change(%{data: data})
  33. |> Object.update_and_set_cache()
  34. {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
  35. assert {:object, {"object not in allowed types", []}} in cng.errors
  36. end
  37. test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
  38. assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
  39. end
  40. test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
  41. no_id =
  42. valid_post_delete
  43. |> Map.delete("id")
  44. {:error, cng} = ObjectValidator.validate(no_id, [])
  45. assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
  46. end
  47. test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
  48. missing_object =
  49. valid_post_delete
  50. |> Map.put("object", "http://does.not/exist")
  51. {:error, cng} = ObjectValidator.validate(missing_object, [])
  52. assert {:object, {"can't find object", []}} in cng.errors
  53. end
  54. test "it's invalid if the actor of the object and the actor of delete are from different domains",
  55. %{valid_post_delete: valid_post_delete} do
  56. valid_user = insert(:user)
  57. valid_other_actor =
  58. valid_post_delete
  59. |> Map.put("actor", valid_user.ap_id)
  60. assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
  61. invalid_other_actor =
  62. valid_post_delete
  63. |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
  64. {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
  65. assert {:actor, {"is not allowed to modify object", []}} in cng.errors
  66. end
  67. test "it's valid if the actor of the object is a local superuser",
  68. %{valid_post_delete: valid_post_delete} do
  69. user =
  70. insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
  71. valid_other_actor =
  72. valid_post_delete
  73. |> Map.put("actor", user.ap_id)
  74. {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
  75. assert meta[:do_not_federate]
  76. end
  77. end
  78. end