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