Merge branch 'feature/new-registrations-digest' into 'develop'
[akkoma] / test / web / rich_media / parsers / twitter_card_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.Parsers.TwitterCardTest do
6 use ExUnit.Case, async: true
7 alias Pleroma.Web.RichMedia.Parsers.TwitterCard
8
9 test "returns error when html not contains twitter card" do
10 assert TwitterCard.parse([{"html", [], [{"head", [], []}, {"body", [], []}]}], %{}) ==
11 {:error, "No twitter card metadata found"}
12 end
13
14 test "parses twitter card with only name attributes" do
15 html =
16 File.read!("test/fixtures/nypd-facial-recognition-children-teenagers3.html")
17 |> Floki.parse_document!()
18
19 assert TwitterCard.parse(html, %{}) ==
20 {:ok,
21 %{
22 "app:id:googleplay": "com.nytimes.android",
23 "app:name:googleplay": "NYTimes",
24 "app:url:googleplay": "nytimes://reader/id/100000006583622",
25 site: nil,
26 title:
27 "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database. - The New York Times"
28 }}
29 end
30
31 test "parses twitter card with only property attributes" do
32 html =
33 File.read!("test/fixtures/nypd-facial-recognition-children-teenagers2.html")
34 |> Floki.parse_document!()
35
36 assert TwitterCard.parse(html, %{}) ==
37 {:ok,
38 %{
39 card: "summary_large_image",
40 description:
41 "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
42 image:
43 "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg",
44 "image:alt": "",
45 title:
46 "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
47 url:
48 "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html"
49 }}
50 end
51
52 test "parses twitter card with name & property attributes" do
53 html =
54 File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html")
55 |> Floki.parse_document!()
56
57 assert TwitterCard.parse(html, %{}) ==
58 {:ok,
59 %{
60 "app:id:googleplay": "com.nytimes.android",
61 "app:name:googleplay": "NYTimes",
62 "app:url:googleplay": "nytimes://reader/id/100000006583622",
63 card: "summary_large_image",
64 description:
65 "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
66 image:
67 "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg",
68 "image:alt": "",
69 site: nil,
70 title:
71 "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
72 url:
73 "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html"
74 }}
75 end
76
77 test "respect only first title tag on the page" do
78 image_path =
79 "https://assets.atlasobscura.com/media/W1siZiIsInVwbG9hZHMvYXNzZXRzLzkwYzgyMzI4LThlMDUtNGRiNS05MDg3LTUzMGUxZTM5N2RmMmVkOTM5ZDM4MGM4OTIx" <>
80 "YTQ5MF9EQVIgZXhodW1hdGlvbiBvZiBNYXJnYXJldCBDb3JiaW4gZ3JhdmUgMTkyNi5qcGciXSxbInAiLCJjb252ZXJ0IiwiIl0sWyJwIiwiY29udmVydCIsIi1xdWFsaXR5IDgxIC1hdXRvLW9" <>
81 "yaWVudCJdLFsicCIsInRodW1iIiwiNjAweD4iXV0/DAR%20exhumation%20of%20Margaret%20Corbin%20grave%201926.jpg"
82
83 html =
84 File.read!("test/fixtures/margaret-corbin-grave-west-point.html") |> Floki.parse_document!()
85
86 assert TwitterCard.parse(html, %{}) ==
87 {:ok,
88 %{
89 site: "@atlasobscura",
90 title:
91 "The Missing Grave of Margaret Corbin, Revolutionary War Veteran - Atlas Obscura",
92 card: "summary_large_image",
93 image: image_path
94 }}
95 end
96
97 test "takes first founded title in html head if there is html markup error" do
98 html =
99 File.read!("test/fixtures/nypd-facial-recognition-children-teenagers4.html")
100 |> Floki.parse_document!()
101
102 assert TwitterCard.parse(html, %{}) ==
103 {:ok,
104 %{
105 site: nil,
106 title:
107 "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database. - The New York Times",
108 "app:id:googleplay": "com.nytimes.android",
109 "app:name:googleplay": "NYTimes",
110 "app:url:googleplay": "nytimes://reader/id/100000006583622"
111 }}
112 end
113 end