# Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. import hashlib import json from unittest.mock import patch from . import fake_client_factory FAKE_ATOM_FEED = b"""Jekyll2022-11-02T15:14:50+00:00https://jupyterlab.github.io/assets/feed.xmlJupyterLab NewsSubscribe to get news about JupyterLab.Thanks for using JupyterLab2022-11-02T14:00:00+00:002022-11-02T14:00:00+00:00https://jupyterlab.github.io/assets/posts/2022/11/02/demo<h1 id="welcome">Welcome</h1> <p>Thanks a lot for your interest in JupyterLab.</p>Big thanks to you, beloved JupyterLab user.""" FAKE_JUPYTERLAB_PYPI_JSON = b"""{ "info": { "version": "1000.0.0" } }""" @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_NewsHandler_get_success(mock_client, labserverapp, jp_fetch): mock_client.body = FAKE_ATOM_FEED response = await jp_fetch("lab", "api", "news", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["news"] == [ { "createdAt": 1667397600000.0, "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.", "modifiedAt": 1667397600000.0, "type": "info", "link": [ "Open full post", "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html", ], "options": { "data": { "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo", "tags": ["news"], } }, } ] @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_NewsHandler_get_failure(mock_client, labserverapp, jp_fetch): response = await jp_fetch("lab", "api", "news", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["news"] == [] @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_CheckForUpdateHandler_get_pypi_success(mock_client, labserverapp, jp_fetch): mock_client.body = FAKE_JUPYTERLAB_PYPI_JSON response = await jp_fetch("lab", "api", "update", method="GET") message = "A newer version (1000.0.0) of JupyterLab is available." assert response.code == 200 payload = json.loads(response.body) assert payload["notification"]["message"] == message assert payload["notification"]["link"] == [ "Open changelog", "https://github.com/jupyterlab/jupyterlab/releases/tag/v1000.0.0", ] assert payload["notification"]["options"] == { "data": {"id": hashlib.sha1(message.encode()).hexdigest(), "tags": ["update"]} # noqa: S324 } @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_CheckForUpdateHandler_get_failure(mock_client, labserverapp, jp_fetch): response = await jp_fetch("lab", "api", "update", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["notification"] is None FAKE_NO_SUMMARY_ATOM_FEED = b"""https://jupyterlab.github.io/assets/feed.xmlJupyterLab News2023-05-02T19:01:33.669598+00:00John Doejohn@example.depython-feedgenhttp://ex.com/logo.jpgSubscribe to get news about JupyterLab.https://jupyterlab.github.io/assets/posts/2022/11/02/demoThanks for using JupyterLab2022-11-02T14:00:00+00:002022-11-02T14:00:00+00:00""" @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_NewsHandler_get_missing_summary(mock_client, labserverapp, jp_fetch): mock_client.body = FAKE_NO_SUMMARY_ATOM_FEED response = await jp_fetch("lab", "api", "news", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["news"] == [ { "createdAt": 1667397600000.0, "message": "Thanks for using JupyterLab", "modifiedAt": 1667397600000.0, "type": "info", "link": [ "Open full post", "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html", ], "options": { "data": { "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo", "tags": ["news"], } }, } ] FAKE_MULTI_ENTRY_LINKS_ATOM_FEED = b"""https://jupyterlab.github.io/assets/feed.xmlJupyterLab News2023-05-02T19:59:44.332080+00:00John Doejohn@example.depython-feedgenhttp://ex.com/logo.jpgSubscribe to get news about JupyterLab.https://jupyterlab.github.io/assets/posts/2022/11/02/demoThanks for using JupyterLab2022-11-02T14:00:00+00:00Big thanks to you, beloved JupyterLab user.2022-11-02T14:00:00+00:00""" @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_NewsHandler_multi_entry_links(mock_client, labserverapp, jp_fetch): mock_client.body = FAKE_MULTI_ENTRY_LINKS_ATOM_FEED response = await jp_fetch("lab", "api", "news", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["news"] == [ { "createdAt": 1667397600000.0, "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.", "modifiedAt": 1667397600000.0, "type": "info", "link": [ "Open full post", "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html", ], "options": { "data": { "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo", "tags": ["news"], } }, } ] FAKE_NO_PUBLISHED_ATOM_FEED = b"""https://jupyterlab.github.io/assets/feed.xmlJupyterLab News2023-05-02T19:32:08.566055+00:00John Doejohn@example.depython-feedgenhttp://ex.com/logo.jpgSubscribe to get news about JupyterLab.https://jupyterlab.github.io/assets/posts/2022/11/02/demoThanks for using JupyterLab2022-11-02T14:00:00+00:00Big thanks to you, beloved JupyterLab user.""" @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_NewsHandler_no_published(mock_client, labserverapp, jp_fetch): mock_client.body = FAKE_NO_PUBLISHED_ATOM_FEED response = await jp_fetch("lab", "api", "news", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["news"] == [ { "createdAt": 1667397600000.0, "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.", "modifiedAt": 1667397600000.0, "type": "info", "link": [ "Open full post", "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html", ], "options": { "data": { "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo", "tags": ["news"], } }, } ] FAKE_LINK_NO_REL_ATOM_FEED = b"""https://jupyterlab.github.io/assets/feed.xmlJupyterLab News2023-05-03T17:06:43.950978+00:00John Doejohn@example.depython-feedgenhttp://ex.com/logo.jpgSubscribe to get news about JupyterLab.https://jupyterlab.github.io/assets/posts/2022/11/02/demoThanks for using JupyterLab2022-11-02T14:00:00+00:00Big thanks to you, beloved JupyterLab user.2022-11-02T14:00:00+00:00""" @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_NewsHandler_link_no_rel(mock_client, labserverapp, jp_fetch): mock_client.body = FAKE_LINK_NO_REL_ATOM_FEED response = await jp_fetch("lab", "api", "news", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["news"] == [ { "createdAt": 1667397600000.0, "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.", "modifiedAt": 1667397600000.0, "type": "info", "link": [ "Open full post", "https://jupyterlab.github.io/assets/posts/2022/11/02/demo.html", ], "options": { "data": { "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo", "tags": ["news"], } }, } ] FAKE_NO_LINK_ATOM_FEED = b"""https://jupyterlab.github.io/assets/feed.xmlJupyterLab News2023-05-03T17:06:43.950978+00:00John Doejohn@example.depython-feedgenhttp://ex.com/logo.jpgSubscribe to get news about JupyterLab.https://jupyterlab.github.io/assets/posts/2022/11/02/demoThanks for using JupyterLab2022-11-02T14:00:00+00:00Big thanks to you, beloved JupyterLab user.2022-11-02T14:00:00+00:00""" @patch("tornado.httpclient.AsyncHTTPClient", new_callable=fake_client_factory) async def test_NewsHandler_no_links(mock_client, labserverapp, jp_fetch): mock_client.body = FAKE_NO_LINK_ATOM_FEED response = await jp_fetch("lab", "api", "news", method="GET") assert response.code == 200 payload = json.loads(response.body) assert payload["news"] == [ { "createdAt": 1667397600000.0, "message": "Thanks for using JupyterLab\nBig thanks to you, beloved JupyterLab user.", "modifiedAt": 1667397600000.0, "type": "info", "link": None, "options": { "data": { "id": "https://jupyterlab.github.io/assets/posts/2022/11/02/demo", "tags": ["news"], } }, } ]