logo

pleroma

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

media_proxy_warming_policy_test.exs (2685B)


  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.MRF.MediaProxyWarmingPolicyTest do
  5. use ExUnit.Case
  6. use Pleroma.Tests.Helpers
  7. alias Pleroma.HTTP
  8. alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  9. alias Pleroma.Web.ActivityPub.MRF
  10. alias Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy
  11. import Mock
  12. import Mox
  13. @message %{
  14. "type" => "Create",
  15. "object" => %{
  16. "type" => "Note",
  17. "content" => "content",
  18. "attachment" => [
  19. %{"url" => [%{"href" => "http://example.com/image.jpg"}]}
  20. ]
  21. }
  22. }
  23. @message_with_history %{
  24. "type" => "Create",
  25. "object" => %{
  26. "type" => "Note",
  27. "content" => "content",
  28. "formerRepresentations" => %{
  29. "orderedItems" => [
  30. %{
  31. "type" => "Note",
  32. "content" => "content",
  33. "attachment" => [
  34. %{"url" => [%{"href" => "http://example.com/image.jpg"}]}
  35. ]
  36. }
  37. ]
  38. }
  39. }
  40. }
  41. setup do
  42. ConfigMock
  43. |> stub_with(Pleroma.Test.StaticConfig)
  44. :ok
  45. end
  46. setup do: clear_config([:media_proxy, :enabled], true)
  47. test "it prefetches media proxy URIs" do
  48. Tesla.Mock.mock(fn %{method: :get, url: "http://example.com/image.jpg"} ->
  49. {:ok, %Tesla.Env{status: 200, body: ""}}
  50. end)
  51. with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
  52. MediaProxyWarmingPolicy.filter(@message)
  53. assert called(HTTP.get(:_, :_, :_))
  54. end
  55. end
  56. test "it does nothing when no attachments are present" do
  57. object =
  58. @message["object"]
  59. |> Map.delete("attachment")
  60. message =
  61. @message
  62. |> Map.put("object", object)
  63. with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
  64. MediaProxyWarmingPolicy.filter(message)
  65. refute called(HTTP.get(:_, :_, :_))
  66. end
  67. end
  68. test "history-aware" do
  69. Tesla.Mock.mock(fn %{method: :get, url: "http://example.com/image.jpg"} ->
  70. {:ok, %Tesla.Env{status: 200, body: ""}}
  71. end)
  72. with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
  73. MRF.filter_one(MediaProxyWarmingPolicy, @message_with_history)
  74. assert called(HTTP.get(:_, :_, :_))
  75. end
  76. end
  77. test "works with Updates" do
  78. Tesla.Mock.mock(fn %{method: :get, url: "http://example.com/image.jpg"} ->
  79. {:ok, %Tesla.Env{status: 200, body: ""}}
  80. end)
  81. with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
  82. MRF.filter_one(MediaProxyWarmingPolicy, @message_with_history |> Map.put("type", "Update"))
  83. assert called(HTTP.get(:_, :_, :_))
  84. end
  85. end
  86. end