Ejemplo n.º 1
0
    def test_errorresponse(self, mocked_client: JupyterHubClient):
        response = Response()
        response.status_code = 500
        mocked_client.agent.post.return_value = response

        with pytest.raises(JupyterHubCommunicationError):
            mocked_client.start_server()
Ejemplo n.º 2
0
    def test_errorresponse(self, mocked_client: JupyterHubClient):
        response = Response()
        response.status_code = 500
        mocked_client.agent.put.return_value = response

        with pytest.raises(JupyterCommunicationError):
            mocked_client.create_directory('somedir')
Ejemplo n.º 3
0
    def test_offline(self, mocked_client: JupyterHubClient):
        msg = 'Failed to establish a new connection: [Errno 111] Connection refused'
        mocked_client.agent.put.side_effect = ConnectionError(msg)

        with pytest.raises(JupyterCommunicationError):
            mocked_client.upload_notebook(self.notebook, self.path,
                                          self.workspace)
Ejemplo n.º 4
0
    def test_errorresponse(self, mocked_client: JupyterHubClient):
        response = Response()
        response.status_code = 500
        mocked_client.agent.put.return_value = response

        with pytest.raises(JupyterCommunicationError):
            mocked_client.upload_notebook(self.notebook, self.path,
                                          self.workspace)
Ejemplo n.º 5
0
def mocked_client():
    url = 'https://hub.ewatercycle.org'
    token = 'some-random-string'
    username = '******'
    client = JupyterHubClient(url, token, username)
    # Replace http client by mock
    client.agent = Mock()
    return client
Ejemplo n.º 6
0
    def test_created(self, mocked_client: JupyterHubClient):
        response = Response()
        response.status_code = 201
        mocked_client.agent.put.return_value = response

        mocked_client.create_directory('somedir')

        api_url = 'https://hub.ewatercycle.org/user/myusername/api/contents/somedir'
        request_json = {'type': 'directory'}
        mocked_client.agent.put.assert_called_with(api_url, json=request_json)
Ejemplo n.º 7
0
def build_client() -> JupyterHubClient:
    token = current_app.config['JUPYTERHUB_TOKEN']
    jupyterhub_url = current_app.config['JUPYTERHUB_URL']
    if jupyterhub_url.startswith('/') and request.origin:
        jupyterhub_url = request.origin + jupyterhub_url
    username = connexion.context['user']
    return JupyterHubClient(jupyterhub_url, token, username)
Ejemplo n.º 8
0
def test_construct():
    url = 'https://hub.ewatercycle.org'
    token = 'some-random-string'
    username = '******'
    client = JupyterHubClient(url, token, username)

    assert client.agent and client.agent.headers[
        'Authorization'] == 'token ' + token
Ejemplo n.º 9
0
def process_notebook(request: Dict[str, str],
                     notebook: NotebookNode,
                     client: JupyterHubClient = None) -> Dict[str, str]:
    if client is None:
        client = build_client()

    client.start_server()

    directory = request['directory']
    client.create_directory(directory)

    path = directory + '/' + request['filename']

    if 'workspace' in request:
        workspace = request['workspace']
    else:
        workspace = uuid4().hex
    location = client.upload_notebook(notebook, path, workspace)
    return {"location": location}
Ejemplo n.º 10
0
    def test_created(self, mocked_client: JupyterHubClient):
        response = Response()
        response.status_code = 201
        mocked_client.agent.post.return_value = response

        result = mocked_client.start_server()

        api_url = 'https://hub.ewatercycle.org/hub/api/users/myusername/server'
        mocked_client.agent.post.assert_called_with(api_url)
        hub_url = 'https://hub.ewatercycle.org/user/myusername/'
        assert result == hub_url
Ejemplo n.º 11
0
    def test_alreadyrunning(self, mocked_client: JupyterHubClient):
        response = Response()
        response.status_code = 400
        response.raw = BytesIO(b'{"message": "Server is already running"}')
        mocked_client.agent.post.return_value = response

        result = mocked_client.start_server()

        api_url = 'https://hub.ewatercycle.org/hub/api/users/myusername/server'
        mocked_client.agent.post.assert_called_with(api_url)
        hub_url = 'https://hub.ewatercycle.org/user/myusername/'
        assert result == hub_url
Ejemplo n.º 12
0
    def test_upload_ok(self, mocked_client: JupyterHubClient):
        response = Response()
        response.status_code = 201
        mocked_client.agent.put.return_value = response

        nb_url = mocked_client.upload_notebook(self.notebook, self.path,
                                               self.workspace)

        api_url = 'https://hub.ewatercycle.org/user/myusername/api/contents/notebook.ipynb'
        request_json = {
            'type': 'notebook',
            'format': 'json',
            'content': 'notebook contents'
        }
        mocked_client.agent.put.assert_called_with(api_url, json=request_json)
        assert nb_url == 'https://hub.ewatercycle.org/user/myusername/lab/workspaces/bar/tree/notebook.ipynb'
Ejemplo n.º 13
0
    def test_offline(self, mocked_client: JupyterHubClient):
        msg = 'Failed to establish a new connection: [Errno 111] Connection refused'
        mocked_client.agent.put.side_effect = ConnectionError(msg)

        with pytest.raises(JupyterCommunicationError):
            mocked_client.create_directory('somedir')
Ejemplo n.º 14
0
    def test_offline(self, mocked_client: JupyterHubClient):
        msg = 'Failed to establish a new connection: [Errno 111] Connection refused'
        mocked_client.agent.post.side_effect = ConnectionError(msg)

        with pytest.raises(JupyterHubCommunicationError):
            mocked_client.start_server()