logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

add_remove_handling_test.exs (5294B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.Transmogrifier.AddRemoveHandlingTest do
  5. use Oban.Testing, repo: Pleroma.Repo
  6. use Pleroma.DataCase, async: true
  7. require Pleroma.Constants
  8. import Pleroma.Factory
  9. alias Pleroma.User
  10. alias Pleroma.Web.ActivityPub.Transmogrifier
  11. test "it accepts Add/Remove activities" do
  12. user =
  13. "test/fixtures/users_mock/user.json"
  14. |> File.read!()
  15. |> String.replace("{{nickname}}", "lain")
  16. object_id = "c61d6733-e256-4fe1-ab13-1e369789423f"
  17. object =
  18. "test/fixtures/statuses/note.json"
  19. |> File.read!()
  20. |> String.replace("{{nickname}}", "lain")
  21. |> String.replace("{{object_id}}", object_id)
  22. object_url = "https://example.com/objects/#{object_id}"
  23. actor = "https://example.com/users/lain"
  24. Tesla.Mock.mock(fn
  25. %{
  26. method: :get,
  27. url: ^actor
  28. } ->
  29. %Tesla.Env{
  30. status: 200,
  31. body: user,
  32. headers: [{"content-type", "application/activity+json"}]
  33. }
  34. %{
  35. method: :get,
  36. url: ^object_url
  37. } ->
  38. %Tesla.Env{
  39. status: 200,
  40. body: object,
  41. headers: [{"content-type", "application/activity+json"}]
  42. }
  43. %{method: :get, url: "https://example.com/users/lain/collections/featured"} ->
  44. %Tesla.Env{
  45. status: 200,
  46. body:
  47. "test/fixtures/users_mock/masto_featured.json"
  48. |> File.read!()
  49. |> String.replace("{{domain}}", "example.com")
  50. |> String.replace("{{nickname}}", "lain"),
  51. headers: [{"content-type", "application/activity+json"}]
  52. }
  53. end)
  54. message = %{
  55. "id" => "https://example.com/objects/d61d6733-e256-4fe1-ab13-1e369789423f",
  56. "actor" => actor,
  57. "object" => object_url,
  58. "target" => "https://example.com/users/lain/collections/featured",
  59. "type" => "Add",
  60. "to" => [Pleroma.Constants.as_public()],
  61. "cc" => ["https://example.com/users/lain/followers"],
  62. "bcc" => [],
  63. "bto" => []
  64. }
  65. assert {:ok, activity} = Transmogrifier.handle_incoming(message)
  66. assert activity.data == message
  67. user = User.get_cached_by_ap_id(actor)
  68. assert user.pinned_objects[object_url]
  69. remove = %{
  70. "id" => "http://localhost:400/objects/d61d6733-e256-4fe1-ab13-1e369789423d",
  71. "actor" => actor,
  72. "object" => object_url,
  73. "target" => "https://example.com/users/lain/collections/featured",
  74. "type" => "Remove",
  75. "to" => [Pleroma.Constants.as_public()],
  76. "cc" => ["https://example.com/users/lain/followers"],
  77. "bcc" => [],
  78. "bto" => []
  79. }
  80. assert {:ok, activity} = Transmogrifier.handle_incoming(remove)
  81. assert activity.data == remove
  82. user = refresh_record(user)
  83. refute user.pinned_objects[object_url]
  84. end
  85. test "Add/Remove activities for remote users without featured address" do
  86. user = insert(:user, local: false, domain: "example.com")
  87. user =
  88. user
  89. |> Ecto.Changeset.change(featured_address: nil)
  90. |> Repo.update!()
  91. %{host: host} = URI.parse(user.ap_id)
  92. user_data =
  93. "test/fixtures/users_mock/user.json"
  94. |> File.read!()
  95. |> String.replace("{{nickname}}", user.nickname)
  96. object_id = "c61d6733-e256-4fe1-ab13-1e369789423f"
  97. object =
  98. "test/fixtures/statuses/note.json"
  99. |> File.read!()
  100. |> String.replace("{{nickname}}", user.nickname)
  101. |> String.replace("{{object_id}}", object_id)
  102. object_url = "https://#{host}/objects/#{object_id}"
  103. actor = "https://#{host}/users/#{user.nickname}"
  104. featured = "https://#{host}/users/#{user.nickname}/collections/featured"
  105. Tesla.Mock.mock(fn
  106. %{
  107. method: :get,
  108. url: ^actor
  109. } ->
  110. %Tesla.Env{
  111. status: 200,
  112. body: user_data,
  113. headers: [{"content-type", "application/activity+json"}]
  114. }
  115. %{
  116. method: :get,
  117. url: ^object_url
  118. } ->
  119. %Tesla.Env{
  120. status: 200,
  121. body: object,
  122. headers: [{"content-type", "application/activity+json"}]
  123. }
  124. %{method: :get, url: ^featured} ->
  125. %Tesla.Env{
  126. status: 200,
  127. body:
  128. "test/fixtures/users_mock/masto_featured.json"
  129. |> File.read!()
  130. |> String.replace("{{domain}}", "#{host}")
  131. |> String.replace("{{nickname}}", user.nickname),
  132. headers: [{"content-type", "application/activity+json"}]
  133. }
  134. end)
  135. message = %{
  136. "id" => "https://#{host}/objects/d61d6733-e256-4fe1-ab13-1e369789423f",
  137. "actor" => actor,
  138. "object" => object_url,
  139. "target" => "https://#{host}/users/#{user.nickname}/collections/featured",
  140. "type" => "Add",
  141. "to" => [Pleroma.Constants.as_public()],
  142. "cc" => ["https://#{host}/users/#{user.nickname}/followers"],
  143. "bcc" => [],
  144. "bto" => []
  145. }
  146. assert {:ok, activity} = Transmogrifier.handle_incoming(message)
  147. assert activity.data == message
  148. user = User.get_cached_by_ap_id(actor)
  149. assert user.pinned_objects[object_url]
  150. end
  151. end