Merge branch 'develop' into activation-meta
[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 setup do
9 Tesla.Mock.mock(fn
10 %{
11 method: :get,
12 url: "http://example.com/ogp"
13 } ->
14 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
15
16 %{
17 method: :get,
18 url: "http://example.com/non-ogp"
19 } ->
20 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/non_ogp_embed.html")}
21
22 %{
23 method: :get,
24 url: "http://example.com/ogp-missing-title"
25 } ->
26 %Tesla.Env{
27 status: 200,
28 body: File.read!("test/fixtures/rich_media/ogp-missing-title.html")
29 }
30
31 %{
32 method: :get,
33 url: "http://example.com/twitter-card"
34 } ->
35 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
36
37 %{
38 method: :get,
39 url: "http://example.com/oembed"
40 } ->
41 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.html")}
42
43 %{
44 method: :get,
45 url: "http://example.com/oembed.json"
46 } ->
47 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.json")}
48
49 %{method: :get, url: "http://example.com/empty"} ->
50 %Tesla.Env{status: 200, body: "hello"}
51 end)
52
53 :ok
54 end
55
56 test "returns error when no metadata present" do
57 assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/empty")
58 end
59
60 test "doesn't just add a title" do
61 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/non-ogp") ==
62 {:error,
63 "Found metadata was invalid or incomplete: %{\"url\" => \"http://example.com/non-ogp\"}"}
64 end
65
66 test "parses ogp" do
67 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp") ==
68 {:ok,
69 %{
70 "image" => "http://ia.media-imdb.com/images/rock.jpg",
71 "title" => "The Rock",
72 "description" =>
73 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
74 "type" => "video.movie",
75 "url" => "http://example.com/ogp"
76 }}
77 end
78
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") ==
81 {:ok,
82 %{
83 "image" => "http://ia.media-imdb.com/images/rock.jpg",
84 "title" => "The Rock (1996)",
85 "description" =>
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"
89 }}
90 end
91
92 test "parses twitter card" do
93 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/twitter-card") ==
94 {:ok,
95 %{
96 "card" => "summary",
97 "site" => "@flickr",
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"
102 }}
103 end
104
105 test "parses OEmbed" do
106 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/oembed") ==
107 {:ok,
108 %{
109 "author_name" => "‮‭‬bees‬",
110 "author_url" => "https://www.flickr.com/photos/bees/",
111 "cache_age" => 3600,
112 "flickr_type" => "photo",
113 "height" => "768",
114 "html" =>
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",
117 "license_id" => 0,
118 "provider_name" => "Flickr",
119 "provider_url" => "https://www.flickr.com/",
120 "thumbnail_height" => 150,
121 "thumbnail_url" =>
122 "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
123 "thumbnail_width" => 150,
124 "title" => "Bacon Lollys",
125 "type" => "photo",
126 "url" => "http://example.com/oembed",
127 "version" => "1.0",
128 "web_page" => "https://www.flickr.com/photos/bees/2362225867/",
129 "web_page_short_url" => "https://flic.kr/p/4AK2sc",
130 "width" => "1024"
131 }}
132 end
133
134 test "rejects invalid OGP data" do
135 assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/malformed")
136 end
137 end