logo

pleroma

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

fetcher_test.exs (7843B)


  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.Object.FetcherTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Config
  8. alias Pleroma.Object
  9. alias Pleroma.Object.Fetcher
  10. import Mock
  11. import Tesla.Mock
  12. setup do
  13. mock(fn
  14. %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
  15. %Tesla.Env{status: 410}
  16. %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
  17. %Tesla.Env{status: 404}
  18. env ->
  19. apply(HttpRequestMock, :request, [env])
  20. end)
  21. :ok
  22. end
  23. describe "error cases" do
  24. setup do
  25. mock(fn
  26. %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
  27. %Tesla.Env{
  28. status: 200,
  29. body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json")
  30. }
  31. %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
  32. %Tesla.Env{
  33. status: 200,
  34. body: File.read!("test/fixtures/fetch_mocks/eal.json")
  35. }
  36. %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
  37. %Tesla.Env{
  38. status: 200,
  39. body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json")
  40. }
  41. %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
  42. %Tesla.Env{
  43. status: 500
  44. }
  45. end)
  46. :ok
  47. end
  48. @tag capture_log: true
  49. test "it works when fetching the OP actor errors out" do
  50. # Here we simulate a case where the author of the OP can't be read
  51. assert {:ok, _} =
  52. Fetcher.fetch_object_from_id(
  53. "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
  54. )
  55. end
  56. end
  57. describe "max thread distance restriction" do
  58. @ap_id "http://mastodon.example.org/@admin/99541947525187367"
  59. setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
  60. test "it returns thread depth exceeded error if thread depth is exceeded" do
  61. Config.put([:instance, :federation_incoming_replies_max_depth], 0)
  62. assert {:error, "Max thread distance exceeded."} =
  63. Fetcher.fetch_object_from_id(@ap_id, depth: 1)
  64. end
  65. test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
  66. Config.put([:instance, :federation_incoming_replies_max_depth], 0)
  67. assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
  68. end
  69. test "it fetches object if requested depth does not exceed max thread depth" do
  70. Config.put([:instance, :federation_incoming_replies_max_depth], 10)
  71. assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
  72. end
  73. end
  74. describe "actor origin containment" do
  75. test "it rejects objects with a bogus origin" do
  76. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
  77. end
  78. test "it rejects objects when attributedTo is wrong (variant 1)" do
  79. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
  80. end
  81. test "it rejects objects when attributedTo is wrong (variant 2)" do
  82. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
  83. end
  84. end
  85. describe "fetching an object" do
  86. test "it fetches an object" do
  87. {:ok, object} =
  88. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  89. assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
  90. {:ok, object_again} =
  91. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  92. assert [attachment] = object.data["attachment"]
  93. assert is_list(attachment["url"])
  94. assert object == object_again
  95. end
  96. test "Return MRF reason when fetched status is rejected by one" do
  97. clear_config([:mrf_keyword, :reject], ["yeah"])
  98. clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
  99. assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
  100. Fetcher.fetch_object_from_id(
  101. "http://mastodon.example.org/@admin/99541947525187367"
  102. )
  103. end
  104. end
  105. describe "implementation quirks" do
  106. test "it can fetch plume articles" do
  107. {:ok, object} =
  108. Fetcher.fetch_object_from_id(
  109. "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
  110. )
  111. assert object
  112. end
  113. test "it can fetch peertube videos" do
  114. {:ok, object} =
  115. Fetcher.fetch_object_from_id(
  116. "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
  117. )
  118. assert object
  119. end
  120. test "it can fetch Mobilizon events" do
  121. {:ok, object} =
  122. Fetcher.fetch_object_from_id(
  123. "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
  124. )
  125. assert object
  126. end
  127. test "it can fetch Honk events" do
  128. {:ok, object} =
  129. Fetcher.fetch_object_from_id("https://honk.tedunangst.com/u/tedu/h/8dkPX284T8286Mm9HD")
  130. assert object
  131. end
  132. test "it can fetch wedistribute articles" do
  133. {:ok, object} =
  134. Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
  135. assert object
  136. end
  137. test "all objects with fake directions are rejected by the object fetcher" do
  138. assert {:error, _} =
  139. Fetcher.fetch_and_contain_remote_object_from_id(
  140. "https://info.pleroma.site/activity4.json"
  141. )
  142. end
  143. test "handle HTTP 410 Gone response" do
  144. assert {:error, "Object has been deleted"} ==
  145. Fetcher.fetch_and_contain_remote_object_from_id(
  146. "https://mastodon.example.org/users/userisgone"
  147. )
  148. end
  149. test "handle HTTP 404 response" do
  150. assert {:error, "Object has been deleted"} ==
  151. Fetcher.fetch_and_contain_remote_object_from_id(
  152. "https://mastodon.example.org/users/userisgone404"
  153. )
  154. end
  155. test "it can fetch pleroma polls with attachments" do
  156. {:ok, object} =
  157. Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
  158. assert object
  159. end
  160. end
  161. describe "pruning" do
  162. test "it can refetch pruned objects" do
  163. object_id = "http://mastodon.example.org/@admin/99541947525187367"
  164. {:ok, object} = Fetcher.fetch_object_from_id(object_id)
  165. assert object
  166. {:ok, _object} = Object.prune(object)
  167. refute Object.get_by_ap_id(object_id)
  168. {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
  169. assert object.data["id"] == object_two.data["id"]
  170. assert object.id != object_two.id
  171. end
  172. end
  173. describe "signed fetches" do
  174. setup do: clear_config([:activitypub, :sign_object_fetches])
  175. test_with_mock "it signs fetches when configured to do so",
  176. Pleroma.Signature,
  177. [:passthrough],
  178. [] do
  179. Config.put([:activitypub, :sign_object_fetches], true)
  180. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  181. assert called(Pleroma.Signature.sign(:_, :_))
  182. end
  183. test_with_mock "it doesn't sign fetches when not configured to do so",
  184. Pleroma.Signature,
  185. [:passthrough],
  186. [] do
  187. Config.put([:activitypub, :sign_object_fetches], false)
  188. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  189. refute called(Pleroma.Signature.sign(:_, :_))
  190. end
  191. end
  192. end