##// END OF EJS Templates
Update CSP tests for new default.
Kyle Kelley -
Show More
@@ -1,133 +1,139 b''
1 """Test the kernels service API."""
1 """Test the kernels service API."""
2
2
3 import json
3 import json
4 import requests
4 import requests
5
5
6 from IPython.html.utils import url_path_join
6 from IPython.html.utils import url_path_join
7 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
7 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
8
8
9 class KernelAPI(object):
9 class KernelAPI(object):
10 """Wrapper for kernel REST API requests"""
10 """Wrapper for kernel REST API requests"""
11 def __init__(self, base_url):
11 def __init__(self, base_url):
12 self.base_url = base_url
12 self.base_url = base_url
13
13
14 def _req(self, verb, path, body=None):
14 def _req(self, verb, path, body=None):
15 response = requests.request(verb,
15 response = requests.request(verb,
16 url_path_join(self.base_url, 'api/kernels', path), data=body)
16 url_path_join(self.base_url, 'api/kernels', path), data=body)
17
17
18 if 400 <= response.status_code < 600:
18 if 400 <= response.status_code < 600:
19 try:
19 try:
20 response.reason = response.json()['message']
20 response.reason = response.json()['message']
21 except:
21 except:
22 pass
22 pass
23 response.raise_for_status()
23 response.raise_for_status()
24
24
25 return response
25 return response
26
26
27 def list(self):
27 def list(self):
28 return self._req('GET', '')
28 return self._req('GET', '')
29
29
30 def get(self, id):
30 def get(self, id):
31 return self._req('GET', id)
31 return self._req('GET', id)
32
32
33 def start(self, name='python'):
33 def start(self, name='python'):
34 body = json.dumps({'name': name})
34 body = json.dumps({'name': name})
35 return self._req('POST', '', body)
35 return self._req('POST', '', body)
36
36
37 def shutdown(self, id):
37 def shutdown(self, id):
38 return self._req('DELETE', id)
38 return self._req('DELETE', id)
39
39
40 def interrupt(self, id):
40 def interrupt(self, id):
41 return self._req('POST', url_path_join(id, 'interrupt'))
41 return self._req('POST', url_path_join(id, 'interrupt'))
42
42
43 def restart(self, id):
43 def restart(self, id):
44 return self._req('POST', url_path_join(id, 'restart'))
44 return self._req('POST', url_path_join(id, 'restart'))
45
45
46 class KernelAPITest(NotebookTestBase):
46 class KernelAPITest(NotebookTestBase):
47 """Test the kernels web service API"""
47 """Test the kernels web service API"""
48 def setUp(self):
48 def setUp(self):
49 self.kern_api = KernelAPI(self.base_url())
49 self.kern_api = KernelAPI(self.base_url())
50
50
51 def tearDown(self):
51 def tearDown(self):
52 for k in self.kern_api.list().json():
52 for k in self.kern_api.list().json():
53 self.kern_api.shutdown(k['id'])
53 self.kern_api.shutdown(k['id'])
54
54
55 def test__no_kernels(self):
55 def test__no_kernels(self):
56 """Make sure there are no kernels running at the start"""
56 """Make sure there are no kernels running at the start"""
57 kernels = self.kern_api.list().json()
57 kernels = self.kern_api.list().json()
58 self.assertEqual(kernels, [])
58 self.assertEqual(kernels, [])
59
59
60 def test_default_kernel(self):
60 def test_default_kernel(self):
61 # POST request
61 # POST request
62 r = self.kern_api._req('POST', '')
62 r = self.kern_api._req('POST', '')
63 kern1 = r.json()
63 kern1 = r.json()
64 self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id'])
64 self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id'])
65 self.assertEqual(r.status_code, 201)
65 self.assertEqual(r.status_code, 201)
66 self.assertIsInstance(kern1, dict)
66 self.assertIsInstance(kern1, dict)
67
67
68 self.assertEqual(r.headers['Content-Security-Policy'], "frame-ancestors 'self'")
68 self.assertEqual(r.headers['Content-Security-Policy'], (
69 "frame-ancestors 'self'; "
70 "report-uri /api/security/csp-report;"
71 ))
69
72
70 def test_main_kernel_handler(self):
73 def test_main_kernel_handler(self):
71 # POST request
74 # POST request
72 r = self.kern_api.start()
75 r = self.kern_api.start()
73 kern1 = r.json()
76 kern1 = r.json()
74 self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id'])
77 self.assertEqual(r.headers['location'], '/api/kernels/' + kern1['id'])
75 self.assertEqual(r.status_code, 201)
78 self.assertEqual(r.status_code, 201)
76 self.assertIsInstance(kern1, dict)
79 self.assertIsInstance(kern1, dict)
77
80
78 self.assertEqual(r.headers['Content-Security-Policy'], "frame-ancestors 'self'")
81 self.assertEqual(r.headers['Content-Security-Policy'], (
82 "frame-ancestors 'self'; "
83 "report-uri /api/security/csp-report;"
84 ))
79
85
80 # GET request
86 # GET request
81 r = self.kern_api.list()
87 r = self.kern_api.list()
82 self.assertEqual(r.status_code, 200)
88 self.assertEqual(r.status_code, 200)
83 assert isinstance(r.json(), list)
89 assert isinstance(r.json(), list)
84 self.assertEqual(r.json()[0]['id'], kern1['id'])
90 self.assertEqual(r.json()[0]['id'], kern1['id'])
85 self.assertEqual(r.json()[0]['name'], kern1['name'])
91 self.assertEqual(r.json()[0]['name'], kern1['name'])
86
92
87 # create another kernel and check that they both are added to the
93 # create another kernel and check that they both are added to the
88 # list of kernels from a GET request
94 # list of kernels from a GET request
89 kern2 = self.kern_api.start().json()
95 kern2 = self.kern_api.start().json()
90 assert isinstance(kern2, dict)
96 assert isinstance(kern2, dict)
91 r = self.kern_api.list()
97 r = self.kern_api.list()
92 kernels = r.json()
98 kernels = r.json()
93 self.assertEqual(r.status_code, 200)
99 self.assertEqual(r.status_code, 200)
94 assert isinstance(kernels, list)
100 assert isinstance(kernels, list)
95 self.assertEqual(len(kernels), 2)
101 self.assertEqual(len(kernels), 2)
96
102
97 # Interrupt a kernel
103 # Interrupt a kernel
98 r = self.kern_api.interrupt(kern2['id'])
104 r = self.kern_api.interrupt(kern2['id'])
99 self.assertEqual(r.status_code, 204)
105 self.assertEqual(r.status_code, 204)
100
106
101 # Restart a kernel
107 # Restart a kernel
102 r = self.kern_api.restart(kern2['id'])
108 r = self.kern_api.restart(kern2['id'])
103 self.assertEqual(r.headers['Location'], '/api/kernels/'+kern2['id'])
109 self.assertEqual(r.headers['Location'], '/api/kernels/'+kern2['id'])
104 rekern = r.json()
110 rekern = r.json()
105 self.assertEqual(rekern['id'], kern2['id'])
111 self.assertEqual(rekern['id'], kern2['id'])
106 self.assertEqual(rekern['name'], kern2['name'])
112 self.assertEqual(rekern['name'], kern2['name'])
107
113
108 def test_kernel_handler(self):
114 def test_kernel_handler(self):
109 # GET kernel with given id
115 # GET kernel with given id
110 kid = self.kern_api.start().json()['id']
116 kid = self.kern_api.start().json()['id']
111 r = self.kern_api.get(kid)
117 r = self.kern_api.get(kid)
112 kern1 = r.json()
118 kern1 = r.json()
113 self.assertEqual(r.status_code, 200)
119 self.assertEqual(r.status_code, 200)
114 assert isinstance(kern1, dict)
120 assert isinstance(kern1, dict)
115 self.assertIn('id', kern1)
121 self.assertIn('id', kern1)
116 self.assertEqual(kern1['id'], kid)
122 self.assertEqual(kern1['id'], kid)
117
123
118 # Request a bad kernel id and check that a JSON
124 # Request a bad kernel id and check that a JSON
119 # message is returned!
125 # message is returned!
120 bad_id = '111-111-111-111-111'
126 bad_id = '111-111-111-111-111'
121 with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
127 with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
122 self.kern_api.get(bad_id)
128 self.kern_api.get(bad_id)
123
129
124 # DELETE kernel with id
130 # DELETE kernel with id
125 r = self.kern_api.shutdown(kid)
131 r = self.kern_api.shutdown(kid)
126 self.assertEqual(r.status_code, 204)
132 self.assertEqual(r.status_code, 204)
127 kernels = self.kern_api.list().json()
133 kernels = self.kern_api.list().json()
128 self.assertEqual(kernels, [])
134 self.assertEqual(kernels, [])
129
135
130 # Request to delete a non-existent kernel id
136 # Request to delete a non-existent kernel id
131 bad_id = '111-111-111-111-111'
137 bad_id = '111-111-111-111-111'
132 with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
138 with assert_http_error(404, 'Kernel does not exist: ' + bad_id):
133 self.kern_api.shutdown(bad_id)
139 self.kern_api.shutdown(bad_id)
General Comments 0
You need to be logged in to leave comments. Login now