logo

pleroma

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

delete_handling_test.exs (3493B)


  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.Transmogrifier.DeleteHandlingTest do
  5. use Oban.Testing, repo: Pleroma.Repo
  6. use Pleroma.DataCase
  7. alias Pleroma.Activity
  8. alias Pleroma.Object
  9. alias Pleroma.Tests.ObanHelpers
  10. alias Pleroma.User
  11. alias Pleroma.Web.ActivityPub.Transmogrifier
  12. import Pleroma.Factory
  13. setup_all do
  14. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  15. :ok
  16. end
  17. test "it works for incoming deletes" do
  18. activity = insert(:note_activity)
  19. deleting_user = insert(:user)
  20. data =
  21. File.read!("test/fixtures/mastodon-delete.json")
  22. |> Poison.decode!()
  23. |> Map.put("actor", deleting_user.ap_id)
  24. |> put_in(["object", "id"], activity.data["object"])
  25. {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
  26. Transmogrifier.handle_incoming(data)
  27. assert id == data["id"]
  28. # We delete the Create activity because we base our timelines on it.
  29. # This should be changed after we unify objects and activities
  30. refute Activity.get_by_id(activity.id)
  31. assert actor == deleting_user.ap_id
  32. # Objects are replaced by a tombstone object.
  33. object = Object.normalize(activity.data["object"])
  34. assert object.data["type"] == "Tombstone"
  35. end
  36. test "it works for incoming when the object has been pruned" do
  37. activity = insert(:note_activity)
  38. {:ok, object} =
  39. Object.normalize(activity.data["object"])
  40. |> Repo.delete()
  41. Cachex.del(:object_cache, "object:#{object.data["id"]}")
  42. deleting_user = insert(:user)
  43. data =
  44. File.read!("test/fixtures/mastodon-delete.json")
  45. |> Poison.decode!()
  46. |> Map.put("actor", deleting_user.ap_id)
  47. |> put_in(["object", "id"], activity.data["object"])
  48. {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
  49. Transmogrifier.handle_incoming(data)
  50. assert id == data["id"]
  51. # We delete the Create activity because we base our timelines on it.
  52. # This should be changed after we unify objects and activities
  53. refute Activity.get_by_id(activity.id)
  54. assert actor == deleting_user.ap_id
  55. end
  56. test "it fails for incoming deletes with spoofed origin" do
  57. activity = insert(:note_activity)
  58. %{ap_id: ap_id} = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
  59. data =
  60. File.read!("test/fixtures/mastodon-delete.json")
  61. |> Poison.decode!()
  62. |> Map.put("actor", ap_id)
  63. |> put_in(["object", "id"], activity.data["object"])
  64. assert match?({:error, _}, Transmogrifier.handle_incoming(data))
  65. end
  66. @tag capture_log: true
  67. test "it works for incoming user deletes" do
  68. %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
  69. data =
  70. File.read!("test/fixtures/mastodon-delete-user.json")
  71. |> Poison.decode!()
  72. {:ok, _} = Transmogrifier.handle_incoming(data)
  73. ObanHelpers.perform_all()
  74. assert User.get_cached_by_ap_id(ap_id).deactivated
  75. end
  76. test "it fails for incoming user deletes with spoofed origin" do
  77. %{ap_id: ap_id} = insert(:user)
  78. data =
  79. File.read!("test/fixtures/mastodon-delete-user.json")
  80. |> Poison.decode!()
  81. |> Map.put("actor", ap_id)
  82. assert match?({:error, _}, Transmogrifier.handle_incoming(data))
  83. assert User.get_cached_by_ap_id(ap_id)
  84. end
  85. end