test_announcements.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. import hashlib
  4. import json
  5. from unittest.mock import patch
  6. from . import fake_client_factory
  7. FAKE_ATOM_FEED = b"""<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="https://jupyterlab.github.io/assets/feed.xml" rel="self" type="application/atom+xml" /><link href="https://jupyterlab.github.io/assets/" rel="alternate" type="text/html" /><updated>2022-11-02T15:14:50+00:00</updated><id>https://jupyterlab.github.io/assets/feed.xml</id><title type="html">JupyterLab News</title><subtitle>Subscribe to get news about JupyterLab.</subtitle><entry><title type="html">Thanks for using JupyterLab</title><link href="https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html" rel="alternate" type="text/html" title="Thanks for using JupyterLab" /><published>2022-11-02T14:00:00+00:00</published><updated>2022-11-02T14:00:00+00:00</updated><id>https://jupyterlab.github.io/assets/posts/2022/11/02/demo</id><content type="html" xml:base="https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html">&lt;h1 id=&quot;welcome&quot;&gt;Welcome&lt;/h1&gt;
  8. &lt;p&gt;Thanks a lot for your interest in JupyterLab.&lt;/p&gt;</content><author><name></name></author><category term="posts" /><summary type="html">Big thanks to you, beloved JupyterLab user.</summary></entry></feed>"""
  9. FAKE_JUPYTERLAB_PYPI_JSON = b"""{ "info": { "version": "1000.0.0" } }"""
  10. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  11. async def test_NewsHandler_get_success(mock_client, labserverapp, jp_fetch):
  12. mock_client.body = FAKE_ATOM_FEED
  13. response = await jp_fetch("lab", "api", "news", method="GET")
  14. assert response.code == 200
  15. payload = json.loads(response.body)
  16. assert payload["news"] == [
  17. {
  18. "createdAt": 1667397600000.0,
  19. "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.",
  20. "modifiedAt": 1667397600000.0,
  21. "type": "info",
  22. "link": [
  23. "Open full post",
  24. "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html",
  25. ],
  26. "options": {
  27. "data": {
  28. "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo",
  29. "tags": ["news"],
  30. }
  31. },
  32. }
  33. ]
  34. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  35. async def test_NewsHandler_get_failure(mock_client, labserverapp, jp_fetch):
  36. response = await jp_fetch("lab", "api", "news", method="GET")
  37. assert response.code == 200
  38. payload = json.loads(response.body)
  39. assert payload["news"] == []
  40. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  41. async def test_CheckForUpdateHandler_get_pypi_success(mock_client, labserverapp, jp_fetch):
  42. mock_client.body = FAKE_JUPYTERLAB_PYPI_JSON
  43. response = await jp_fetch("lab", "api", "update", method="GET")
  44. message = "A newer version (1000.0.0) of JupyterLab is available."
  45. assert response.code == 200
  46. payload = json.loads(response.body)
  47. assert payload["notification"]["message"] == message
  48. assert payload["notification"]["link"] == [
  49. "Read more…",
  50. "https://github.com/jupyterlab/jupyterlab/releases/tag/v1000.0.0",
  51. ]
  52. assert payload["notification"]["options"] == {
  53. "data": {"id": hashlib.sha1(message.encode()).hexdigest(), "tags": ["update"]} # noqa: S324
  54. }
  55. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  56. async def test_CheckForUpdateHandler_get_failure(mock_client, labserverapp, jp_fetch):
  57. response = await jp_fetch("lab", "api", "update", method="GET")
  58. assert response.code == 200
  59. payload = json.loads(response.body)
  60. assert payload["notification"] is None
  61. FAKE_NO_SUMMARY_ATOM_FEED = b"""<?xml version='1.0' encoding='UTF-8'?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><id>https://jupyterlab.github.io/assets/feed.xml</id><title>JupyterLab News</title><updated>2023-05-02T19:01:33.669598+00:00</updated><author><name>John Doe</name><email>john@example.de</email></author><link href="https://jupyterlab.github.io/assets/feed.xml" rel="self" type="application/atom+xml"/><link href="https://jupyterlab.github.io/assets/" rel="alternate" type="text/html"/><generator uri="https://lkiesow.github.io/python-feedgen" version="0.9.0">python-feedgen</generator><logo>http://ex.com/logo.jpg</logo><subtitle>Subscribe to get news about JupyterLab.</subtitle><entry><id>https://jupyterlab.github.io/assets/posts/2022/11/02/demo</id><title>Thanks for using JupyterLab</title><updated>2022-11-02T14:00:00+00:00</updated><link href="https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html" rel="alternate" type="text/html" title="Thanks for using JupyterLab"/><published>2022-11-02T14:00:00+00:00</published></entry></feed>"""
  62. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  63. async def test_NewsHandler_get_missing_summary(mock_client, labserverapp, jp_fetch):
  64. mock_client.body = FAKE_NO_SUMMARY_ATOM_FEED
  65. response = await jp_fetch("lab", "api", "news", method="GET")
  66. assert response.code == 200
  67. payload = json.loads(response.body)
  68. assert payload["news"] == [
  69. {
  70. "createdAt": 1667397600000.0,
  71. "message": "Thanks for using JupyterLab",
  72. "modifiedAt": 1667397600000.0,
  73. "type": "info",
  74. "link": [
  75. "Open full post",
  76. "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html",
  77. ],
  78. "options": {
  79. "data": {
  80. "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo",
  81. "tags": ["news"],
  82. }
  83. },
  84. }
  85. ]
  86. FAKE_MULTI_ENTRY_LINKS_ATOM_FEED = b"""<?xml version='1.0' encoding='UTF-8'?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><id>https://jupyterlab.github.io/assets/feed.xml</id><title>JupyterLab News</title><updated>2023-05-02T19:59:44.332080+00:00</updated><author><name>John Doe</name><email>john@example.de</email></author><link href="https://jupyterlab.github.io/assets/feed.xml" rel="self" type="application/atom+xml"/><link href="https://jupyterlab.github.io/assets/" rel="alternate" type="text/html"/><generator uri="https://lkiesow.github.io/python-feedgen" version="0.9.0">python-feedgen</generator><logo>http://ex.com/logo.jpg</logo><subtitle>Subscribe to get news about JupyterLab.</subtitle><entry><id>https://jupyterlab.github.io/assets/posts/2022/11/02/demo</id><title>Thanks for using JupyterLab</title><updated>2022-11-02T14:00:00+00:00</updated><link href="https://jupyterlab.github.io/assets/posts/2022/11/02/demo_self.html" rel="self" type="text/html" title="Thanks for using JupyterLab"/><link href="https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html" rel="alternate" type="text/html" title="Thanks for using JupyterLab"/><summary>Big thanks to you, beloved JupyterLab user.</summary><published>2022-11-02T14:00:00+00:00</published></entry></feed>"""
  87. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  88. async def test_NewsHandler_multi_entry_links(mock_client, labserverapp, jp_fetch):
  89. mock_client.body = FAKE_MULTI_ENTRY_LINKS_ATOM_FEED
  90. response = await jp_fetch("lab", "api", "news", method="GET")
  91. assert response.code == 200
  92. payload = json.loads(response.body)
  93. assert payload["news"] == [
  94. {
  95. "createdAt": 1667397600000.0,
  96. "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.",
  97. "modifiedAt": 1667397600000.0,
  98. "type": "info",
  99. "link": [
  100. "Open full post",
  101. "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html",
  102. ],
  103. "options": {
  104. "data": {
  105. "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo",
  106. "tags": ["news"],
  107. }
  108. },
  109. }
  110. ]
  111. FAKE_NO_PUBLISHED_ATOM_FEED = b"""<?xml version='1.0' encoding='UTF-8'?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><id>https://jupyterlab.github.io/assets/feed.xml</id><title>JupyterLab News</title><updated>2023-05-02T19:32:08.566055+00:00</updated><author><name>John Doe</name><email>john@example.de</email></author><link href="https://jupyterlab.github.io/assets/feed.xml" rel="self" type="application/atom+xml"/><link href="https://jupyterlab.github.io/assets/" rel="alternate" type="text/html"/><generator uri="https://lkiesow.github.io/python-feedgen" version="0.9.0">python-feedgen</generator><logo>http://ex.com/logo.jpg</logo><subtitle>Subscribe to get news about JupyterLab.</subtitle><entry><id>https://jupyterlab.github.io/assets/posts/2022/11/02/demo</id><title>Thanks for using JupyterLab</title><updated>2022-11-02T14:00:00+00:00</updated><link href="https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html" rel="alternate" type="text/html" title="Thanks for using JupyterLab"/><summary>Big thanks to you, beloved JupyterLab user.</summary></entry></feed>"""
  112. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  113. async def test_NewsHandler_no_published(mock_client, labserverapp, jp_fetch):
  114. mock_client.body = FAKE_NO_PUBLISHED_ATOM_FEED
  115. response = await jp_fetch("lab", "api", "news", method="GET")
  116. assert response.code == 200
  117. payload = json.loads(response.body)
  118. assert payload["news"] == [
  119. {
  120. "createdAt": 1667397600000.0,
  121. "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.",
  122. "modifiedAt": 1667397600000.0,
  123. "type": "info",
  124. "link": [
  125. "Open full post",
  126. "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html",
  127. ],
  128. "options": {
  129. "data": {
  130. "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo",
  131. "tags": ["news"],
  132. }
  133. },
  134. }
  135. ]
  136. FAKE_LINK_NO_REL_ATOM_FEED = b"""<?xml version='1.0' encoding='UTF-8'?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><id>https://jupyterlab.github.io/assets/feed.xml</id><title>JupyterLab News</title><updated>2023-05-03T17:06:43.950978+00:00</updated><author><name>John Doe</name><email>john@example.de</email></author><link href="https://jupyterlab.github.io/assets/feed.xml" rel="self" type="application/atom+xml"/><link href="https://jupyterlab.github.io/assets/" rel="alternate" type="text/html"/><generator uri="https://lkiesow.github.io/python-feedgen" version="0.9.0">python-feedgen</generator><logo>http://ex.com/logo.jpg</logo><subtitle>Subscribe to get news about JupyterLab.</subtitle><entry><id>https://jupyterlab.github.io/assets/posts/2022/11/02/demo</id><title>Thanks for using JupyterLab</title><updated>2022-11-02T14:00:00+00:00</updated><link href="https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html" type="text/html" title="Thanks for using JupyterLab"/><summary>Big thanks to you, beloved JupyterLab user.</summary><published>2022-11-02T14:00:00+00:00</published></entry></feed>"""
  137. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  138. async def test_NewsHandler_link_no_rel(mock_client, labserverapp, jp_fetch):
  139. mock_client.body = FAKE_LINK_NO_REL_ATOM_FEED
  140. response = await jp_fetch("lab", "api", "news", method="GET")
  141. assert response.code == 200
  142. payload = json.loads(response.body)
  143. assert payload["news"] == [
  144. {
  145. "createdAt": 1667397600000.0,
  146. "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.",
  147. "modifiedAt": 1667397600000.0,
  148. "type": "info",
  149. "link": [
  150. "Open full post",
  151. "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html",
  152. ],
  153. "options": {
  154. "data": {
  155. "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo",
  156. "tags": ["news"],
  157. }
  158. },
  159. }
  160. ]
  161. FAKE_NO_LINK_ATOM_FEED = b"""<?xml version='1.0' encoding='UTF-8'?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><id>https://jupyterlab.github.io/assets/feed.xml</id><title>JupyterLab News</title><updated>2023-05-03T17:06:43.950978+00:00</updated><author><name>John Doe</name><email>john@example.de</email></author><link href="https://jupyterlab.github.io/assets/feed.xml" rel="self" type="application/atom+xml"/><link href="https://jupyterlab.github.io/assets/" rel="alternate" type="text/html"/><generator uri="https://lkiesow.github.io/python-feedgen" version="0.9.0">python-feedgen</generator><logo>http://ex.com/logo.jpg</logo><subtitle>Subscribe to get news about JupyterLab.</subtitle><entry><id>https://jupyterlab.github.io/assets/posts/2022/11/02/demo</id><title>Thanks for using JupyterLab</title><updated>2022-11-02T14:00:00+00:00</updated><summary>Big thanks to you, beloved JupyterLab user.</summary><published>2022-11-02T14:00:00+00:00</published></entry></feed>"""
  162. @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory)
  163. async def test_NewsHandler_no_links(mock_client, labserverapp, jp_fetch):
  164. mock_client.body = FAKE_NO_LINK_ATOM_FEED
  165. response = await jp_fetch("lab", "api", "news", method="GET")
  166. assert response.code == 200
  167. payload = json.loads(response.body)
  168. assert payload["news"] == [
  169. {
  170. "createdAt": 1667397600000.0,
  171. "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.",
  172. "modifiedAt": 1667397600000.0,
  173. "type": "info",
  174. "link": None,
  175. "options": {
  176. "data": {
  177. "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo",
  178. "tags": ["news"],
  179. }
  180. },
  181. }
  182. ]