Пример #1
0
def serverapp(environ, config, http_port, tmp_path, home_dir, data_dir,
              config_dir, runtime_dir, root_dir):

    config.NotebookNotary.db_file = ':memory:'
    token = hexlify(os.urandom(4)).decode('ascii')
    url_prefix = '/'
    app = ServerApp(
        port=http_port,
        port_retries=0,
        open_browser=False,
        config_dir=str(config_dir),
        data_dir=str(data_dir),
        runtime_dir=str(runtime_dir),
        root_dir=str(root_dir),
        base_url=url_prefix,
        config=config,
        allow_root=True,
        token=token,
    )
    app.init_signal = lambda: None
    app.log.propagate = True
    app.log.handlers = []
    # Initialize app without httpserver
    app.initialize(argv=[], new_httpserver=False)
    app.log.propagate = True
    app.log.handlers = []
    # Start app without ioloop
    app.start_app()
    return app
Пример #2
0
 def initialize_server(argv=[], **kwargs):
     """Get an instance of the Jupyter Server."""
     # Get a jupyter server instance
     serverapp = ServerApp(**kwargs)
     # Initialize ServerApp config.
     # Parses the command line looking for
     # ServerApp configuration.
     serverapp.initialize(argv=argv)
     return serverapp
def test_load_ordered(ordered_server_extensions):
    app = ServerApp()
    app.jpserver_extensions = OrderedDict([('mockextension2', True),
                                           ('mockextension1', True)])

    app.init_server_extensions()

    assert app.mockII is True, "Mock II should have been loaded"
    assert app.mockI is True, "Mock I should have been loaded"
    assert app.mock_shared == 'II', "Mock II should be loaded after Mock I"
Пример #4
0
def test_deprecated_config():
    cfg = Config()
    cfg.ServerApp.token = token = "asdf"
    cfg.ServerApp.password = password = passwd("secrets")
    app = ServerApp(config=cfg)
    app.initialize([])
    app.init_configurables()
    assert app.identity_provider.token == token
    assert app.token == token
    assert app.identity_provider.hashed_password == password
    assert app.password == password
Пример #5
0
def test_deprecated_config_priority():
    cfg = Config()
    cfg.ServerApp.token = "ignored"
    cfg.IdentityProvider.token = token = "idp_token"
    cfg.ServerApp.password = passwd("ignored")
    cfg.PasswordIdentityProvider.hashed_password = password = passwd("used")
    app = ServerApp(config=cfg)
    app.initialize([])
    app.init_configurables()
    assert app.identity_provider.token == token
    assert app.identity_provider.hashed_password == password
Пример #6
0
    def _default_serverapp(self):
        # load the current global instance, if any
        if ServerApp.initialized():
            try:
                return ServerApp.instance()
            except Exception:
                # error retrieving instance, e.g. MultipleInstanceError
                pass

        # serverapp accessed before it was defined,
        # declare an empty one
        return ServerApp()
Пример #7
0
def test_legacy_identity_config(jp_serverapp):
    # setting login_handler_class sets LegacyIdentityProvider
    app = ServerApp()
    idp = jp_serverapp.identity_provider
    assert type(idp) is LegacyIdentityProvider
    assert idp.login_available
    assert idp.auth_enabled
    assert idp.token
    assert idp.get_handlers() == [
        ("/login", idp.login_handler_class),
        ("/logout", idp.logout_handler_class),
    ]
Пример #8
0
def test_password_required(identity_provider_class, password_set, password_required, ok):
    app = ServerApp()
    idp = identity_provider_class(
        parent=app,
        hashed_password="******" if password_set else "",
        password_required=password_required,
    )
    app.identity_provider = idp
    if ok:
        ctx = nullcontext()
    else:
        ctx = pytest.raises(SystemExit)

    with ctx:
        idp.validate_security(app, ssl_options=None)
Пример #9
0
    def test_merge_config(self):
        # enabled at sys level
        mock_sys = self._inject_mock_extension('mockext_sys')
        # enabled at sys, disabled at user
        mock_both = self._inject_mock_extension('mockext_both')
        # enabled at user
        mock_user = self._inject_mock_extension('mockext_user')
        # enabled at Python
        mock_py = self._inject_mock_extension('mockext_py')

        toggle_serverextension_python('mockext_sys', enabled=True, user=False)
        toggle_serverextension_python('mockext_user', enabled=True, user=True)
        toggle_serverextension_python('mockext_both', enabled=True, user=False)
        toggle_serverextension_python('mockext_both', enabled=False, user=True)

        app = ServerApp(jpserver_extensions={'mockext_py': True})
        app.init_server_extensions()

        assert mock_user.loaded
        assert mock_sys.loaded
        assert mock_py.loaded
        assert not mock_both.loaded
Пример #10
0
def test_validate_security(
    identity_provider_class,
    ip,
    token,
    ssl,
    warns,
    caplog,
):
    app = ServerApp(ip=ip, log=logging.getLogger())
    idp = identity_provider_class(parent=app, token=token)
    app.identity_provider = idp

    with caplog.at_level(logging.WARNING):
        idp.validate_security(app, ssl_options=ssl)
    for record in caplog.records:
        print(record)

    if warns:
        assert len(caplog.records) > 0
        if isinstance(warns, str):
            logged = "\n".join(record.msg for record in caplog.records)
            assert warns in logged
    else:
        assert len(caplog.records) == 0
def jp_asyncio_patch():
    """Appropriately configures the event loop policy if running on Windows w/ Python >= 3.8."""
    ServerApp()._init_asyncio_patch()
Defaults for these options can also be set by creating a file named
``jupyter_server_config.py`` in your Jupyter folder. The Jupyter
folder is in your home directory, ``~/.jupyter``.

To create a ``jupyter_server_config.py`` file, with all the defaults
commented out, you can use the following command line::

  $ jupyter server --generate-config


.. _options:

Options
-------

This list of options can be generated by running the following and hitting
enter::

  $ jupyter server --help-all

"""
try:
    destination = os.path.join(os.path.dirname(__file__),
                               "source/other/full-config.rst")
except:
    destination = os.path.join(os.getcwd(), "full-config.rst")

with open(destination, "w") as f:
    f.write(header)
    f.write(ServerApp().document_config_options())
Пример #13
0
 def asyncio_patch():
     ServerApp()._init_asyncio_patch()