2016-08-04 18:08:32 +03:00
|
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
2016-08-16 16:53:18 +03:00
|
|
|
|
from synapse.rest.media.v1.preview_url_resource import (
|
2018-07-09 09:09:20 +03:00
|
|
|
|
decode_and_calc_og,
|
2021-02-08 20:33:30 +03:00
|
|
|
|
get_html_media_encoding,
|
2018-07-09 09:09:20 +03:00
|
|
|
|
summarize_paragraphs,
|
2016-08-16 16:53:18 +03:00
|
|
|
|
)
|
2016-08-04 18:08:32 +03:00
|
|
|
|
|
2018-07-09 09:09:20 +03:00
|
|
|
|
from . import unittest
|
|
|
|
|
|
2021-01-07 14:41:28 +03:00
|
|
|
|
try:
|
|
|
|
|
import lxml
|
|
|
|
|
except ImportError:
|
|
|
|
|
lxml = None
|
|
|
|
|
|
2016-08-04 18:08:32 +03:00
|
|
|
|
|
2021-02-08 20:33:30 +03:00
|
|
|
|
class SummarizeTestCase(unittest.TestCase):
|
2021-01-07 14:41:28 +03:00
|
|
|
|
if not lxml:
|
|
|
|
|
skip = "url preview feature requires lxml"
|
|
|
|
|
|
2016-08-04 18:08:32 +03:00
|
|
|
|
def test_long_summarize(self):
|
|
|
|
|
example_paras = [
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"""Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:
|
2016-08-04 18:08:32 +03:00
|
|
|
|
Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in
|
|
|
|
|
Troms county, Norway. The administrative centre of the municipality is
|
|
|
|
|
the city of Tromsø. Outside of Norway, Tromso and Tromsö are
|
|
|
|
|
alternative spellings of the city.Tromsø is considered the northernmost
|
|
|
|
|
city in the world with a population above 50,000. The most populous town
|
|
|
|
|
north of it is Alta, Norway, with a population of 14,272 (2013).""",
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"""Tromsø lies in Northern Norway. The municipality has a population of
|
2016-08-04 18:08:32 +03:00
|
|
|
|
(2015) 72,066, but with an annual influx of students it has over 75,000
|
|
|
|
|
most of the year. It is the largest urban area in Northern Norway and the
|
|
|
|
|
third largest north of the Arctic Circle (following Murmansk and Norilsk).
|
|
|
|
|
Most of Tromsø, including the city centre, is located on the island of
|
|
|
|
|
Tromsøya, 350 kilometres (217 mi) north of the Arctic Circle. In 2012,
|
|
|
|
|
Tromsøya had a population of 36,088. Substantial parts of the urban area
|
|
|
|
|
are also situated on the mainland to the east, and on parts of Kvaløya—a
|
|
|
|
|
large island to the west. Tromsøya is connected to the mainland by the Tromsø
|
|
|
|
|
Bridge and the Tromsøysund Tunnel, and to the island of Kvaløya by the
|
|
|
|
|
Sandnessund Bridge. Tromsø Airport connects the city to many destinations
|
|
|
|
|
in Europe. The city is warmer than most other places located on the same
|
|
|
|
|
latitude, due to the warming effect of the Gulf Stream.""",
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"""The city centre of Tromsø contains the highest number of old wooden
|
2016-08-04 18:08:32 +03:00
|
|
|
|
houses in Northern Norway, the oldest house dating from 1789. The Arctic
|
|
|
|
|
Cathedral, a modern church from 1965, is probably the most famous landmark
|
|
|
|
|
in Tromsø. The city is a cultural centre for its region, with several
|
|
|
|
|
festivals taking place in the summer. Some of Norway's best-known
|
|
|
|
|
musicians, Torbjørn Brundtland and Svein Berge of the electronica duo
|
|
|
|
|
Röyksopp and Lene Marlin grew up and started their careers in Tromsø.
|
|
|
|
|
Noted electronic musician Geir Jenssen also hails from Tromsø.""",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(
|
2016-08-04 18:08:32 +03:00
|
|
|
|
desc,
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
|
|
|
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
|
|
|
|
" Troms county, Norway. The administrative centre of the municipality is"
|
|
|
|
|
" the city of Tromsø. Outside of Norway, Tromso and Tromsö are"
|
|
|
|
|
" alternative spellings of the city.Tromsø is considered the northernmost"
|
|
|
|
|
" city in the world with a population above 50,000. The most populous town"
|
2018-08-10 16:54:09 +03:00
|
|
|
|
" north of it is Alta, Norway, with a population of 14,272 (2013).",
|
2016-08-04 18:08:32 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
desc = summarize_paragraphs(example_paras[1:], min_size=200, max_size=500)
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(
|
2016-08-04 18:08:32 +03:00
|
|
|
|
desc,
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"Tromsø lies in Northern Norway. The municipality has a population of"
|
|
|
|
|
" (2015) 72,066, but with an annual influx of students it has over 75,000"
|
|
|
|
|
" most of the year. It is the largest urban area in Northern Norway and the"
|
|
|
|
|
" third largest north of the Arctic Circle (following Murmansk and Norilsk)."
|
|
|
|
|
" Most of Tromsø, including the city centre, is located on the island of"
|
|
|
|
|
" Tromsøya, 350 kilometres (217 mi) north of the Arctic Circle. In 2012,"
|
2018-08-10 16:54:09 +03:00
|
|
|
|
" Tromsøya had a population of 36,088. Substantial parts of the urban…",
|
2016-08-04 18:08:32 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_short_summarize(self):
|
|
|
|
|
example_paras = [
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
|
|
|
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
|
|
|
|
" Troms county, Norway.",
|
|
|
|
|
"Tromsø lies in Northern Norway. The municipality has a population of"
|
|
|
|
|
" (2015) 72,066, but with an annual influx of students it has over 75,000"
|
|
|
|
|
" most of the year.",
|
|
|
|
|
"The city centre of Tromsø contains the highest number of old wooden"
|
|
|
|
|
" houses in Northern Norway, the oldest house dating from 1789. The Arctic"
|
|
|
|
|
" Cathedral, a modern church from 1965, is probably the most famous landmark"
|
|
|
|
|
" in Tromsø.",
|
2016-08-04 18:08:32 +03:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(
|
2016-08-04 18:08:32 +03:00
|
|
|
|
desc,
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
|
|
|
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
|
|
|
|
" Troms county, Norway.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Tromsø lies in Northern Norway. The municipality has a population of"
|
|
|
|
|
" (2015) 72,066, but with an annual influx of students it has over 75,000"
|
2018-08-10 16:54:09 +03:00
|
|
|
|
" most of the year.",
|
2016-08-04 18:08:32 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_small_then_large_summarize(self):
|
|
|
|
|
example_paras = [
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
|
|
|
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
|
|
|
|
" Troms county, Norway.",
|
|
|
|
|
"Tromsø lies in Northern Norway. The municipality has a population of"
|
|
|
|
|
" (2015) 72,066, but with an annual influx of students it has over 75,000"
|
|
|
|
|
" most of the year."
|
|
|
|
|
" The city centre of Tromsø contains the highest number of old wooden"
|
|
|
|
|
" houses in Northern Norway, the oldest house dating from 1789. The Arctic"
|
|
|
|
|
" Cathedral, a modern church from 1965, is probably the most famous landmark"
|
|
|
|
|
" in Tromsø.",
|
2016-08-04 18:08:32 +03:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(
|
2016-08-04 18:08:32 +03:00
|
|
|
|
desc,
|
2016-12-05 17:39:54 +03:00
|
|
|
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
|
|
|
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
|
|
|
|
" Troms county, Norway.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Tromsø lies in Northern Norway. The municipality has a population of"
|
|
|
|
|
" (2015) 72,066, but with an annual influx of students it has over 75,000"
|
|
|
|
|
" most of the year. The city centre of Tromsø contains the highest number"
|
|
|
|
|
" of old wooden houses in Northern Norway, the oldest house dating from"
|
2018-08-10 16:54:09 +03:00
|
|
|
|
" 1789. The Arctic Cathedral, a modern church from…",
|
2016-08-04 18:08:32 +03:00
|
|
|
|
)
|
2016-08-16 16:53:18 +03:00
|
|
|
|
|
|
|
|
|
|
2021-02-08 20:33:30 +03:00
|
|
|
|
class CalcOgTestCase(unittest.TestCase):
|
2021-01-07 14:41:28 +03:00
|
|
|
|
if not lxml:
|
|
|
|
|
skip = "url preview feature requires lxml"
|
|
|
|
|
|
2016-08-16 16:53:18 +03:00
|
|
|
|
def test_simple(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2016-08-16 16:53:18 +03:00
|
|
|
|
<html>
|
|
|
|
|
<head><title>Foo</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
Some text.
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(og, {"og:title": "Foo", "og:description": "Some text."})
|
2016-08-16 16:53:18 +03:00
|
|
|
|
|
|
|
|
|
def test_comment(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2016-08-16 16:53:18 +03:00
|
|
|
|
<html>
|
|
|
|
|
<head><title>Foo</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
<!-- HTML comment -->
|
|
|
|
|
Some text.
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(og, {"og:title": "Foo", "og:description": "Some text."})
|
2016-08-16 16:53:18 +03:00
|
|
|
|
|
|
|
|
|
def test_comment2(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2016-08-16 16:53:18 +03:00
|
|
|
|
<html>
|
|
|
|
|
<head><title>Foo</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
Some text.
|
|
|
|
|
<!-- HTML comment -->
|
|
|
|
|
Some more text.
|
|
|
|
|
<p>Text</p>
|
|
|
|
|
More text
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(
|
2018-08-10 16:54:09 +03:00
|
|
|
|
og,
|
|
|
|
|
{
|
|
|
|
|
"og:title": "Foo",
|
|
|
|
|
"og:description": "Some text.\n\nSome more text.\n\nText\n\nMore text",
|
|
|
|
|
},
|
|
|
|
|
)
|
2016-08-16 16:53:18 +03:00
|
|
|
|
|
|
|
|
|
def test_script(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2016-08-16 16:53:18 +03:00
|
|
|
|
<html>
|
|
|
|
|
<head><title>Foo</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
<script> (function() {})() </script>
|
|
|
|
|
Some text.
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(og, {"og:title": "Foo", "og:description": "Some text."})
|
2016-12-15 00:38:18 +03:00
|
|
|
|
|
|
|
|
|
def test_missing_title(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2016-12-15 00:38:18 +03:00
|
|
|
|
<html>
|
|
|
|
|
<body>
|
|
|
|
|
Some text.
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(og, {"og:title": None, "og:description": "Some text."})
|
2016-12-15 00:38:18 +03:00
|
|
|
|
|
|
|
|
|
def test_h1_as_title(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2016-12-15 00:38:18 +03:00
|
|
|
|
<html>
|
|
|
|
|
<meta property="og:description" content="Some text."/>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>Title</h1>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(og, {"og:title": "Title", "og:description": "Some text."})
|
2016-12-15 00:38:18 +03:00
|
|
|
|
|
|
|
|
|
def test_missing_title_and_broken_h1(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2016-12-15 00:38:18 +03:00
|
|
|
|
<html>
|
|
|
|
|
<body>
|
|
|
|
|
<h1><a href="foo"/></h1>
|
|
|
|
|
Some text.
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
|
2020-12-07 18:00:08 +03:00
|
|
|
|
self.assertEqual(og, {"og:title": None, "og:description": "Some text."})
|
|
|
|
|
|
|
|
|
|
def test_empty(self):
|
2021-02-08 20:33:30 +03:00
|
|
|
|
"""Test a body with no data in it."""
|
|
|
|
|
html = b""
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
self.assertEqual(og, {})
|
|
|
|
|
|
|
|
|
|
def test_no_tree(self):
|
|
|
|
|
"""A valid body with no tree in it."""
|
|
|
|
|
html = b"\x00"
|
2020-12-07 18:00:08 +03:00
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
self.assertEqual(og, {})
|
2021-01-26 15:32:17 +03:00
|
|
|
|
|
|
|
|
|
def test_invalid_encoding(self):
|
|
|
|
|
"""An invalid character encoding should be ignored and treated as UTF-8, if possible."""
|
2021-02-08 20:33:30 +03:00
|
|
|
|
html = b"""
|
2021-01-26 15:32:17 +03:00
|
|
|
|
<html>
|
|
|
|
|
<head><title>Foo</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
Some text.
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
og = decode_and_calc_og(
|
|
|
|
|
html, "http://example.com/test.html", "invalid-encoding"
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(og, {"og:title": "Foo", "og:description": "Some text."})
|
|
|
|
|
|
|
|
|
|
def test_invalid_encoding2(self):
|
|
|
|
|
"""A body which doesn't match the sent character encoding."""
|
|
|
|
|
# Note that this contains an invalid UTF-8 sequence in the title.
|
|
|
|
|
html = b"""
|
|
|
|
|
<html>
|
|
|
|
|
<head><title>\xff\xff Foo</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
Some text.
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
|
|
|
|
self.assertEqual(og, {"og:title": "ÿÿ Foo", "og:description": "Some text."})
|
2021-02-08 20:33:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MediaEncodingTestCase(unittest.TestCase):
|
|
|
|
|
def test_meta_charset(self):
|
|
|
|
|
"""A character encoding is found via the meta tag."""
|
|
|
|
|
encoding = get_html_media_encoding(
|
|
|
|
|
b"""
|
|
|
|
|
<html>
|
|
|
|
|
<head><meta charset="ascii">
|
|
|
|
|
</head>
|
|
|
|
|
</html>
|
|
|
|
|
""",
|
|
|
|
|
"text/html",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(encoding, "ascii")
|
|
|
|
|
|
|
|
|
|
# A less well-formed version.
|
|
|
|
|
encoding = get_html_media_encoding(
|
|
|
|
|
b"""
|
|
|
|
|
<html>
|
|
|
|
|
<head>< meta charset = ascii>
|
|
|
|
|
</head>
|
|
|
|
|
</html>
|
|
|
|
|
""",
|
|
|
|
|
"text/html",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(encoding, "ascii")
|
|
|
|
|
|
|
|
|
|
def test_xml_encoding(self):
|
|
|
|
|
"""A character encoding is found via the meta tag."""
|
|
|
|
|
encoding = get_html_media_encoding(
|
|
|
|
|
b"""
|
|
|
|
|
<?xml version="1.0" encoding="ascii"?>
|
|
|
|
|
<html>
|
|
|
|
|
</html>
|
|
|
|
|
""",
|
|
|
|
|
"text/html",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(encoding, "ascii")
|
|
|
|
|
|
|
|
|
|
def test_meta_xml_encoding(self):
|
|
|
|
|
"""Meta tags take precedence over XML encoding."""
|
|
|
|
|
encoding = get_html_media_encoding(
|
|
|
|
|
b"""
|
|
|
|
|
<?xml version="1.0" encoding="ascii"?>
|
|
|
|
|
<html>
|
|
|
|
|
<head><meta charset="UTF-16">
|
|
|
|
|
</head>
|
|
|
|
|
</html>
|
|
|
|
|
""",
|
|
|
|
|
"text/html",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(encoding, "UTF-16")
|
|
|
|
|
|
|
|
|
|
def test_content_type(self):
|
|
|
|
|
"""A character encoding is found via the Content-Type header."""
|
|
|
|
|
# Test a few variations of the header.
|
|
|
|
|
headers = (
|
|
|
|
|
'text/html; charset="ascii";',
|
|
|
|
|
"text/html;charset=ascii;",
|
|
|
|
|
'text/html; charset="ascii"',
|
|
|
|
|
"text/html; charset=ascii",
|
|
|
|
|
'text/html; charset="ascii;',
|
|
|
|
|
'text/html; charset=ascii";',
|
|
|
|
|
)
|
|
|
|
|
for header in headers:
|
|
|
|
|
encoding = get_html_media_encoding(b"", header)
|
|
|
|
|
self.assertEqual(encoding, "ascii")
|
|
|
|
|
|
|
|
|
|
def test_fallback(self):
|
|
|
|
|
"""A character encoding cannot be found in the body or header."""
|
|
|
|
|
encoding = get_html_media_encoding(b"", "text/html")
|
|
|
|
|
self.assertEqual(encoding, "utf-8")
|