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
12 url: "http://example.com/ogp"
14 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
18 url: "http://example.com/non-ogp"
20 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/non_ogp_embed.html")}
24 url: "http://example.com/ogp-missing-title"
28 body: File.read!("test/fixtures/rich_media/ogp-missing-title.html")
33 url: "http://example.com/twitter-card"
35 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
39 url: "http://example.com/oembed"
41 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.html")}
45 url: "http://example.com/oembed.json"
47 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.json")}
49 %{method: :get, url: "http://example.com/empty"} ->
50 %Tesla.Env{status: 200, body: "hello"}
56 test "returns error when no metadata present" do
57 assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/empty")
60 test "doesn't just add a title" do
61 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/non-ogp") ==
63 "Found metadata was invalid or incomplete: %{\"url\" => \"http://example.com/non-ogp\"}"}
67 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp") ==
70 "image" => "http://ia.media-imdb.com/images/rock.jpg",
71 "title" => "The Rock",
73 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
74 "type" => "video.movie",
75 "url" => "http://example.com/ogp"
79 test "falls back to <title> when ogp:title is missing" do
80 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp-missing-title") ==
83 "image" => "http://ia.media-imdb.com/images/rock.jpg",
84 "title" => "The Rock (1996)",
86 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
87 "type" => "video.movie",
88 "url" => "http://example.com/ogp-missing-title"
92 test "parses twitter card" do
93 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/twitter-card") ==
98 "image" => "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
99 "title" => "Small Island Developing States Photo Submission",
100 "description" => "View the album on Flickr.",
101 "url" => "http://example.com/twitter-card"
105 test "parses OEmbed" do
106 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/oembed") ==
109 "author_name" => "bees",
110 "author_url" => "https://www.flickr.com/photos/bees/",
112 "flickr_type" => "photo",
115 "<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>",
116 "license" => "All Rights Reserved",
118 "provider_name" => "Flickr",
119 "provider_url" => "https://www.flickr.com/",
120 "thumbnail_height" => 150,
122 "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
123 "thumbnail_width" => 150,
124 "title" => "Bacon Lollys",
126 "url" => "http://example.com/oembed",
128 "web_page" => "https://www.flickr.com/photos/bees/2362225867/",
129 "web_page_short_url" => "https://flic.kr/p/4AK2sc",
134 test "rejects invalid OGP data" do
135 assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/malformed")