logo

pleroma

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

parser_test.exs (4242B)


  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, async: false
  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. test "returns error when no metadata present" do
  12. assert {:error, _} = Parser.parse("https://example.com/empty")
  13. end
  14. test "doesn't just add a title" do
  15. assert {:error, {:invalid_metadata, _}} = Parser.parse("https://example.com/non-ogp")
  16. end
  17. test "parses ogp" do
  18. assert Parser.parse("https://example.com/ogp") ==
  19. {:ok,
  20. %{
  21. "image" => "http://ia.media-imdb.com/images/rock.jpg",
  22. "title" => "The Rock",
  23. "description" =>
  24. "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
  25. "type" => "video.movie",
  26. "url" => "https://example.com/ogp"
  27. }}
  28. end
  29. test "falls back to <title> when ogp:title is missing" do
  30. assert Parser.parse("https://example.com/ogp-missing-title") ==
  31. {:ok,
  32. %{
  33. "image" => "http://ia.media-imdb.com/images/rock.jpg",
  34. "title" => "The Rock (1996)",
  35. "description" =>
  36. "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
  37. "type" => "video.movie",
  38. "url" => "https://example.com/ogp-missing-title"
  39. }}
  40. end
  41. test "parses twitter card" do
  42. assert Parser.parse("https://example.com/twitter-card") ==
  43. {:ok,
  44. %{
  45. "card" => "summary",
  46. "site" => "@flickr",
  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, :overload} = 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. end