##// END OF EJS Templates
Make sessions REST API test more robust....
Thomas Kluyver -
Show More
@@ -1,107 +1,114 b''
1 """Test the sessions web service API."""
1 """Test the sessions web service API."""
2
2
3 import errno
3 import io
4 import io
4 import os
5 import os
5 import json
6 import json
6 import requests
7 import requests
7 import shutil
8 import shutil
8
9
9 pjoin = os.path.join
10 pjoin = os.path.join
10
11
11 from IPython.html.utils import url_path_join
12 from IPython.html.utils import url_path_join
12 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
13 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
13 from IPython.nbformat.current import new_notebook, write
14 from IPython.nbformat.current import new_notebook, write
14
15
15 class SessionAPI(object):
16 class SessionAPI(object):
16 """Wrapper for notebook API calls."""
17 """Wrapper for notebook API calls."""
17 def __init__(self, base_url):
18 def __init__(self, base_url):
18 self.base_url = base_url
19 self.base_url = base_url
19
20
20 def _req(self, verb, path, body=None):
21 def _req(self, verb, path, body=None):
21 response = requests.request(verb,
22 response = requests.request(verb,
22 url_path_join(self.base_url, 'api/sessions', path), data=body)
23 url_path_join(self.base_url, 'api/sessions', path), data=body)
23
24
24 if 400 <= response.status_code < 600:
25 if 400 <= response.status_code < 600:
25 try:
26 try:
26 response.reason = response.json()['message']
27 response.reason = response.json()['message']
27 except:
28 except:
28 pass
29 pass
29 response.raise_for_status()
30 response.raise_for_status()
30
31
31 return response
32 return response
32
33
33 def list(self):
34 def list(self):
34 return self._req('GET', '')
35 return self._req('GET', '')
35
36
36 def get(self, id):
37 def get(self, id):
37 return self._req('GET', id)
38 return self._req('GET', id)
38
39
39 def create(self, name, path):
40 def create(self, name, path):
40 body = json.dumps({'notebook': {'name':name, 'path':path}})
41 body = json.dumps({'notebook': {'name':name, 'path':path}})
41 return self._req('POST', '', body)
42 return self._req('POST', '', body)
42
43
43 def modify(self, id, name, path):
44 def modify(self, id, name, path):
44 body = json.dumps({'notebook': {'name':name, 'path':path}})
45 body = json.dumps({'notebook': {'name':name, 'path':path}})
45 return self._req('PATCH', id, body)
46 return self._req('PATCH', id, body)
46
47
47 def delete(self, id):
48 def delete(self, id):
48 return self._req('DELETE', id)
49 return self._req('DELETE', id)
49
50
50 class SessionAPITest(NotebookTestBase):
51 class SessionAPITest(NotebookTestBase):
51 """Test the sessions web service API"""
52 """Test the sessions web service API"""
52 def setUp(self):
53 def setUp(self):
53 nbdir = self.notebook_dir.name
54 nbdir = self.notebook_dir.name
54 os.mkdir(pjoin(nbdir, 'foo'))
55 try:
56 os.mkdir(pjoin(nbdir, 'foo'))
57 except OSError as e:
58 # Deleting the folder in an earlier test may have failed
59 if e.errno != errno.EEXIST:
60 raise
55
61
56 with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w') as f:
62 with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w') as f:
57 nb = new_notebook(name='nb1')
63 nb = new_notebook(name='nb1')
58 write(nb, f, format='ipynb')
64 write(nb, f, format='ipynb')
59
65
60 self.sess_api = SessionAPI(self.base_url())
66 self.sess_api = SessionAPI(self.base_url())
61
67
62 def tearDown(self):
68 def tearDown(self):
63 for session in self.sess_api.list().json():
69 for session in self.sess_api.list().json():
64 self.sess_api.delete(session['id'])
70 self.sess_api.delete(session['id'])
65 shutil.rmtree(pjoin(self.notebook_dir.name, 'foo'))
71 shutil.rmtree(pjoin(self.notebook_dir.name, 'foo'),
72 ignore_errors=True)
66
73
67 def test_create(self):
74 def test_create(self):
68 sessions = self.sess_api.list().json()
75 sessions = self.sess_api.list().json()
69 self.assertEqual(len(sessions), 0)
76 self.assertEqual(len(sessions), 0)
70
77
71 resp = self.sess_api.create('nb1.ipynb', 'foo')
78 resp = self.sess_api.create('nb1.ipynb', 'foo')
72 self.assertEqual(resp.status_code, 201)
79 self.assertEqual(resp.status_code, 201)
73 newsession = resp.json()
80 newsession = resp.json()
74 self.assertIn('id', newsession)
81 self.assertIn('id', newsession)
75 self.assertEqual(newsession['notebook']['name'], 'nb1.ipynb')
82 self.assertEqual(newsession['notebook']['name'], 'nb1.ipynb')
76 self.assertEqual(newsession['notebook']['path'], 'foo')
83 self.assertEqual(newsession['notebook']['path'], 'foo')
77 self.assertEqual(resp.headers['Location'], '/api/sessions/{0}'.format(newsession['id']))
84 self.assertEqual(resp.headers['Location'], '/api/sessions/{0}'.format(newsession['id']))
78
85
79 sessions = self.sess_api.list().json()
86 sessions = self.sess_api.list().json()
80 self.assertEqual(sessions, [newsession])
87 self.assertEqual(sessions, [newsession])
81
88
82 # Retrieve it
89 # Retrieve it
83 sid = newsession['id']
90 sid = newsession['id']
84 got = self.sess_api.get(sid).json()
91 got = self.sess_api.get(sid).json()
85 self.assertEqual(got, newsession)
92 self.assertEqual(got, newsession)
86
93
87 def test_delete(self):
94 def test_delete(self):
88 newsession = self.sess_api.create('nb1.ipynb', 'foo').json()
95 newsession = self.sess_api.create('nb1.ipynb', 'foo').json()
89 sid = newsession['id']
96 sid = newsession['id']
90
97
91 resp = self.sess_api.delete(sid)
98 resp = self.sess_api.delete(sid)
92 self.assertEqual(resp.status_code, 204)
99 self.assertEqual(resp.status_code, 204)
93
100
94 sessions = self.sess_api.list().json()
101 sessions = self.sess_api.list().json()
95 self.assertEqual(sessions, [])
102 self.assertEqual(sessions, [])
96
103
97 with assert_http_error(404):
104 with assert_http_error(404):
98 self.sess_api.get(sid)
105 self.sess_api.get(sid)
99
106
100 def test_modify(self):
107 def test_modify(self):
101 newsession = self.sess_api.create('nb1.ipynb', 'foo').json()
108 newsession = self.sess_api.create('nb1.ipynb', 'foo').json()
102 sid = newsession['id']
109 sid = newsession['id']
103
110
104 changed = self.sess_api.modify(sid, 'nb2.ipynb', '').json()
111 changed = self.sess_api.modify(sid, 'nb2.ipynb', '').json()
105 self.assertEqual(changed['id'], sid)
112 self.assertEqual(changed['id'], sid)
106 self.assertEqual(changed['notebook']['name'], 'nb2.ipynb')
113 self.assertEqual(changed['notebook']['name'], 'nb2.ipynb')
107 self.assertEqual(changed['notebook']['path'], '')
114 self.assertEqual(changed['notebook']['path'], '')
General Comments 0
You need to be logged in to leave comments. Login now