Merge branch 'fix/ogp-title' into 'develop'
[akkoma] / test / web / rich_media / parser_test.exs
1 defmodule Pleroma.Web.RichMedia.ParserTest do
2 use ExUnit.Case, async: true
3
4 setup do
5 Tesla.Mock.mock(fn
6 %{
7 method: :get,
8 url: "http://example.com/ogp"
9 } ->
10 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}
11
12 %{
13 method: :get,
14 url: "http://example.com/ogp-missing-title"
15 } ->
16 %Tesla.Env{
17 status: 200,
18 body: File.read!("test/fixtures/rich_media/ogp-missing-title.html")
19 }
20
21 %{
22 method: :get,
23 url: "http://example.com/twitter-card"
24 } ->
25 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
26
27 %{
28 method: :get,
29 url: "http://example.com/oembed"
30 } ->
31 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.html")}
32
33 %{
34 method: :get,
35 url: "http://example.com/oembed.json"
36 } ->
37 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.json")}
38
39 %{method: :get, url: "http://example.com/empty"} ->
40 %Tesla.Env{status: 200, body: "hello"}
41 end)
42
43 :ok
44 end
45
46 test "returns error when no metadata present" do
47 assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/empty")
48 end
49
50 test "parses ogp" do
51 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp") ==
52 {:ok,
53 %{
54 image: "http://ia.media-imdb.com/images/rock.jpg",
55 title: "The Rock",
56 description:
57 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
58 type: "video.movie",
59 url: "http://www.imdb.com/title/tt0117500/"
60 }}
61 end
62
63 test "falls back to <title> when ogp:title is missing" do
64 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/ogp-missing-title") ==
65 {:ok,
66 %{
67 image: "http://ia.media-imdb.com/images/rock.jpg",
68 title: "The Rock (1996)",
69 description:
70 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
71 type: "video.movie",
72 url: "http://www.imdb.com/title/tt0117500/"
73 }}
74 end
75
76 test "parses twitter card" do
77 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/twitter-card") ==
78 {:ok,
79 %{
80 card: "summary",
81 site: "@flickr",
82 image: "https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg",
83 title: "Small Island Developing States Photo Submission",
84 description: "View the album on Flickr."
85 }}
86 end
87
88 test "parses OEmbed" do
89 assert Pleroma.Web.RichMedia.Parser.parse("http://example.com/oembed") ==
90 {:ok,
91 %{
92 author_name: "‮‭‬bees‬",
93 author_url: "https://www.flickr.com/photos/bees/",
94 cache_age: 3600,
95 flickr_type: "photo",
96 height: "768",
97 html:
98 "<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>",
99 license: "All Rights Reserved",
100 license_id: 0,
101 provider_name: "Flickr",
102 provider_url: "https://www.flickr.com/",
103 thumbnail_height: 150,
104 thumbnail_url: "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
105 thumbnail_width: 150,
106 title: "Bacon Lollys",
107 type: "photo",
108 url: "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_b.jpg",
109 version: "1.0",
110 web_page: "https://www.flickr.com/photos/bees/2362225867/",
111 web_page_short_url: "https://flic.kr/p/4AK2sc",
112 width: "1024"
113 }}
114 end
115
116 test "rejects invalid OGP data" do
117 assert {:error, _} = Pleroma.Web.RichMedia.Parser.parse("http://example.com/malformed")
118 end
119 end