1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.ParserTest do
6 use ExUnit.Case, async: true
8 alias Pleroma.Web.RichMedia.Parser
14 url: "http://example.com/ogp"
16 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
20 url: "http://example.com/non-ogp"
22 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/non_ogp_embed.html")}
26 url: "http://example.com/ogp-missing-title"
30 body: File.read!("test/fixtures/rich_media/ogp-missing-title.html")
35 url: "http://example.com/twitter-card"
37 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
41 url: "http://example.com/oembed"
43 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.html")}
47 url: "http://example.com/oembed.json"
49 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.json")}
51 %{method: :get, url: "http://example.com/empty"} ->
52 %Tesla.Env{status: 200, body: "hello"}
54 %{method: :get, url: "http://example.com/malformed"} ->
55 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/malformed-data.html")}
57 %{method: :get, url: "http://example.com/error"} ->
62 url: "http://example.com/huge-page"
66 headers: [{"content-length", "2000001"}, {"content-type", "text/html"}]
71 url: "http://example.com/pdf-file"
75 headers: [{"content-length", "1000000"}, {"content-type", "application/pdf"}]
79 %Tesla.Env{status: 404, body: "", headers: []}
85 test "returns error when no metadata present" do
86 assert {:error, _} = Parser.parse("http://example.com/empty")
89 test "doesn't just add a title" do
90 assert Parser.parse("http://example.com/non-ogp") ==
92 "Found metadata was invalid or incomplete: %{\"url\" => \"http://example.com/non-ogp\"}"}
96 assert Parser.parse("http://example.com/ogp") ==
99 "image" => "http://ia.media-imdb.com/images/rock.jpg",
100 "title" => "The Rock",
102 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
103 "type" => "video.movie",
104 "url" => "http://example.com/ogp"
108 test "falls back to <title> when ogp:title is missing" do
109 assert Parser.parse("http://example.com/ogp-missing-title") ==
112 "image" => "http://ia.media-imdb.com/images/rock.jpg",
113 "title" => "The Rock (1996)",
115 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
116 "type" => "video.movie",
117 "url" => "http://example.com/ogp-missing-title"
121 test "parses twitter card" do
122 assert Parser.parse("http://example.com/twitter-card") ==
127 "image" => "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
128 "title" => "Small Island Developing States Photo Submission",
129 "description" => "View the album on Flickr.",
130 "url" => "http://example.com/twitter-card"
134 test "parses OEmbed" do
135 assert Parser.parse("http://example.com/oembed") ==
138 "author_name" => "bees",
139 "author_url" => "https://www.flickr.com/photos/bees/",
141 "flickr_type" => "photo",
144 "<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>",
145 "license" => "All Rights Reserved",
147 "provider_name" => "Flickr",
148 "provider_url" => "https://www.flickr.com/",
149 "thumbnail_height" => 150,
151 "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
152 "thumbnail_width" => 150,
153 "title" => "Bacon Lollys",
155 "url" => "http://example.com/oembed",
157 "web_page" => "https://www.flickr.com/photos/bees/2362225867/",
158 "web_page_short_url" => "https://flic.kr/p/4AK2sc",
163 test "rejects invalid OGP data" do
164 assert {:error, _} = Parser.parse("http://example.com/malformed")
167 test "returns error if getting page was not successful" do
168 assert {:error, :overload} = Parser.parse("http://example.com/error")
171 test "does a HEAD request to check if the body is too large" do
172 assert {:error, :body_too_large} = Parser.parse("http://example.com/huge-page")
175 test "does a HEAD request to check if the body is html" do
176 assert {:error, {:content_type, _}} = Parser.parse("http://example.com/pdf-file")