logo

pleroma

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

parser_test.exs (6249B)


  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.ParserTest do
  5. use ExUnit.Case, async: true
  6. alias Pleroma.Web.RichMedia.Parser
  7. setup do
  8. Tesla.Mock.mock(fn
  9. %{
  10. method: :get,
  11. url: "http://example.com/ogp"
  12. } ->
  13. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
  14. %{
  15. method: :get,
  16. url: "http://example.com/non-ogp"
  17. } ->
  18. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/non_ogp_embed.html")}
  19. %{
  20. method: :get,
  21. url: "http://example.com/ogp-missing-title"
  22. } ->
  23. %Tesla.Env{
  24. status: 200,
  25. body: File.read!("test/fixtures/rich_media/ogp-missing-title.html")
  26. }
  27. %{
  28. method: :get,
  29. url: "http://example.com/twitter-card"
  30. } ->
  31. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
  32. %{
  33. method: :get,
  34. url: "http://example.com/oembed"
  35. } ->
  36. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.html")}
  37. %{
  38. method: :get,
  39. url: "http://example.com/oembed.json"
  40. } ->
  41. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.json")}
  42. %{method: :get, url: "http://example.com/empty"} ->
  43. %Tesla.Env{status: 200, body: "hello"}
  44. %{method: :get, url: "http://example.com/malformed"} ->
  45. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/malformed-data.html")}
  46. %{method: :get, url: "http://example.com/error"} ->
  47. {:error, :overload}
  48. %{
  49. method: :head,
  50. url: "http://example.com/huge-page"
  51. } ->
  52. %Tesla.Env{
  53. status: 200,
  54. headers: [{"content-length", "2000001"}, {"content-type", "text/html"}]
  55. }
  56. %{
  57. method: :head,
  58. url: "http://example.com/pdf-file"
  59. } ->
  60. %Tesla.Env{
  61. status: 200,
  62. headers: [{"content-length", "1000000"}, {"content-type", "application/pdf"}]
  63. }
  64. %{method: :head} ->
  65. %Tesla.Env{status: 404, body: "", headers: []}
  66. end)
  67. :ok
  68. end
  69. test "returns error when no metadata present" do
  70. assert {:error, _} = Parser.parse("http://example.com/empty")
  71. end
  72. test "doesn't just add a title" do
  73. assert {:error, {:invalid_metadata, _}} = Parser.parse("http://example.com/non-ogp")
  74. end
  75. test "parses ogp" do
  76. assert Parser.parse("http://example.com/ogp") ==
  77. {:ok,
  78. %{
  79. "image" => "http://ia.media-imdb.com/images/rock.jpg",
  80. "title" => "The Rock",
  81. "description" =>
  82. "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
  83. "type" => "video.movie",
  84. "url" => "http://example.com/ogp"
  85. }}
  86. end
  87. test "falls back to <title> when ogp:title is missing" do
  88. assert Parser.parse("http://example.com/ogp-missing-title") ==
  89. {:ok,
  90. %{
  91. "image" => "http://ia.media-imdb.com/images/rock.jpg",
  92. "title" => "The Rock (1996)",
  93. "description" =>
  94. "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
  95. "type" => "video.movie",
  96. "url" => "http://example.com/ogp-missing-title"
  97. }}
  98. end
  99. test "parses twitter card" do
  100. assert Parser.parse("http://example.com/twitter-card") ==
  101. {:ok,
  102. %{
  103. "card" => "summary",
  104. "site" => "@flickr",
  105. "image" => "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
  106. "title" => "Small Island Developing States Photo Submission",
  107. "description" => "View the album on Flickr.",
  108. "url" => "http://example.com/twitter-card"
  109. }}
  110. end
  111. test "parses OEmbed" do
  112. assert Parser.parse("http://example.com/oembed") ==
  113. {:ok,
  114. %{
  115. "author_name" => "‮‭‬bees‬",
  116. "author_url" => "https://www.flickr.com/photos/bees/",
  117. "cache_age" => 3600,
  118. "flickr_type" => "photo",
  119. "height" => "768",
  120. "html" =>
  121. "<a data-flickr-embed=\"true\" href=\"https://www.flickr.com/photos/bees/2362225867/\" title=\"Bacon Lollys by ‮‭‬bees‬, on Flickr\"><img src=\"https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_b.jpg\" width=\"1024\" height=\"768\" alt=\"Bacon Lollys\"></a><script async src=\"https://embedr.flickr.com/assets/client-code.js\" charset=\"utf-8\"></script>",
  122. "license" => "All Rights Reserved",
  123. "license_id" => 0,
  124. "provider_name" => "Flickr",
  125. "provider_url" => "https://www.flickr.com/",
  126. "thumbnail_height" => 150,
  127. "thumbnail_url" =>
  128. "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
  129. "thumbnail_width" => 150,
  130. "title" => "Bacon Lollys",
  131. "type" => "photo",
  132. "url" => "http://example.com/oembed",
  133. "version" => "1.0",
  134. "web_page" => "https://www.flickr.com/photos/bees/2362225867/",
  135. "web_page_short_url" => "https://flic.kr/p/4AK2sc",
  136. "width" => "1024"
  137. }}
  138. end
  139. test "rejects invalid OGP data" do
  140. assert {:error, _} = Parser.parse("http://example.com/malformed")
  141. end
  142. test "returns error if getting page was not successful" do
  143. assert {:error, :overload} = Parser.parse("http://example.com/error")
  144. end
  145. test "does a HEAD request to check if the body is too large" do
  146. assert {:error, :body_too_large} = Parser.parse("http://example.com/huge-page")
  147. end
  148. test "does a HEAD request to check if the body is html" do
  149. assert {:error, {:content_type, _}} = Parser.parse("http://example.com/pdf-file")
  150. end
  151. end