pytest_plugin.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import urllib.parse
  2. import pytest
  3. from jupyter_server.utils import url_path_join
  4. from jupyterlab_server import LabConfig
  5. from tornado.escape import url_escape
  6. from traitlets import Unicode
  7. from jupyterlab.labapp import LabApp
  8. def mkdir(tmp_path, *parts):
  9. path = tmp_path.joinpath(*parts)
  10. if not path.exists():
  11. path.mkdir(parents=True)
  12. return path
  13. app_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "app_settings"))
  14. user_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "user_settings"))
  15. schemas_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "schemas"))
  16. workspaces_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "workspaces"))
  17. @pytest.fixture
  18. def make_lab_app(
  19. jp_root_dir, jp_template_dir, app_settings_dir, user_settings_dir, schemas_dir, workspaces_dir
  20. ):
  21. def _make_lab_app(**kwargs):
  22. class TestLabApp(LabApp):
  23. base_url = "/lab"
  24. extension_url = "/lab"
  25. default_url = Unicode("/", help="The default URL to redirect to from `/`")
  26. lab_config = LabConfig(
  27. app_name="JupyterLab Test App",
  28. static_dir=str(jp_root_dir),
  29. templates_dir=str(jp_template_dir),
  30. app_url="/lab",
  31. app_settings_dir=str(app_settings_dir),
  32. user_settings_dir=str(user_settings_dir),
  33. schemas_dir=str(schemas_dir),
  34. workspaces_dir=str(workspaces_dir),
  35. )
  36. app = TestLabApp()
  37. return app
  38. # Create the index files.
  39. index = jp_template_dir.joinpath("index.html")
  40. index.write_text(
  41. """
  42. <!DOCTYPE html>
  43. <html>
  44. <head>
  45. <title>{{page_config['appName'] | e}}</title>
  46. </head>
  47. <body>
  48. {# Copy so we do not modify the page_config with updates. #}
  49. {% set page_config_full = page_config.copy() %}
  50. {# Set a dummy variable - we just want the side effect of the update. #}
  51. {% set _ = page_config_full.update(baseUrl=base_url, wsUrl=ws_url) %}
  52. <script id="jupyter-config-data" type="application/json">
  53. {{ page_config_full | tojson }}
  54. </script>
  55. <script src="{{page_config['fullStaticUrl'] | e}}/bundle.js" main="index"></script>
  56. <script type="text/javascript">
  57. /* Remove token from URL. */
  58. (function () {
  59. var parsedUrl = new URL(window.location.href);
  60. if (parsedUrl.searchParams.get('token')) {
  61. parsedUrl.searchParams.delete('token');
  62. window.history.replaceState({ }, '', parsedUrl.href);
  63. }
  64. })();
  65. </script>
  66. </body>
  67. </html>
  68. """
  69. )
  70. return _make_lab_app
  71. @pytest.fixture
  72. def labapp(jp_serverapp, make_lab_app):
  73. app = make_lab_app()
  74. app._link_jupyter_server_extension(jp_serverapp)
  75. app.initialize()
  76. return app
  77. @pytest.fixture
  78. def fetch_long(http_server_client, jp_auth_header, jp_base_url):
  79. """fetch fixture that handles auth, base_url, and path"""
  80. def client_fetch(*parts, headers={}, params={}, **kwargs):
  81. # Handle URL strings
  82. path_url = url_escape(url_path_join(*parts), plus=False)
  83. path_url = url_path_join(jp_base_url, path_url)
  84. params_url = urllib.parse.urlencode(params)
  85. url = path_url + "?" + params_url
  86. # Add auth keys to header
  87. headers.update(jp_auth_header)
  88. # Make request.
  89. return http_server_client.fetch(url, headers=headers, request_timeout=250, **kwargs)
  90. return client_fetch