logo

pleroma

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

announce_handling_test.exs (5743B)


  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.AnnounceHandlingTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Object
  8. alias Pleroma.Web.ActivityPub.Transmogrifier
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. test "it works for incoming honk announces" do
  12. user = insert(:user, ap_id: "https://honktest/u/test", local: false)
  13. other_user = insert(:user)
  14. {:ok, post} = CommonAPI.post(other_user, %{status: "bonkeronk"})
  15. announce = %{
  16. "@context" => "https://www.w3.org/ns/activitystreams",
  17. "actor" => "https://honktest/u/test",
  18. "id" => "https://honktest/u/test/bonk/1793M7B9MQ48847vdx",
  19. "object" => post.data["object"],
  20. "published" => "2019-06-25T19:33:58Z",
  21. "to" => "https://www.w3.org/ns/activitystreams#Public",
  22. "type" => "Announce"
  23. }
  24. {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(announce)
  25. object = Object.get_by_ap_id(post.data["object"])
  26. assert length(object.data["announcements"]) == 1
  27. assert user.ap_id in object.data["announcements"]
  28. end
  29. test "it works for incoming announces with actor being inlined (kroeg)" do
  30. data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
  31. _user = insert(:user, local: false, ap_id: data["actor"]["id"])
  32. other_user = insert(:user)
  33. {:ok, post} = CommonAPI.post(other_user, %{status: "kroegeroeg"})
  34. data =
  35. data
  36. |> put_in(["object", "id"], post.data["object"])
  37. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  38. assert data["actor"] == "https://puckipedia.com/"
  39. end
  40. test "it works for incoming announces, fetching the announced object" do
  41. data =
  42. File.read!("test/fixtures/mastodon-announce.json")
  43. |> Poison.decode!()
  44. |> Map.put("object", "http://mastodon.example.org/users/admin/statuses/99541947525187367")
  45. Tesla.Mock.mock(fn
  46. %{method: :get} ->
  47. %Tesla.Env{status: 200, body: File.read!("test/fixtures/mastodon-note-object.json")}
  48. end)
  49. _user = insert(:user, local: false, ap_id: data["actor"])
  50. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  51. assert data["actor"] == "http://mastodon.example.org/users/admin"
  52. assert data["type"] == "Announce"
  53. assert data["id"] ==
  54. "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
  55. assert data["object"] ==
  56. "http://mastodon.example.org/users/admin/statuses/99541947525187367"
  57. assert(Activity.get_create_by_object_ap_id(data["object"]))
  58. end
  59. @tag capture_log: true
  60. test "it works for incoming announces with an existing activity" do
  61. user = insert(:user)
  62. {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
  63. data =
  64. File.read!("test/fixtures/mastodon-announce.json")
  65. |> Poison.decode!()
  66. |> Map.put("object", activity.data["object"])
  67. _user = insert(:user, local: false, ap_id: data["actor"])
  68. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  69. assert data["actor"] == "http://mastodon.example.org/users/admin"
  70. assert data["type"] == "Announce"
  71. assert data["id"] ==
  72. "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
  73. assert data["object"] == activity.data["object"]
  74. assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
  75. end
  76. # Ignore inlined activities for now
  77. @tag skip: true
  78. test "it works for incoming announces with an inlined activity" do
  79. data =
  80. File.read!("test/fixtures/mastodon-announce-private.json")
  81. |> Poison.decode!()
  82. _user =
  83. insert(:user,
  84. local: false,
  85. ap_id: data["actor"],
  86. follower_address: data["actor"] <> "/followers"
  87. )
  88. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  89. assert data["actor"] == "http://mastodon.example.org/users/admin"
  90. assert data["type"] == "Announce"
  91. assert data["id"] ==
  92. "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
  93. object = Object.normalize(data["object"])
  94. assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
  95. assert object.data["content"] == "this is a private toot"
  96. end
  97. @tag capture_log: true
  98. test "it rejects incoming announces with an inlined activity from another origin" do
  99. Tesla.Mock.mock(fn
  100. %{method: :get} -> %Tesla.Env{status: 404, body: ""}
  101. end)
  102. data =
  103. File.read!("test/fixtures/bogus-mastodon-announce.json")
  104. |> Poison.decode!()
  105. _user = insert(:user, local: false, ap_id: data["actor"])
  106. assert {:error, e} = Transmogrifier.handle_incoming(data)
  107. end
  108. test "it does not clobber the addressing on announce activities" do
  109. user = insert(:user)
  110. {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
  111. data =
  112. File.read!("test/fixtures/mastodon-announce.json")
  113. |> Poison.decode!()
  114. |> Map.put("object", Object.normalize(activity).data["id"])
  115. |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
  116. |> Map.put("cc", [])
  117. _user =
  118. insert(:user,
  119. local: false,
  120. ap_id: data["actor"],
  121. follower_address: "http://mastodon.example.org/users/admin/followers"
  122. )
  123. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  124. assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
  125. end
  126. end