Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.0KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Object.FetcherTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Object
  8. alias Pleroma.Object.Fetcher
  9. import Tesla.Mock
  10. import Mock
  11. setup do
  12. mock(fn
  13. %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
  14. %Tesla.Env{status: 410}
  15. %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
  16. %Tesla.Env{status: 404}
  17. env ->
  18. apply(HttpRequestMock, :request, [env])
  19. end)
  20. :ok
  21. end
  22. describe "actor origin containment" do
  23. test_with_mock "it rejects objects with a bogus origin",
  24. Pleroma.Web.OStatus,
  25. [:passthrough],
  26. [] do
  27. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
  28. refute called(Pleroma.Web.OStatus.fetch_activity_from_url(:_))
  29. end
  30. test_with_mock "it rejects objects when attributedTo is wrong (variant 1)",
  31. Pleroma.Web.OStatus,
  32. [:passthrough],
  33. [] do
  34. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
  35. refute called(Pleroma.Web.OStatus.fetch_activity_from_url(:_))
  36. end
  37. test_with_mock "it rejects objects when attributedTo is wrong (variant 2)",
  38. Pleroma.Web.OStatus,
  39. [:passthrough],
  40. [] do
  41. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
  42. refute called(Pleroma.Web.OStatus.fetch_activity_from_url(:_))
  43. end
  44. end
  45. describe "fetching an object" do
  46. test "it fetches an object" do
  47. {:ok, object} =
  48. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  49. assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
  50. assert activity.data["id"]
  51. {:ok, object_again} =
  52. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  53. assert [attachment] = object.data["attachment"]
  54. assert is_list(attachment["url"])
  55. assert object == object_again
  56. end
  57. test "it works with objects only available via Ostatus" do
  58. {:ok, object} = Fetcher.fetch_object_from_id("https://shitposter.club/notice/2827873")
  59. assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
  60. assert activity.data["id"]
  61. {:ok, object_again} = Fetcher.fetch_object_from_id("https://shitposter.club/notice/2827873")
  62. assert object == object_again
  63. end
  64. test "it correctly stitches up conversations between ostatus and ap" do
  65. last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
  66. {:ok, object} = Fetcher.fetch_object_from_id(last)
  67. object = Object.get_by_ap_id(object.data["inReplyTo"])
  68. assert object
  69. end
  70. end
  71. describe "implementation quirks" do
  72. test "it can fetch plume articles" do
  73. {:ok, object} =
  74. Fetcher.fetch_object_from_id(
  75. "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
  76. )
  77. assert object
  78. end
  79. test "it can fetch peertube videos" do
  80. {:ok, object} =
  81. Fetcher.fetch_object_from_id(
  82. "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
  83. )
  84. assert object
  85. end
  86. test "it can fetch wedistribute articles" do
  87. {:ok, object} =
  88. Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
  89. assert object
  90. end
  91. test "all objects with fake directions are rejected by the object fetcher" do
  92. assert {:error, _} =
  93. Fetcher.fetch_and_contain_remote_object_from_id(
  94. "https://info.pleroma.site/activity4.json"
  95. )
  96. end
  97. test "handle HTTP 410 Gone response" do
  98. assert {:error, "Object has been deleted"} ==
  99. Fetcher.fetch_and_contain_remote_object_from_id(
  100. "https://mastodon.example.org/users/userisgone"
  101. )
  102. end
  103. test "handle HTTP 404 response" do
  104. assert {:error, "Object has been deleted"} ==
  105. Fetcher.fetch_and_contain_remote_object_from_id(
  106. "https://mastodon.example.org/users/userisgone404"
  107. )
  108. end
  109. end
  110. describe "pruning" do
  111. test "it can refetch pruned objects" do
  112. object_id = "http://mastodon.example.org/@admin/99541947525187367"
  113. {:ok, object} = Fetcher.fetch_object_from_id(object_id)
  114. assert object
  115. {:ok, _object} = Object.prune(object)
  116. refute Object.get_by_ap_id(object_id)
  117. {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
  118. assert object.data["id"] == object_two.data["id"]
  119. assert object.id != object_two.id
  120. end
  121. end
  122. describe "signed fetches" do
  123. test_with_mock "it signs fetches when configured to do so",
  124. Pleroma.Signature,
  125. [:passthrough],
  126. [] do
  127. option = Pleroma.Config.get([:activitypub, :sign_object_fetches])
  128. Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
  129. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  130. assert called(Pleroma.Signature.sign(:_, :_))
  131. Pleroma.Config.put([:activitypub, :sign_object_fetches], option)
  132. end
  133. test_with_mock "it doesn't sign fetches when not configured to do so",
  134. Pleroma.Signature,
  135. [:passthrough],
  136. [] do
  137. option = Pleroma.Config.get([:activitypub, :sign_object_fetches])
  138. Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
  139. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  140. refute called(Pleroma.Signature.sign(:_, :_))
  141. Pleroma.Config.put([:activitypub, :sign_object_fetches], option)
  142. end
  143. end
  144. end