You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

163 lines
6.6 KiB

  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. from parameterized import parameterized
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.media.oembed import OEmbedProvider, OEmbedResult
  18. from synapse.server import HomeServer
  19. from synapse.types import JsonDict
  20. from synapse.util import Clock
  21. from tests.unittest import HomeserverTestCase
  22. try:
  23. import lxml
  24. except ImportError:
  25. lxml = None # type: ignore[assignment]
  26. class OEmbedTests(HomeserverTestCase):
  27. if not lxml:
  28. skip = "url preview feature requires lxml"
  29. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  30. self.oembed = OEmbedProvider(hs)
  31. def parse_response(self, response: JsonDict) -> OEmbedResult:
  32. return self.oembed.parse_oembed_response(
  33. "https://test", json.dumps(response).encode("utf-8")
  34. )
  35. def test_version(self) -> None:
  36. """Accept versions that are similar to 1.0 as a string or int (or missing)."""
  37. for version in ("1.0", 1.0, 1):
  38. result = self.parse_response({"version": version})
  39. # An empty Open Graph response is an error, ensure the URL is included.
  40. self.assertIn("og:url", result.open_graph_result)
  41. # A missing version should be treated as 1.0.
  42. result = self.parse_response({"type": "link"})
  43. self.assertIn("og:url", result.open_graph_result)
  44. # Invalid versions should be rejected.
  45. for version in ("2.0", "1", 1.1, 0, None, {}, []):
  46. result = self.parse_response({"version": version, "type": "link"})
  47. # An empty Open Graph response is an error, ensure the URL is included.
  48. self.assertEqual({}, result.open_graph_result)
  49. def test_cache_age(self) -> None:
  50. """Ensure a cache-age is parsed properly."""
  51. # Correct-ish cache ages are allowed.
  52. for cache_age in ("1", 1.0, 1):
  53. result = self.parse_response({"cache_age": cache_age})
  54. self.assertEqual(result.cache_age, 1000)
  55. # Invalid cache ages are ignored.
  56. for cache_age in ("invalid", {}):
  57. result = self.parse_response({"cache_age": cache_age})
  58. self.assertIsNone(result.cache_age)
  59. # Cache age is optional.
  60. result = self.parse_response({})
  61. self.assertIsNone(result.cache_age)
  62. @parameterized.expand(
  63. [
  64. ("title", "title"),
  65. ("provider_name", "site_name"),
  66. ("thumbnail_url", "image"),
  67. ],
  68. name_func=lambda func, num, p: f"{func.__name__}_{p.args[0]}",
  69. )
  70. def test_property(self, oembed_property: str, open_graph_property: str) -> None:
  71. """Test properties which must be strings."""
  72. result = self.parse_response({oembed_property: "test"})
  73. self.assertIn(f"og:{open_graph_property}", result.open_graph_result)
  74. self.assertEqual(result.open_graph_result[f"og:{open_graph_property}"], "test")
  75. result = self.parse_response({oembed_property: 1})
  76. self.assertNotIn(f"og:{open_graph_property}", result.open_graph_result)
  77. def test_author_name(self) -> None:
  78. """Test the author_name property."""
  79. result = self.parse_response({"author_name": "test"})
  80. self.assertEqual(result.author_name, "test")
  81. result = self.parse_response({"author_name": 1})
  82. self.assertIsNone(result.author_name)
  83. def test_rich(self) -> None:
  84. """Test a type of rich."""
  85. result = self.parse_response({"html": "test<img src='foo'>", "type": "rich"})
  86. self.assertIn("og:description", result.open_graph_result)
  87. self.assertIn("og:image", result.open_graph_result)
  88. self.assertEqual(result.open_graph_result["og:description"], "test")
  89. self.assertEqual(result.open_graph_result["og:image"], "foo")
  90. result = self.parse_response({"type": "rich"})
  91. self.assertNotIn("og:description", result.open_graph_result)
  92. result = self.parse_response({"html": 1, "type": "rich"})
  93. self.assertNotIn("og:description", result.open_graph_result)
  94. def test_photo(self) -> None:
  95. """Test a type of photo."""
  96. result = self.parse_response({"url": "test", "type": "photo"})
  97. self.assertIn("og:image", result.open_graph_result)
  98. self.assertEqual(result.open_graph_result["og:image"], "test")
  99. result = self.parse_response({"type": "photo"})
  100. self.assertNotIn("og:image", result.open_graph_result)
  101. result = self.parse_response({"url": 1, "type": "photo"})
  102. self.assertNotIn("og:image", result.open_graph_result)
  103. def test_video(self) -> None:
  104. """Test a type of video."""
  105. result = self.parse_response({"html": "test", "type": "video"})
  106. self.assertIn("og:type", result.open_graph_result)
  107. self.assertEqual(result.open_graph_result["og:type"], "video.other")
  108. self.assertIn("og:description", result.open_graph_result)
  109. self.assertEqual(result.open_graph_result["og:description"], "test")
  110. result = self.parse_response({"type": "video"})
  111. self.assertIn("og:type", result.open_graph_result)
  112. self.assertEqual(result.open_graph_result["og:type"], "video.other")
  113. self.assertNotIn("og:description", result.open_graph_result)
  114. result = self.parse_response({"url": 1, "type": "video"})
  115. self.assertIn("og:type", result.open_graph_result)
  116. self.assertEqual(result.open_graph_result["og:type"], "video.other")
  117. self.assertNotIn("og:description", result.open_graph_result)
  118. def test_link(self) -> None:
  119. """Test type of link."""
  120. result = self.parse_response({"type": "link"})
  121. self.assertIn("og:type", result.open_graph_result)
  122. self.assertEqual(result.open_graph_result["og:type"], "website")
  123. def test_title_html_entities(self) -> None:
  124. """Test HTML entities in title"""
  125. result = self.parse_response(
  126. {"title": "Why JSON isn&#8217;t a Good Configuration Language"}
  127. )
  128. self.assertEqual(
  129. result.open_graph_result["og:title"],
  130. "Why JSON isn’t a Good Configuration Language",
  131. )