logo

pleroma

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

parser_test.exs (5096B)


  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.RichMedia.ParserTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Web.RichMedia.Parser
  7. import Tesla.Mock
  8. setup do
  9. mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  10. end
  11. setup_all do: clear_config([:rich_media, :enabled], true)
  12. test "returns error when no metadata present" do
  13. assert {:error, _} = Parser.parse("https://example.com/empty")
  14. end
  15. test "doesn't just add a title" do
  16. assert {:error, :invalid_metadata} = Parser.parse("https://example.com/non-ogp")
  17. end
  18. test "parses ogp" do
  19. assert Parser.parse("https://example.com/ogp") ==
  20. {:ok,
  21. %{
  22. "image" => "http://ia.media-imdb.com/images/rock.jpg",
  23. "title" => "The Rock",
  24. "description" =>
  25. "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
  26. "type" => "video.movie",
  27. "url" => "https://example.com/ogp"
  28. }}
  29. end
  30. test "falls back to <title> when ogp:title is missing" do
  31. assert Parser.parse("https://example.com/ogp-missing-title") ==
  32. {:ok,
  33. %{
  34. "image" => "http://ia.media-imdb.com/images/rock.jpg",
  35. "title" => "The Rock (1996)",
  36. "description" =>
  37. "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
  38. "type" => "video.movie",
  39. "url" => "https://example.com/ogp-missing-title"
  40. }}
  41. end
  42. test "parses twitter card" do
  43. assert Parser.parse("https://example.com/twitter-card") ==
  44. {:ok,
  45. %{
  46. "card" => "summary",
  47. "image" => "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
  48. "title" => "Small Island Developing States Photo Submission",
  49. "description" => "View the album on Flickr.",
  50. "url" => "https://example.com/twitter-card"
  51. }}
  52. end
  53. test "parses OEmbed and filters HTML tags" do
  54. assert Parser.parse("https://example.com/oembed") ==
  55. {:ok,
  56. %{
  57. "author_name" => "\u202E\u202D\u202Cbees\u202C",
  58. "author_url" => "https://www.flickr.com/photos/bees/",
  59. "cache_age" => 3600,
  60. "flickr_type" => "photo",
  61. "height" => "768",
  62. "html" =>
  63. "<a href=\"https://www.flickr.com/photos/bees/2362225867/\" title=\"Bacon Lollys by \u202E\u202D\u202Cbees\u202C, on Flickr\"><img src=\"https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_b.jpg\" width=\"1024\" height=\"768\" alt=\"Bacon Lollys\"/></a>",
  64. "license" => "All Rights Reserved",
  65. "license_id" => 0,
  66. "provider_name" => "Flickr",
  67. "provider_url" => "https://www.flickr.com/",
  68. "thumbnail_height" => 150,
  69. "thumbnail_url" =>
  70. "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
  71. "thumbnail_width" => 150,
  72. "title" => "Bacon Lollys",
  73. "type" => "photo",
  74. "url" => "https://example.com/oembed",
  75. "version" => "1.0",
  76. "web_page" => "https://www.flickr.com/photos/bees/2362225867/",
  77. "web_page_short_url" => "https://flic.kr/p/4AK2sc",
  78. "width" => "1024"
  79. }}
  80. end
  81. test "rejects invalid OGP data" do
  82. assert {:error, _} = Parser.parse("https://example.com/malformed")
  83. end
  84. test "returns error if getting page was not successful" do
  85. assert {:error, :get} = Parser.parse("https://example.com/error")
  86. end
  87. test "does a HEAD request to check if the body is too large" do
  88. assert {:error, :body_too_large} = Parser.parse("https://example.com/huge-page")
  89. end
  90. test "does a HEAD request to check if the body is html" do
  91. assert {:error, :content_type} = Parser.parse("https://example.com/pdf-file")
  92. end
  93. test "refuses to crawl incomplete URLs" do
  94. url = "example.com/ogp"
  95. assert {:error, :validate} == Parser.parse(url)
  96. end
  97. test "refuses to crawl malformed URLs" do
  98. url = "example.com[]/ogp"
  99. assert {:error, :validate} == Parser.parse(url)
  100. end
  101. test "refuses to crawl URLs of private network from posts" do
  102. [
  103. "http://127.0.0.1:4000/notice/9kCP7VNyPJXFOXDrgO",
  104. "https://10.111.10.1/notice/9kCP7V",
  105. "https://172.16.32.40/notice/9kCP7V",
  106. "https://192.168.10.40/notice/9kCP7V",
  107. "https://pleroma.local/notice/9kCP7V"
  108. ]
  109. |> Enum.each(fn url ->
  110. assert {:error, :validate} == Parser.parse(url)
  111. end)
  112. end
  113. test "returns error when disabled" do
  114. clear_config([:rich_media, :enabled], false)
  115. assert match?({:error, :rich_media_disabled}, Parser.parse("https://example.com/ogp"))
  116. end
  117. end