logo

pleroma

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

media_proxy_cache_controller_test.exs (5525B)


  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.Web.AdminAPI.MediaProxyCacheControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. import Mock
  8. alias Pleroma.Web.MediaProxy
  9. setup do: clear_config([:media_proxy])
  10. setup do
  11. on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
  12. end
  13. setup do
  14. admin = insert(:user, is_admin: true)
  15. token = insert(:oauth_admin_token, user: admin)
  16. conn =
  17. build_conn()
  18. |> assign(:user, admin)
  19. |> assign(:token, token)
  20. Config.put([:media_proxy, :enabled], true)
  21. Config.put([:media_proxy, :invalidation, :enabled], true)
  22. Config.put([:media_proxy, :invalidation, :provider], MediaProxy.Invalidation.Script)
  23. {:ok, %{admin: admin, token: token, conn: conn}}
  24. end
  25. describe "GET /api/pleroma/admin/media_proxy_caches" do
  26. test "shows banned MediaProxy URLs", %{conn: conn} do
  27. MediaProxy.put_in_banned_urls([
  28. "http://localhost:4001/media/a688346.jpg",
  29. "http://localhost:4001/media/fb1f4d.jpg"
  30. ])
  31. MediaProxy.put_in_banned_urls("http://localhost:4001/media/gb1f44.jpg")
  32. MediaProxy.put_in_banned_urls("http://localhost:4001/media/tb13f47.jpg")
  33. MediaProxy.put_in_banned_urls("http://localhost:4001/media/wb1f46.jpg")
  34. response =
  35. conn
  36. |> get("/api/pleroma/admin/media_proxy_caches?page_size=2")
  37. |> json_response_and_validate_schema(200)
  38. assert response["page_size"] == 2
  39. assert response["count"] == 5
  40. assert response["urls"] == [
  41. "http://localhost:4001/media/fb1f4d.jpg",
  42. "http://localhost:4001/media/a688346.jpg"
  43. ]
  44. response =
  45. conn
  46. |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=2")
  47. |> json_response_and_validate_schema(200)
  48. assert response["urls"] == [
  49. "http://localhost:4001/media/gb1f44.jpg",
  50. "http://localhost:4001/media/tb13f47.jpg"
  51. ]
  52. assert response["page_size"] == 2
  53. assert response["count"] == 5
  54. response =
  55. conn
  56. |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&page=3")
  57. |> json_response_and_validate_schema(200)
  58. assert response["urls"] == ["http://localhost:4001/media/wb1f46.jpg"]
  59. end
  60. test "search banned MediaProxy URLs", %{conn: conn} do
  61. MediaProxy.put_in_banned_urls([
  62. "http://localhost:4001/media/a688346.jpg",
  63. "http://localhost:4001/media/ff44b1f4d.jpg"
  64. ])
  65. MediaProxy.put_in_banned_urls("http://localhost:4001/media/gb1f44.jpg")
  66. MediaProxy.put_in_banned_urls("http://localhost:4001/media/tb13f47.jpg")
  67. MediaProxy.put_in_banned_urls("http://localhost:4001/media/wb1f46.jpg")
  68. response =
  69. conn
  70. |> get("/api/pleroma/admin/media_proxy_caches?page_size=2&query=F44")
  71. |> json_response_and_validate_schema(200)
  72. assert response["urls"] == [
  73. "http://localhost:4001/media/gb1f44.jpg",
  74. "http://localhost:4001/media/ff44b1f4d.jpg"
  75. ]
  76. assert response["page_size"] == 2
  77. assert response["count"] == 2
  78. end
  79. end
  80. describe "POST /api/pleroma/admin/media_proxy_caches/delete" do
  81. test "deleted MediaProxy URLs from banned", %{conn: conn} do
  82. MediaProxy.put_in_banned_urls([
  83. "http://localhost:4001/media/a688346.jpg",
  84. "http://localhost:4001/media/fb1f4d.jpg"
  85. ])
  86. conn
  87. |> put_req_header("content-type", "application/json")
  88. |> post("/api/pleroma/admin/media_proxy_caches/delete", %{
  89. urls: ["http://localhost:4001/media/a688346.jpg"]
  90. })
  91. |> json_response_and_validate_schema(200)
  92. refute MediaProxy.in_banned_urls("http://localhost:4001/media/a688346.jpg")
  93. assert MediaProxy.in_banned_urls("http://localhost:4001/media/fb1f4d.jpg")
  94. end
  95. end
  96. describe "POST /api/pleroma/admin/media_proxy_caches/purge" do
  97. test "perform invalidates cache of MediaProxy", %{conn: conn} do
  98. urls = [
  99. "http://example.com/media/a688346.jpg",
  100. "http://example.com/media/fb1f4d.jpg"
  101. ]
  102. with_mocks [
  103. {MediaProxy.Invalidation.Script, [],
  104. [
  105. purge: fn _, _ -> {"ok", 0} end
  106. ]}
  107. ] do
  108. conn
  109. |> put_req_header("content-type", "application/json")
  110. |> post("/api/pleroma/admin/media_proxy_caches/purge", %{urls: urls, ban: false})
  111. |> json_response_and_validate_schema(200)
  112. refute MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
  113. refute MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")
  114. end
  115. end
  116. test "perform invalidates cache of MediaProxy and adds url to banned", %{conn: conn} do
  117. urls = [
  118. "http://example.com/media/a688346.jpg",
  119. "http://example.com/media/fb1f4d.jpg"
  120. ]
  121. with_mocks [{MediaProxy.Invalidation.Script, [], [purge: fn _, _ -> {"ok", 0} end]}] do
  122. conn
  123. |> put_req_header("content-type", "application/json")
  124. |> post(
  125. "/api/pleroma/admin/media_proxy_caches/purge",
  126. %{urls: urls, ban: true}
  127. )
  128. |> json_response_and_validate_schema(200)
  129. assert MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
  130. assert MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")
  131. end
  132. end
  133. end
  134. end