logo

pleroma

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

helpers_test.exs (3439B)


  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.RichMedia.HelpersTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Config
  7. alias Pleroma.Object
  8. alias Pleroma.Web.CommonAPI
  9. alias Pleroma.Web.RichMedia.Helpers
  10. import Pleroma.Factory
  11. import Tesla.Mock
  12. setup do
  13. mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  14. :ok
  15. end
  16. setup do: clear_config([:rich_media, :enabled])
  17. test "refuses to crawl incomplete URLs" do
  18. user = insert(:user)
  19. {:ok, activity} =
  20. CommonAPI.post(user, %{
  21. status: "[test](example.com/ogp)",
  22. content_type: "text/markdown"
  23. })
  24. Config.put([:rich_media, :enabled], true)
  25. assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  26. end
  27. test "refuses to crawl malformed URLs" do
  28. user = insert(:user)
  29. {:ok, activity} =
  30. CommonAPI.post(user, %{
  31. status: "[test](example.com[]/ogp)",
  32. content_type: "text/markdown"
  33. })
  34. Config.put([:rich_media, :enabled], true)
  35. assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  36. end
  37. test "crawls valid, complete URLs" do
  38. user = insert(:user)
  39. {:ok, activity} =
  40. CommonAPI.post(user, %{
  41. status: "[test](https://example.com/ogp)",
  42. content_type: "text/markdown"
  43. })
  44. Config.put([:rich_media, :enabled], true)
  45. assert %{page_url: "https://example.com/ogp", rich_media: _} =
  46. Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  47. end
  48. test "refuses to crawl URLs from posts marked sensitive" do
  49. user = insert(:user)
  50. {:ok, activity} =
  51. CommonAPI.post(user, %{
  52. status: "http://example.com/ogp",
  53. sensitive: true
  54. })
  55. %Object{} = object = Object.normalize(activity)
  56. assert object.data["sensitive"]
  57. Config.put([:rich_media, :enabled], true)
  58. assert %{} = Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  59. end
  60. test "refuses to crawl URLs from posts tagged NSFW" do
  61. user = insert(:user)
  62. {:ok, activity} =
  63. CommonAPI.post(user, %{
  64. status: "http://example.com/ogp #nsfw"
  65. })
  66. %Object{} = object = Object.normalize(activity)
  67. assert object.data["sensitive"]
  68. Config.put([:rich_media, :enabled], true)
  69. assert %{} = Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
  70. end
  71. test "refuses to crawl URLs of private network from posts" do
  72. user = insert(:user)
  73. {:ok, activity} =
  74. CommonAPI.post(user, %{status: "http://127.0.0.1:4000/notice/9kCP7VNyPJXFOXDrgO"})
  75. {:ok, activity2} = CommonAPI.post(user, %{status: "https://10.111.10.1/notice/9kCP7V"})
  76. {:ok, activity3} = CommonAPI.post(user, %{status: "https://172.16.32.40/notice/9kCP7V"})
  77. {:ok, activity4} = CommonAPI.post(user, %{status: "https://192.168.10.40/notice/9kCP7V"})
  78. {:ok, activity5} = CommonAPI.post(user, %{status: "https://pleroma.local/notice/9kCP7V"})
  79. Config.put([:rich_media, :enabled], true)
  80. assert %{} = Helpers.fetch_data_for_activity(activity)
  81. assert %{} = Helpers.fetch_data_for_activity(activity2)
  82. assert %{} = Helpers.fetch_data_for_activity(activity3)
  83. assert %{} = Helpers.fetch_data_for_activity(activity4)
  84. assert %{} = Helpers.fetch_data_for_activity(activity5)
  85. end
  86. end