test_embed.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import requests
  2. from base import BaseTestCase, Media
  3. from parameterized import parameterized
  4. class Embed:
  5. container = '.tweet-embed'
  6. footer = '.embed-footer'
  7. tweet_content = '.tweet-content'
  8. tweet_header = '.tweet-header'
  9. fullname = '.fullname'
  10. username = '.username'
  11. avatar = '.avatar'
  12. stats = '.tweet-stats'
  13. quote = '.quote'
  14. error_panel = '.error-panel'
  15. class TweetEmbedTest(BaseTestCase):
  16. """Test tweet embed rendering."""
  17. tweet = 'elonmusk/status/1141367104702038016'
  18. def test_embed_container_visible(self):
  19. self.open_nitter(self.tweet + '/embed')
  20. self.assert_element_visible(Embed.container)
  21. def test_embed_has_footer(self):
  22. self.open_nitter(self.tweet + '/embed')
  23. self.assert_element_visible(Embed.footer)
  24. self.assert_text_visible('Read more on', Embed.footer)
  25. def test_embed_has_tweet_content(self):
  26. self.open_nitter(self.tweet + '/embed')
  27. self.assert_element_visible(Embed.tweet_content)
  28. def test_embed_has_avatar(self):
  29. self.open_nitter(self.tweet + '/embed')
  30. self.assert_element_visible(Embed.avatar)
  31. def test_embed_has_username(self):
  32. self.open_nitter(self.tweet + '/embed')
  33. self.assert_element_visible(Embed.username)
  34. def test_embed_has_stats(self):
  35. self.open_nitter(self.tweet + '/embed')
  36. self.assert_element_visible(Embed.stats)
  37. def test_embed_footer_links_to_tweet(self):
  38. self.open_nitter(self.tweet + '/embed')
  39. href = self.get_attribute(Embed.footer, 'href')
  40. self.assertIn('/elonmusk/status/1141367104702038016', href)
  41. class TweetEmbedMediaTest(BaseTestCase):
  42. """Test embed rendering with various media types."""
  43. def test_embed_with_image(self):
  44. self.open_nitter('mobile_test/status/519364660823207936/embed')
  45. self.assert_element_visible(Embed.container)
  46. self.scroll_to(Media.container)
  47. self.assert_element_visible(Media.image)
  48. def test_embed_with_gif(self):
  49. self.open_nitter('elonmusk/status/1141367104702038016/embed')
  50. self.assert_element_visible(Embed.container)
  51. self.scroll_to(Media.container)
  52. self.assert_element_visible(Media.gif)
  53. def test_embed_with_video(self):
  54. self.open_nitter('d0m96/status/1078373829917974528/embed')
  55. self.assert_element_visible(Embed.container)
  56. self.scroll_to(Media.container)
  57. self.assert_element_visible(Media.video)
  58. def test_embed_with_gallery(self):
  59. self.open_nitter('mobile_test/status/451108446603980803/embed')
  60. self.assert_element_visible(Embed.container)
  61. self.scroll_to(Media.container)
  62. self.assert_element_visible(Media.row)
  63. class TweetEmbedQuoteTest(BaseTestCase):
  64. """Test embed rendering with quoted tweets."""
  65. def test_embed_with_quote_shows_quote(self):
  66. self.open_nitter('elonmusk/status/1138827760107790336/embed')
  67. self.assert_element_visible(Embed.container)
  68. self.assert_element_visible(Embed.quote)
  69. def test_embed_quote_has_content(self):
  70. self.open_nitter('elonmusk/status/1138827760107790336/embed')
  71. quote = self.find_element(Embed.quote)
  72. self.assertIsNotNone(quote.text)
  73. class EmbedErrorTest(BaseTestCase):
  74. """Test embed error handling."""
  75. def test_nonexistent_tweet_shows_error(self):
  76. self.open_nitter('nobody/status/1/embed')
  77. self.assert_element_visible('.tweet-embed.error-embed')
  78. self.assert_text_visible('not found', Embed.error_panel)
  79. def test_protected_account_embed_shows_error(self):
  80. self.open_nitter('mobile_test_7/status/1/embed')
  81. self.assert_element_visible('.tweet-embed.error-embed')
  82. def test_invalid_tweet_id_shows_error(self):
  83. self.open_nitter('jack/status/notanumber/embed')
  84. self.assert_element_visible('.tweet-embed.error-embed')
  85. class OEmbedApiTest(BaseTestCase):
  86. """Test oEmbed API endpoint."""
  87. base_url = 'http://localhost:8080'
  88. tweet_url = 'https://twitter.com/elonmusk/status/1141367104702038016'
  89. def test_oembed_returns_json(self):
  90. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
  91. self.assertEqual(resp.status_code, 200)
  92. self.assertEqual(resp.headers['Content-Type'], 'application/json')
  93. def test_oembed_has_required_fields(self):
  94. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
  95. data = resp.json()
  96. self.assertEqual(data['type'], 'rich')
  97. self.assertEqual(data['version'], '1.0')
  98. self.assertIn('html', data)
  99. self.assertIn('author_name', data)
  100. self.assertIn('provider_name', data)
  101. def test_oembed_html_contains_iframe(self):
  102. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
  103. data = resp.json()
  104. self.assertIn('<iframe', data['html'])
  105. self.assertIn('/embed', data['html'])
  106. def test_oembed_has_cors_header(self):
  107. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
  108. self.assertEqual(resp.headers.get('Access-Control-Allow-Origin'), '*')
  109. def test_oembed_missing_url_returns_400(self):
  110. resp = requests.get(f'{self.base_url}/api/oembed')
  111. self.assertEqual(resp.status_code, 400)
  112. def test_oembed_invalid_url_returns_400(self):
  113. resp = requests.get(f'{self.base_url}/api/oembed?url=https://example.com')
  114. self.assertEqual(resp.status_code, 400)
  115. def test_oembed_supports_x_com_url(self):
  116. x_url = 'https://x.com/elonmusk/status/1141367104702038016'
  117. resp = requests.get(f'{self.base_url}/api/oembed?url={x_url}')
  118. self.assertEqual(resp.status_code, 200)
  119. def test_oembed_strips_query_params(self):
  120. url_with_params = 'https://twitter.com/elonmusk/status/1141367104702038016?s=20&t=abc'
  121. resp = requests.get(f'{self.base_url}/api/oembed?url={url_with_params}')
  122. self.assertEqual(resp.status_code, 200)
  123. data = resp.json()
  124. self.assertIn('html', data)
  125. def test_oembed_handles_trailing_slash(self):
  126. url_with_slash = 'https://twitter.com/elonmusk/status/1141367104702038016/'
  127. resp = requests.get(f'{self.base_url}/api/oembed?url={url_with_slash}')
  128. self.assertEqual(resp.status_code, 200)
  129. def test_oembed_handles_mobile_url(self):
  130. mobile_url = 'https://mobile.twitter.com/elonmusk/status/1141367104702038016'
  131. resp = requests.get(f'{self.base_url}/api/oembed?url={mobile_url}')
  132. self.assertEqual(resp.status_code, 200)
  133. def test_oembed_rejects_malformed_tweet_id(self):
  134. bad_url = 'https://twitter.com/elonmusk/status/notanumber'
  135. resp = requests.get(f'{self.base_url}/api/oembed?url={bad_url}')
  136. self.assertEqual(resp.status_code, 400)
  137. def test_oembed_maxwidth_param_accepted(self):
  138. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=400')
  139. self.assertEqual(resp.status_code, 200)
  140. def test_oembed_maxwidth_clamps_to_range(self):
  141. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=100')
  142. data = resp.json()
  143. self.assertEqual(data['width'], 220)
  144. self.assertIn('max-width:220px', data['html'])
  145. def test_oembed_maxwidth_caps_at_550(self):
  146. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=9999')
  147. data = resp.json()
  148. self.assertEqual(data['width'], 550)
  149. def test_oembed_maxwidth_invalid_uses_default(self):
  150. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=abc')
  151. data = resp.json()
  152. self.assertEqual(data['width'], 550)
  153. def test_oembed_author_url_present(self):
  154. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
  155. data = resp.json()
  156. self.assertIn('author_url', data)
  157. self.assertIn('elonmusk', data['author_url'])
  158. def test_oembed_accepts_nitter_url(self):
  159. nitter_url = f'{self.base_url}/elonmusk/status/1141367104702038016'
  160. resp = requests.get(f'{self.base_url}/api/oembed?url={nitter_url}')
  161. self.assertEqual(resp.status_code, 200)
  162. data = resp.json()
  163. self.assertIn('html', data)
  164. def test_oembed_format_json_accepted(self):
  165. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&format=json')
  166. self.assertEqual(resp.status_code, 200)
  167. def test_oembed_format_xml_returns_501(self):
  168. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&format=xml')
  169. self.assertEqual(resp.status_code, 501)
  170. def test_oembed_has_title(self):
  171. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
  172. data = resp.json()
  173. self.assertIn('title', data)
  174. self.assertIsInstance(data['title'], str)
  175. self.assertGreater(len(data['title']), 0)
  176. def test_oembed_has_null_height(self):
  177. resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
  178. data = resp.json()
  179. self.assertIsNone(data['height'])
  180. class OEmbedDiscoveryTest(BaseTestCase):
  181. """Test oEmbed discovery link tags on tweet pages."""
  182. base_url = 'http://localhost:8080'
  183. def test_tweet_page_has_oembed_link_tag(self):
  184. self.open_nitter('elonmusk/status/1141367104702038016')
  185. self.assert_element_present('link[type="application/json+oembed"]')
  186. def test_oembed_link_tag_points_to_api(self):
  187. resp = requests.get(f'{self.base_url}/elonmusk/status/1141367104702038016')
  188. self.assertIn('application/json+oembed', resp.text)
  189. self.assertIn('/api/oembed?url=', resp.text)
  190. self.assertIn('1141367104702038016', resp.text)
  191. def test_oembed_discovery_roundtrip(self):
  192. """Fetch a tweet page, extract oEmbed URL, call it, verify response."""
  193. import re
  194. resp = requests.get(f'{self.base_url}/elonmusk/status/1141367104702038016')
  195. match = re.search(
  196. r'type="application/json\+oembed"\s+href="([^"]*)"', resp.text)
  197. self.assertIsNotNone(match, "No oEmbed discovery link found in page")
  198. oembed_url = match.group(1).replace('&amp;', '&')
  199. oembed_resp = requests.get(oembed_url)
  200. self.assertEqual(oembed_resp.status_code, 200)
  201. data = oembed_resp.json()
  202. self.assertEqual(data['type'], 'rich')
  203. self.assertIn('html', data)
  204. class VideoEmbedTest(BaseTestCase):
  205. """Test video embed route (/i/videos/tweet/{id})."""
  206. video_tweet_id = '1078373829917974528'
  207. def test_video_embed_has_video_element(self):
  208. self.open_nitter(f'i/videos/tweet/{self.video_tweet_id}')
  209. self.assert_element_visible('video')
  210. def test_video_embed_has_poster(self):
  211. self.open_nitter(f'i/videos/tweet/{self.video_tweet_id}')
  212. poster = self.get_attribute('video', 'poster')
  213. self.assertIsNotNone(poster)
  214. self.assertIn('pic/', poster)
  215. def test_video_embed_nonexistent_returns_error(self):
  216. self.open_nitter('i/videos/tweet/1')
  217. self.assert_element_visible('.error-embed')