pytest_plugin.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import pytest, shutil, os
  2. import urllib.parse
  3. from tornado.escape import url_escape
  4. from traitlets import Unicode
  5. from jupyterlab import LabApp
  6. from jupyterlab_server import LabConfig
  7. from jupyterlab_server.tests.utils import here
  8. from jupyterlab_server.app import LabServerApp
  9. from jupyter_server.utils import url_path_join
  10. def mkdir(tmp_path, *parts):
  11. path = tmp_path.joinpath(*parts)
  12. if not path.exists():
  13. path.mkdir(parents=True)
  14. return path
  15. app_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, 'app_settings'))
  16. user_settings_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, 'user_settings'))
  17. schemas_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, 'schemas'))
  18. workspaces_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, 'workspaces'))
  19. @pytest.fixture
  20. def make_lab_app(jp_root_dir, jp_template_dir, app_settings_dir, user_settings_dir, schemas_dir, workspaces_dir):
  21. def _make_lab_app(**kwargs):
  22. class TestLabApp(LabApp):
  23. base_url = '/lab'
  24. extension_url = '/lab'
  25. default_url = Unicode('/',
  26. help='The default URL to redirect to from `/`')
  27. lab_config = LabConfig(
  28. app_name = 'JupyterLab Test App',
  29. static_dir = str(jp_root_dir),
  30. templates_dir = str(jp_template_dir),
  31. app_url = '/lab',
  32. app_settings_dir = str(app_settings_dir),
  33. user_settings_dir = str(user_settings_dir),
  34. schemas_dir = str(schemas_dir),
  35. workspaces_dir = str(workspaces_dir),
  36. )
  37. app = TestLabApp()
  38. return app
  39. # Create the index files.
  40. index = jp_template_dir.joinpath('index.html')
  41. index.write_text("""
  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. return _make_lab_app
  70. @pytest.fixture
  71. def labapp(jp_serverapp, make_lab_app):
  72. app = make_lab_app()
  73. app._link_jupyter_server_extension(jp_serverapp)
  74. app.initialize()
  75. return app
  76. @pytest.fixture
  77. def fetch_long(http_server_client, jp_auth_header, jp_base_url):
  78. """fetch fixture that handles auth, base_url, and path"""
  79. def client_fetch(*parts, headers={}, params={}, **kwargs):
  80. # Handle URL strings
  81. path_url = url_escape(url_path_join(*parts), plus=False)
  82. path_url = url_path_join(jp_base_url, path_url)
  83. params_url = urllib.parse.urlencode(params)
  84. url = path_url + "?" + params_url
  85. # Add auth keys to header
  86. headers.update(jp_auth_header)
  87. # Make request.
  88. return http_server_client.fetch(
  89. url, headers=headers, request_timeout=150, **kwargs
  90. )
  91. return client_fetch