Merge branch 'bugfix/llal-object-containment' into 'develop'
[akkoma] / test / web / rich_media / parser_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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, "Found metadata was invalid or incomplete: %{}"}
63 end
64
65 test "parses ogp" do
66 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp") ==
67 {:ok,
68 %{
69 image: "http://ia.media-imdb.com/images/rock.jpg",
70 title: "The Rock",
71 description:
72 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
73 type: "video.movie",
74 url: "http://www.imdb.com/title/tt0117500/"
75 }}
76 end
77
78 test "falls back to <title> when ogp:title is missing" do
79 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp-missing-title") ==
80 {:ok,
81 %{
82 image: "http://ia.media-imdb.com/images/rock.jpg",
83 title: "The Rock (1996)",
84 description:
85 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
86 type: "video.movie",
87 url: "http://www.imdb.com/title/tt0117500/"
88 }}
89 end
90
91 test "parses twitter card" do
92 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/twitter-card") ==
93 {:ok,
94 %{
95 card: "summary",
96 site: "@flickr",
97 image: "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
98 title: "Small Island Developing States Photo Submission",
99 description: "View the album on Flickr."
100 }}
101 end
102
103 test "parses OEmbed" do
104 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/oembed") ==
105 {:ok,
106 %{
107 author_name: "‮‭‬bees‬",
108 author_url: "https://www.flickr.com/photos/bees/",
109 cache_age: 3600,
110 flickr_type: "photo",
111 height: "768",
112 html:
113 "<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>",
114 license: "All Rights Reserved",
115 license_id: 0,
116 provider_name: "Flickr",
117 provider_url: "https://www.flickr.com/",
118 thumbnail_height: 150,
119 thumbnail_url: "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
120 thumbnail_width: 150,
121 title: "Bacon Lollys",
122 type: "photo",
123 url: "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_b.jpg",
124 version: "1.0",
125 web_page: "https://www.flickr.com/photos/bees/2362225867/",
126 web_page_short_url: "https://flic.kr/p/4AK2sc",
127 width: "1024"
128 }}
129 end
130
131 test "rejects invalid OGP data" do
132 assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/malformed")
133 end
134 end