make search provider configurable
[akkoma] / test / pleroma / web / rich_media / parser_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
60 %{
61 method: :head,
62 url: "http://example.com/huge-page"
63 } ->
64 %Tesla.Env{
65 status: 200,
66 headers: [{"content-length", "2000001"}, {"content-type", "text/html"}]
67 }
68
69 %{
70 method: :head,
71 url: "http://example.com/pdf-file"
72 } ->
73 %Tesla.Env{
74 status: 200,
75 headers: [{"content-length", "1000000"}, {"content-type", "application/pdf"}]
76 }
77
78 %{method: :head} ->
79 %Tesla.Env{status: 404, body: "", headers: []}
80 end)
81
82 :ok
83 end
84
85 test "returns error when no metadata present" do
86 assert {:error, _} = Parser.parse("http://example.com/empty")
87 end
88
89 test "doesn't just add a title" do
90 assert {:error, {:invalid_metadata, _}} = Parser.parse("http://example.com/non-ogp")
91 end
92
93 test "parses ogp" do
94 assert Parser.parse("http://example.com/ogp") ==
95 {:ok,
96 %{
97 "image" => "http://ia.media-imdb.com/images/rock.jpg",
98 "title" => "The Rock",
99 "description" =>
100 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
101 "type" => "video.movie",
102 "url" => "http://example.com/ogp"
103 }}
104 end
105
106 test "falls back to <title> when ogp:title is missing" do
107 assert Parser.parse("http://example.com/ogp-missing-title") ==
108 {:ok,
109 %{
110 "image" => "http://ia.media-imdb.com/images/rock.jpg",
111 "title" => "The Rock (1996)",
112 "description" =>
113 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
114 "type" => "video.movie",
115 "url" => "http://example.com/ogp-missing-title"
116 }}
117 end
118
119 test "parses twitter card" do
120 assert Parser.parse("http://example.com/twitter-card") ==
121 {:ok,
122 %{
123 "card" => "summary",
124 "site" => "@flickr",
125 "image" => "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
126 "title" => "Small Island Developing States Photo Submission",
127 "description" => "View the album on Flickr.",
128 "url" => "http://example.com/twitter-card"
129 }}
130 end
131
132 test "parses OEmbed" do
133 assert Parser.parse("http://example.com/oembed") ==
134 {:ok,
135 %{
136 "author_name" => "\u202E\u202D\u202Cbees\u202C",
137 "author_url" => "https://www.flickr.com/photos/bees/",
138 "cache_age" => 3600,
139 "flickr_type" => "photo",
140 "height" => "768",
141 "html" =>
142 "<a data-flickr-embed=\"true\" 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><script async src=\"https://embedr.flickr.com/assets/client-code.js\" charset=\"utf-8\"></script>",
143 "license" => "All Rights Reserved",
144 "license_id" => 0,
145 "provider_name" => "Flickr",
146 "provider_url" => "https://www.flickr.com/",
147 "thumbnail_height" => 150,
148 "thumbnail_url" =>
149 "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
150 "thumbnail_width" => 150,
151 "title" => "Bacon Lollys",
152 "type" => "photo",
153 "url" => "http://example.com/oembed",
154 "version" => "1.0",
155 "web_page" => "https://www.flickr.com/photos/bees/2362225867/",
156 "web_page_short_url" => "https://flic.kr/p/4AK2sc",
157 "width" => "1024"
158 }}
159 end
160
161 test "rejects invalid OGP data" do
162 assert {:error, _} = Parser.parse("http://example.com/malformed")
163 end
164
165 test "returns error if getting page was not successful" do
166 assert {:error, :overload} = Parser.parse("http://example.com/error")
167 end
168
169 test "does a HEAD request to check if the body is too large" do
170 assert {:error, :body_too_large} = Parser.parse("http://example.com/huge-page")
171 end
172
173 test "does a HEAD request to check if the body is html" do
174 assert {:error, {:content_type, _}} = Parser.parse("http://example.com/pdf-file")
175 end
176 end