First
[anni] / test / pleroma / web / rich_media / parsers / twitter_card_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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   end
12
13   test "parses twitter card with only name attributes" do
14     html =
15       File.read!("test/fixtures/nypd-facial-recognition-children-teenagers3.html")
16       |> Floki.parse_document!()
17
18     assert TwitterCard.parse(html, %{}) ==
19              %{
20                "app:id:googleplay" => "com.nytimes.android",
21                "app:name:googleplay" => "NYTimes",
22                "app:url:googleplay" => "nytimes://reader/id/100000006583622",
23                "site" => nil,
24                "description" =>
25                  "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
26                "image" =>
27                  "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
28                "type" => "article",
29                "url" =>
30                  "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
31                "title" =>
32                  "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database."
33              }
34   end
35
36   test "parses twitter card with only property attributes" do
37     html =
38       File.read!("test/fixtures/nypd-facial-recognition-children-teenagers2.html")
39       |> Floki.parse_document!()
40
41     assert TwitterCard.parse(html, %{}) ==
42              %{
43                "card" => "summary_large_image",
44                "description" =>
45                  "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
46                "image" =>
47                  "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg",
48                "image:alt" => "",
49                "title" =>
50                  "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
51                "url" =>
52                  "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
53                "type" => "article"
54              }
55   end
56
57   test "parses twitter card with name & property attributes" do
58     html =
59       File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html")
60       |> Floki.parse_document!()
61
62     assert TwitterCard.parse(html, %{}) ==
63              %{
64                "app:id:googleplay" => "com.nytimes.android",
65                "app:name:googleplay" => "NYTimes",
66                "app:url:googleplay" => "nytimes://reader/id/100000006583622",
67                "card" => "summary_large_image",
68                "description" =>
69                  "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
70                "image" =>
71                  "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg",
72                "image:alt" => "",
73                "site" => nil,
74                "title" =>
75                  "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
76                "url" =>
77                  "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
78                "type" => "article"
79              }
80   end
81
82   test "respect only first title tag on the page" do
83     image_path =
84       "https://assets.atlasobscura.com/media/W1siZiIsInVwbG9hZHMvYXNzZXRzLzkwYzgyMzI4LThlMDUtNGRiNS05MDg3LTUzMGUxZTM5N2RmMmVkOTM5ZDM4MGM4OTIx" <>
85         "YTQ5MF9EQVIgZXhodW1hdGlvbiBvZiBNYXJnYXJldCBDb3JiaW4gZ3JhdmUgMTkyNi5qcGciXSxbInAiLCJjb252ZXJ0IiwiIl0sWyJwIiwiY29udmVydCIsIi1xdWFsaXR5IDgxIC1hdXRvLW9" <>
86         "yaWVudCJdLFsicCIsInRodW1iIiwiNjAweD4iXV0/DAR%20exhumation%20of%20Margaret%20Corbin%20grave%201926.jpg"
87
88     html =
89       File.read!("test/fixtures/margaret-corbin-grave-west-point.html") |> Floki.parse_document!()
90
91     assert TwitterCard.parse(html, %{}) ==
92              %{
93                "site" => "@atlasobscura",
94                "title" => "The Missing Grave of Margaret Corbin, Revolutionary War Veteran",
95                "card" => "summary_large_image",
96                "image" => image_path,
97                "description" =>
98                  "She's the only woman veteran honored with a monument at West Point. But where was she buried?",
99                "site_name" => "Atlas Obscura",
100                "type" => "article",
101                "url" => "http://www.atlasobscura.com/articles/margaret-corbin-grave-west-point"
102              }
103   end
104
105   test "takes first founded title in html head if there is html markup error" do
106     html =
107       File.read!("test/fixtures/nypd-facial-recognition-children-teenagers4.html")
108       |> Floki.parse_document!()
109
110     assert TwitterCard.parse(html, %{}) ==
111              %{
112                "site" => nil,
113                "title" =>
114                  "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
115                "app:id:googleplay" => "com.nytimes.android",
116                "app:name:googleplay" => "NYTimes",
117                "app:url:googleplay" => "nytimes://reader/id/100000006583622",
118                "description" =>
119                  "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
120                "image" =>
121                  "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
122                "type" => "article",
123                "url" =>
124                  "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html"
125              }
126   end
127 end