Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1 @@
from .manager import ConfigManager

View file

@ -0,0 +1,39 @@
"""Tornado handlers for frontend config storage."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import os
import io
import errno
from tornado import web
from ...base.handlers import APIHandler
class ConfigHandler(APIHandler):
@web.authenticated
def get(self, section_name):
self.set_header("Content-Type", 'application/json')
self.finish(json.dumps(self.config_manager.get(section_name)))
@web.authenticated
def put(self, section_name):
data = self.get_json_body() # Will raise 400 if content is not valid JSON
self.config_manager.set(section_name, data)
self.set_status(204)
@web.authenticated
def patch(self, section_name):
new_data = self.get_json_body()
section = self.config_manager.update(section_name, new_data)
self.finish(json.dumps(section))
# URL to handler mappings
section_name_regex = r"(?P<section_name>\w+)"
default_handlers = [
(r"/api/config/%s" % section_name_regex, ConfigHandler),
]

View file

@ -0,0 +1,58 @@
"""Manager to read and modify frontend config data in JSON files.
"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os.path
from notebook.config_manager import BaseJSONConfigManager, recursive_update
from jupyter_core.paths import jupyter_config_dir, jupyter_config_path
from traitlets import Unicode, Instance, List, observe, default
from traitlets.config import LoggingConfigurable
class ConfigManager(LoggingConfigurable):
"""Config Manager used for storing notebook frontend config"""
# Public API
def get(self, section_name):
"""Get the config from all config sections."""
config = {}
# step through back to front, to ensure front of the list is top priority
for p in self.read_config_path[::-1]:
cm = BaseJSONConfigManager(config_dir=p)
recursive_update(config, cm.get(section_name))
return config
def set(self, section_name, data):
"""Set the config only to the user's config."""
return self.write_config_manager.set(section_name, data)
def update(self, section_name, new_data):
"""Update the config only to the user's config."""
return self.write_config_manager.update(section_name, new_data)
# Private API
read_config_path = List(Unicode())
@default('read_config_path')
def _default_read_config_path(self):
return [os.path.join(p, 'nbconfig') for p in jupyter_config_path()]
write_config_dir = Unicode()
@default('write_config_dir')
def _default_write_config_dir(self):
return os.path.join(jupyter_config_dir(), 'nbconfig')
write_config_manager = Instance(BaseJSONConfigManager)
@default('write_config_manager')
def _default_write_config_manager(self):
return BaseJSONConfigManager(config_dir=self.write_config_dir)
@observe('write_config_dir')
def _update_write_config_dir(self, change):
self.write_config_manager = BaseJSONConfigManager(config_dir=self.write_config_dir)

View file

@ -0,0 +1,68 @@
# coding: utf-8
"""Test the config webservice API."""
import json
import requests
from notebook.utils import url_path_join
from notebook.tests.launchnotebook import NotebookTestBase
class ConfigAPI(object):
"""Wrapper for notebook API calls."""
def __init__(self, request):
self.request = request
def _req(self, verb, section, body=None):
response = self.request(verb,
url_path_join('api/config', section),
data=body,
)
response.raise_for_status()
return response
def get(self, section):
return self._req('GET', section)
def set(self, section, values):
return self._req('PUT', section, json.dumps(values))
def modify(self, section, values):
return self._req('PATCH', section, json.dumps(values))
class APITest(NotebookTestBase):
"""Test the config web service API"""
def setUp(self):
self.config_api = ConfigAPI(self.request)
def test_create_retrieve_config(self):
sample = {'foo': 'bar', 'baz': 73}
r = self.config_api.set('example', sample)
self.assertEqual(r.status_code, 204)
r = self.config_api.get('example')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json(), sample)
def test_modify(self):
sample = {'foo': 'bar', 'baz': 73,
'sub': {'a': 6, 'b': 7}, 'sub2': {'c': 8}}
self.config_api.set('example', sample)
r = self.config_api.modify('example', {'foo': None, # should delete foo
'baz': 75,
'wib': [1,2,3],
'sub': {'a': 8, 'b': None, 'd': 9},
'sub2': {'c': None} # should delete sub2
})
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json(), {'baz': 75, 'wib': [1,2,3],
'sub': {'a': 8, 'd': 9}})
def test_get_unknown(self):
# We should get an empty config dictionary instead of a 404
r = self.config_api.get('nonexistant')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json(), {})