logo

pleroma

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

undo_handling_test.exs (6309B)


  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.UndoHandlingTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Object
  8. alias Pleroma.User
  9. alias Pleroma.Web.ActivityPub.Transmogrifier
  10. alias Pleroma.Web.CommonAPI
  11. import Pleroma.Factory
  12. test "it works for incoming emoji reaction undos" do
  13. user = insert(:user)
  14. {:ok, activity} = CommonAPI.post(user, %{status: "hello"})
  15. {:ok, reaction_activity} = CommonAPI.react_with_emoji(activity.id, user, "👌")
  16. data =
  17. File.read!("test/fixtures/mastodon-undo-like.json")
  18. |> Poison.decode!()
  19. |> Map.put("object", reaction_activity.data["id"])
  20. |> Map.put("actor", user.ap_id)
  21. {:ok, activity} = Transmogrifier.handle_incoming(data)
  22. assert activity.actor == user.ap_id
  23. assert activity.data["id"] == data["id"]
  24. assert activity.data["type"] == "Undo"
  25. end
  26. test "it returns an error for incoming unlikes wihout a like activity" do
  27. user = insert(:user)
  28. {:ok, activity} = CommonAPI.post(user, %{status: "leave a like pls"})
  29. data =
  30. File.read!("test/fixtures/mastodon-undo-like.json")
  31. |> Poison.decode!()
  32. |> Map.put("object", activity.data["object"])
  33. assert Transmogrifier.handle_incoming(data) == :error
  34. end
  35. test "it works for incoming unlikes with an existing like activity" do
  36. user = insert(:user)
  37. {:ok, activity} = CommonAPI.post(user, %{status: "leave a like pls"})
  38. like_data =
  39. File.read!("test/fixtures/mastodon-like.json")
  40. |> Poison.decode!()
  41. |> Map.put("object", activity.data["object"])
  42. _liker = insert(:user, ap_id: like_data["actor"], local: false)
  43. {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
  44. data =
  45. File.read!("test/fixtures/mastodon-undo-like.json")
  46. |> Poison.decode!()
  47. |> Map.put("object", like_data)
  48. |> Map.put("actor", like_data["actor"])
  49. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  50. assert data["actor"] == "http://mastodon.example.org/users/admin"
  51. assert data["type"] == "Undo"
  52. assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
  53. assert data["object"] == "http://mastodon.example.org/users/admin#likes/2"
  54. note = Object.get_by_ap_id(like_data["object"])
  55. assert note.data["like_count"] == 0
  56. assert note.data["likes"] == []
  57. end
  58. test "it works for incoming unlikes with an existing like activity and a compact object" do
  59. user = insert(:user)
  60. {:ok, activity} = CommonAPI.post(user, %{status: "leave a like pls"})
  61. like_data =
  62. File.read!("test/fixtures/mastodon-like.json")
  63. |> Poison.decode!()
  64. |> Map.put("object", activity.data["object"])
  65. _liker = insert(:user, ap_id: like_data["actor"], local: false)
  66. {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
  67. data =
  68. File.read!("test/fixtures/mastodon-undo-like.json")
  69. |> Poison.decode!()
  70. |> Map.put("object", like_data["id"])
  71. |> Map.put("actor", like_data["actor"])
  72. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  73. assert data["actor"] == "http://mastodon.example.org/users/admin"
  74. assert data["type"] == "Undo"
  75. assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
  76. assert data["object"] == "http://mastodon.example.org/users/admin#likes/2"
  77. end
  78. test "it works for incoming unannounces with an existing notice" do
  79. user = insert(:user)
  80. {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
  81. announce_data =
  82. File.read!("test/fixtures/mastodon-announce.json")
  83. |> Poison.decode!()
  84. |> Map.put("object", activity.data["object"])
  85. _announcer = insert(:user, ap_id: announce_data["actor"], local: false)
  86. {:ok, %Activity{data: announce_data, local: false}} =
  87. Transmogrifier.handle_incoming(announce_data)
  88. data =
  89. File.read!("test/fixtures/mastodon-undo-announce.json")
  90. |> Poison.decode!()
  91. |> Map.put("object", announce_data)
  92. |> Map.put("actor", announce_data["actor"])
  93. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  94. assert data["type"] == "Undo"
  95. assert data["object"] ==
  96. "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
  97. end
  98. test "it works for incoming unfollows with an existing follow" do
  99. user = insert(:user)
  100. follow_data =
  101. File.read!("test/fixtures/mastodon-follow-activity.json")
  102. |> Poison.decode!()
  103. |> Map.put("object", user.ap_id)
  104. _follower = insert(:user, ap_id: follow_data["actor"], local: false)
  105. {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
  106. data =
  107. File.read!("test/fixtures/mastodon-unfollow-activity.json")
  108. |> Poison.decode!()
  109. |> Map.put("object", follow_data)
  110. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  111. assert data["type"] == "Undo"
  112. assert data["object"]["type"] == "Follow"
  113. assert data["object"]["object"] == user.ap_id
  114. assert data["actor"] == "http://mastodon.example.org/users/admin"
  115. refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
  116. end
  117. test "it works for incoming unblocks with an existing block" do
  118. user = insert(:user)
  119. block_data =
  120. File.read!("test/fixtures/mastodon-block-activity.json")
  121. |> Poison.decode!()
  122. |> Map.put("object", user.ap_id)
  123. _blocker = insert(:user, ap_id: block_data["actor"], local: false)
  124. {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
  125. data =
  126. File.read!("test/fixtures/mastodon-unblock-activity.json")
  127. |> Poison.decode!()
  128. |> Map.put("object", block_data)
  129. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  130. assert data["type"] == "Undo"
  131. assert data["object"] == block_data["id"]
  132. blocker = User.get_cached_by_ap_id(data["actor"])
  133. refute User.blocks?(blocker, user)
  134. end
  135. end