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