test_notebooks_api.py
113 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
Zachary Sailer
|
r13044 | """Test the notebooks webservice API.""" | ||
Zachary Sailer
|
r13041 | |||
import os | ||||
import sys | ||||
import json | ||||
from zmq.utils import jsonapi | ||||
import requests | ||||
Zachary Sailer
|
r13048 | from IPython.html.utils import url_path_join | ||
Zachary Sailer
|
r13041 | from IPython.html.tests.launchnotebook import NotebookTestBase | ||
class APITest(NotebookTestBase): | ||||
"""Test the kernels web service API""" | ||||
Zachary Sailer
|
r13044 | def notebook_url(self): | ||
Zachary Sailer
|
r13048 | return url_path_join(super(APITest,self).base_url(), 'api/notebooks') | ||
Zachary Sailer
|
r13041 | |||
def mknb(self, name='', path='/'): | ||||
Zachary Sailer
|
r13044 | url = self.notebook_url() + path | ||
Zachary Sailer
|
r13041 | return url, requests.post(url) | ||
def delnb(self, name, path='/'): | ||||
Zachary Sailer
|
r13044 | url = self.notebook_url() + path + name | ||
Zachary Sailer
|
r13041 | r = requests.delete(url) | ||
return r.status_code | ||||
Zachary Sailer
|
r13045 | def test_notebook_handler(self): | ||
Zachary Sailer
|
r13041 | # POST a notebook and test the dict thats returned. | ||
Zachary Sailer
|
r13044 | #url, nb = self.mknb() | ||
url = self.notebook_url() | ||||
Zachary Sailer
|
r13046 | nb = requests.post(url+'/') | ||
print nb.text | ||||
Zachary Sailer
|
r13041 | data = nb.json() | ||
assert isinstance(data, dict) | ||||
Zachary Sailer
|
r13045 | self.assertIn('name', data) | ||
self.assertIn('path', data) | ||||
Zachary Sailer
|
r13041 | self.assertEqual(data['name'], u'Untitled0.ipynb') | ||
self.assertEqual(data['path'], u'/') | ||||
# GET list of notebooks in directory. | ||||
r = requests.get(url) | ||||
assert isinstance(r.json(), list) | ||||
assert isinstance(r.json()[0], dict) | ||||
Zachary Sailer
|
r13044 | |||
self.delnb('Untitled0.ipynb') | ||||
Zachary Sailer
|
r13045 | |||
Zachary Sailer
|
r13041 | # GET with a notebook name. | ||
Zachary Sailer
|
r13043 | url, nb = self.mknb() | ||
data = nb.json() | ||||
Zachary Sailer
|
r13044 | url = self.notebook_url() + '/Untitled0.ipynb' | ||
Zachary Sailer
|
r13041 | r = requests.get(url) | ||
assert isinstance(data, dict) | ||||
# PATCH (rename) request. | ||||
new_name = {'name':'test.ipynb'} | ||||
r = requests.patch(url, data=jsonapi.dumps(new_name)) | ||||
data = r.json() | ||||
assert isinstance(data, dict) | ||||
# make sure the patch worked. | ||||
Zachary Sailer
|
r13044 | new_url = self.notebook_url() + '/test.ipynb' | ||
Zachary Sailer
|
r13041 | r = requests.get(new_url) | ||
assert isinstance(r.json(), dict) | ||||
# GET bad (old) notebook name. | ||||
r = requests.get(url) | ||||
self.assertEqual(r.status_code, 404) | ||||
# POST notebooks to folders one and two levels down. | ||||
os.makedirs(os.path.join(self.notebook_dir.name, 'foo')) | ||||
os.makedirs(os.path.join(self.notebook_dir.name, 'foo','bar')) | ||||
Zachary Sailer
|
r13044 | assert os.path.isdir(os.path.join(self.notebook_dir.name, 'foo')) | ||
Zachary Sailer
|
r13041 | url, nb = self.mknb(path='/foo/') | ||
url2, nb2 = self.mknb(path='/foo/bar/') | ||||
data = nb.json() | ||||
data2 = nb2.json() | ||||
assert isinstance(data, dict) | ||||
assert isinstance(data2, dict) | ||||
Zachary Sailer
|
r13045 | self.assertIn('name', data) | ||
self.assertIn('path', data) | ||||
Zachary Sailer
|
r13041 | self.assertEqual(data['name'], u'Untitled0.ipynb') | ||
self.assertEqual(data['path'], u'/foo/') | ||||
Zachary Sailer
|
r13045 | self.assertIn('name', data2) | ||
self.assertIn('path', data2) | ||||
Zachary Sailer
|
r13041 | self.assertEqual(data2['name'], u'Untitled0.ipynb') | ||
self.assertEqual(data2['path'], u'/foo/bar/') | ||||
# GET request on notebooks one and two levels down. | ||||
Zachary Sailer
|
r13045 | r = requests.get(url+'/Untitled0.ipynb') | ||
r2 = requests.get(url2+'/Untitled0.ipynb') | ||||
Zachary Sailer
|
r13041 | assert isinstance(r.json(), dict) | ||
assert isinstance(r2.json(), dict) | ||||
# PATCH notebooks that are one and two levels down. | ||||
new_name = {'name': 'testfoo.ipynb'} | ||||
Zachary Sailer
|
r13045 | r = requests.patch(url+'/Untitled0.ipynb', data=jsonapi.dumps(new_name)) | ||
r = requests.get(url+'/testfoo.ipynb') | ||||
Zachary Sailer
|
r13041 | data = r.json() | ||
assert isinstance(data, dict) | ||||
Zachary Sailer
|
r13045 | self.assertIn('name', data) | ||
Zachary Sailer
|
r13041 | self.assertEqual(data['name'], 'testfoo.ipynb') | ||
Zachary Sailer
|
r13045 | r = requests.get(url+'/Untitled0.ipynb') | ||
Zachary Sailer
|
r13041 | self.assertEqual(r.status_code, 404) | ||
# DELETE notebooks | ||||
Zachary Sailer
|
r13044 | r0 = self.delnb('test.ipynb') | ||
r1 = self.delnb('testfoo.ipynb', '/foo/') | ||||
r2 = self.delnb('Untitled0.ipynb', '/foo/bar/') | ||||
self.assertEqual(r0, 204) | ||||
self.assertEqual(r1, 204) | ||||
self.assertEqual(r2, 204) | ||||