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