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.

154 lines
4.5KB

  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 "it rejects objects with a bogus origin" do
  24. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
  25. end
  26. test "it rejects objects when attributedTo is wrong (variant 1)" do
  27. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
  28. end
  29. test "it rejects objects when attributedTo is wrong (variant 2)" do
  30. {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
  31. end
  32. end
  33. describe "fetching an object" do
  34. test "it fetches an object" do
  35. {:ok, object} =
  36. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  37. assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
  38. assert activity.data["id"]
  39. {:ok, object_again} =
  40. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  41. assert [attachment] = object.data["attachment"]
  42. assert is_list(attachment["url"])
  43. assert object == object_again
  44. end
  45. end
  46. describe "implementation quirks" do
  47. test "it can fetch plume articles" do
  48. {:ok, object} =
  49. Fetcher.fetch_object_from_id(
  50. "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
  51. )
  52. assert object
  53. end
  54. test "it can fetch peertube videos" do
  55. {:ok, object} =
  56. Fetcher.fetch_object_from_id(
  57. "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
  58. )
  59. assert object
  60. end
  61. test "it can fetch wedistribute articles" do
  62. {:ok, object} =
  63. Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
  64. assert object
  65. end
  66. test "all objects with fake directions are rejected by the object fetcher" do
  67. assert {:error, _} =
  68. Fetcher.fetch_and_contain_remote_object_from_id(
  69. "https://info.pleroma.site/activity4.json"
  70. )
  71. end
  72. test "handle HTTP 410 Gone response" do
  73. assert {:error, "Object has been deleted"} ==
  74. Fetcher.fetch_and_contain_remote_object_from_id(
  75. "https://mastodon.example.org/users/userisgone"
  76. )
  77. end
  78. test "handle HTTP 404 response" do
  79. assert {:error, "Object has been deleted"} ==
  80. Fetcher.fetch_and_contain_remote_object_from_id(
  81. "https://mastodon.example.org/users/userisgone404"
  82. )
  83. end
  84. end
  85. describe "pruning" do
  86. test "it can refetch pruned objects" do
  87. object_id = "http://mastodon.example.org/@admin/99541947525187367"
  88. {:ok, object} = Fetcher.fetch_object_from_id(object_id)
  89. assert object
  90. {:ok, _object} = Object.prune(object)
  91. refute Object.get_by_ap_id(object_id)
  92. {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
  93. assert object.data["id"] == object_two.data["id"]
  94. assert object.id != object_two.id
  95. end
  96. end
  97. describe "signed fetches" do
  98. clear_config([:activitypub, :sign_object_fetches])
  99. test_with_mock "it signs fetches when configured to do so",
  100. Pleroma.Signature,
  101. [:passthrough],
  102. [] do
  103. Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
  104. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  105. assert called(Pleroma.Signature.sign(:_, :_))
  106. end
  107. test_with_mock "it doesn't sign fetches when not configured to do so",
  108. Pleroma.Signature,
  109. [:passthrough],
  110. [] do
  111. Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
  112. Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
  113. refute called(Pleroma.Signature.sign(:_, :_))
  114. end
  115. end
  116. end