##// END OF EJS Templates
Merge branch csp into 3.x...
Merge branch csp into 3.x Add APIHandler

File last commit:

r21472:f2d874c5
r21487:7222bd53 merge
Show More
test_kernels_api.py
141 lines | 4.7 KiB | text/x-python | PythonLexer
Brian E. Granger
Added base class for Notebook API tests.
r13038 """Test the kernels service API."""
Thomas Kluyver
Add support for different kernel specs to kernels REST API
r17221 import json
Brian E. Granger
Added base class for Notebook API tests.
r13038 import requests
Zachary Sailer
Code review changes....
r13048 from IPython.html.utils import url_path_join
Thomas Kluyver
Refactor tests for kernels REST API
r13105 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
Brian E. Granger
Added base class for Notebook API tests.
r13038
Thomas Kluyver
Refactor tests for kernels REST API
r13105 class KernelAPI(object):
"""Wrapper for kernel REST API requests"""
def __init__(self, base_url):
self.base_url = base_url
def _req(self, verb, path, body=None):
response = requests.request(verb,
url_path_join(self.base_url, 'api/kernels', path), data=body)
if 400 <= response.status_code < 600:
try:
response.reason = response.json()['message']
except:
pass
response.raise_for_status()
return response
def list(self):
return self._req('GET', '')
def get(self, id):
return self._req('GET', id)
Thomas Kluyver
Add support for different kernel specs to kernels REST API
r17221 def start(self, name='python'):
body = json.dumps({'name': name})
return self._req('POST', '', body)
Thomas Kluyver
Refactor tests for kernels REST API
r13105
def shutdown(self, id):
return self._req('DELETE', id)
def interrupt(self, id):
return self._req('POST', url_path_join(id, 'interrupt'))
def restart(self, id):
return self._req('POST', url_path_join(id, 'restart'))
Brian E. Granger
Added base class for Notebook API tests.
r13038
class KernelAPITest(NotebookTestBase):
"""Test the kernels web service API"""
Thomas Kluyver
Refactor tests for kernels REST API
r13105 def setUp(self):
self.kern_api = KernelAPI(self.base_url())
Brian E. Granger
Added base class for Notebook API tests.
r13038
Thomas Kluyver
Refactor tests for kernels REST API
r13105 def tearDown(self):
for k in self.kern_api.list().json():
self.kern_api.shutdown(k['id'])
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045
MinRK
ensure test_no_kernels runs first...
r13050 def test__no_kernels(self):
Brian E. Granger
Added base class for Notebook API tests.
r13038 """Make sure there are no kernels running at the start"""
Thomas Kluyver
Refactor tests for kernels REST API
r13105 kernels = self.kern_api.list().json()
self.assertEqual(kernels, [])
Zachary Sailer
add tests for session api
r13044
MinRK
use default kernel name in kernels service...
r18308 def test_default_kernel(self):
# POST request
r = self.kern_api._req('POST', '')
kern1 = r.json()
self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id'])
self.assertEqual(r.status_code, 201)
self.assertIsInstance(kern1, dict)
Kyle Kelley
Update CSP tests for new default.
r19154 self.assertEqual(r.headers['Content-Security-Policy'], (
"frame-ancestors 'self'; "
Min RK
make content_security_policy a property...
r21472 "report-uri /api/security/csp-report; "
"default-src 'none'"
Kyle Kelley
Update CSP tests for new default.
r19154 ))
MinRK
use default kernel name in kernels service...
r18308
Zachary Sailer
add tests for session api
r13044 def test_main_kernel_handler(self):
# POST request
Thomas Kluyver
Refactor tests for kernels REST API
r13105 r = self.kern_api.start()
kern1 = r.json()
Thomas Kluyver
Use assertEqual, not deprecated assertEquals
r13107 self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id'])
self.assertEqual(r.status_code, 201)
Thomas Kluyver
Refactor tests for kernels REST API
r13105 self.assertIsInstance(kern1, dict)
Zachary Sailer
add tests for session api
r13044
Kyle Kelley
Update CSP tests for new default.
r19154 self.assertEqual(r.headers['Content-Security-Policy'], (
"frame-ancestors 'self'; "
Min RK
make content_security_policy a property...
r21472 "report-uri /api/security/csp-report; "
"default-src 'none'"
Kyle Kelley
Update CSP tests for new default.
r19154 ))
rgbkrk
Only allow iframe embedding on same origin.
r17235
Zachary Sailer
add tests for session api
r13044 # GET request
Thomas Kluyver
Refactor tests for kernels REST API
r13105 r = self.kern_api.list()
Thomas Kluyver
Use assertEqual, not deprecated assertEquals
r13107 self.assertEqual(r.status_code, 200)
Zachary Sailer
add tests for session api
r13044 assert isinstance(r.json(), list)
Thomas Kluyver
Refactor tests for kernels REST API
r13105 self.assertEqual(r.json()[0]['id'], kern1['id'])
Thomas Kluyver
Add support for different kernel specs to kernels REST API
r17221 self.assertEqual(r.json()[0]['name'], kern1['name'])
Thomas Kluyver
Refactor tests for kernels REST API
r13105
# create another kernel and check that they both are added to the
Zachary Sailer
adding to test_kernels_api.py...
r13058 # list of kernels from a GET request
Thomas Kluyver
Refactor tests for kernels REST API
r13105 kern2 = self.kern_api.start().json()
assert isinstance(kern2, dict)
r = self.kern_api.list()
Zachary Sailer
adding to test_kernels_api.py...
r13058 kernels = r.json()
Thomas Kluyver
Use assertEqual, not deprecated assertEquals
r13107 self.assertEqual(r.status_code, 200)
Zachary Sailer
adding to test_kernels_api.py...
r13058 assert isinstance(kernels, list)
Thomas Kluyver
Use assertEqual, not deprecated assertEquals
r13107 self.assertEqual(len(kernels), 2)
Zachary Sailer
add tests for session api
r13044
Thomas Kluyver
Add tests for interrupting & restarting kernel...
r13106 # Interrupt a kernel
r = self.kern_api.interrupt(kern2['id'])
self.assertEqual(r.status_code, 204)
# Restart a kernel
r = self.kern_api.restart(kern2['id'])
self.assertEqual(r.headers['Location'], '/api/kernels/'+kern2['id'])
rekern = r.json()
self.assertEqual(rekern['id'], kern2['id'])
Thomas Kluyver
Add support for different kernel specs to kernels REST API
r17221 self.assertEqual(rekern['name'], kern2['name'])
Thomas Kluyver
Add tests for interrupting & restarting kernel...
r13106
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 def test_kernel_handler(self):
Zachary Sailer
adding to test_kernels_api.py...
r13058 # GET kernel with given id
Thomas Kluyver
Refactor tests for kernels REST API
r13105 kid = self.kern_api.start().json()['id']
r = self.kern_api.get(kid)
kern1 = r.json()
Thomas Kluyver
Use assertEqual, not deprecated assertEquals
r13107 self.assertEqual(r.status_code, 200)
Thomas Kluyver
Refactor tests for kernels REST API
r13105 assert isinstance(kern1, dict)
self.assertIn('id', kern1)
self.assertEqual(kern1['id'], kid)
Zachary Sailer
adding to test_kernels_api.py...
r13058 # Request a bad kernel id and check that a JSON
# message is returned!
bad_id = '111-111-111-111-111'
Thomas Kluyver
Refactor tests for kernels REST API
r13105 with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
self.kern_api.get(bad_id)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 # DELETE kernel with id
Thomas Kluyver
Refactor tests for kernels REST API
r13105 r = self.kern_api.shutdown(kid)
Zachary Sailer
review fixes on tests, add extra kernel api test
r13045 self.assertEqual(r.status_code, 204)
Thomas Kluyver
Refactor tests for kernels REST API
r13105 kernels = self.kern_api.list().json()
self.assertEqual(kernels, [])
Zachary Sailer
adding to test_kernels_api.py...
r13058 # Request to delete a non-existent kernel id
bad_id = '111-111-111-111-111'
Thomas Kluyver
Refactor tests for kernels REST API
r13105 with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
self.kern_api.shutdown(bad_id)