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