From dd32582da66419655a0d002f736d031624e7352d 2013-10-17 21:09:18
From: Thomas Kluyver <takowl@gmail.com>
Date: 2013-10-17 21:09:18
Subject: [PATCH] Add failing test for listing nonexistant directory

---

diff --git a/IPython/html/services/notebooks/tests/test_notebooks_api.py b/IPython/html/services/notebooks/tests/test_notebooks_api.py
index 842fa16..65b7f12 100644
--- a/IPython/html/services/notebooks/tests/test_notebooks_api.py
+++ b/IPython/html/services/notebooks/tests/test_notebooks_api.py
@@ -13,7 +13,7 @@ pjoin = os.path.join
 import requests
 
 from IPython.html.utils import url_path_join
-from IPython.html.tests.launchnotebook import NotebookTestBase
+from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
 from IPython.nbformat.current import (new_notebook, write, read, new_worksheet,
                                       new_heading_cell, to_notebook_json)
 from IPython.utils.data import uniq_stable
@@ -119,13 +119,9 @@ class APITest(NotebookTestBase):
         expected = { normalize('NFC', name) for name in expected }
         self.assertEqual(nbnames, expected)
 
-    def assert_404(self, name, path):
-        try:
-            self.nb_api.read(name, path)
-        except requests.HTTPError as e:
-            self.assertEqual(e.response.status_code, 404)
-        else:
-            assert False, "Reading a non-existent notebook should fail"
+    def test_list_nonexistant_dir(self):
+        with assert_http_error(404):
+            self.nb_api.list('nonexistant')
 
     def test_get_contents(self):
         for d, name in self.dirs_nbs:
@@ -136,7 +132,8 @@ class APITest(NotebookTestBase):
             self.assertIsInstance(nb['content']['metadata'], dict)
 
         # Name that doesn't exist - should be a 404
-        self.assert_404('q.ipynb', 'foo')
+        with assert_http_error(404):
+            self.nb_api.read('q.ipynb', 'foo')
 
     def _check_nb_created(self, resp, name, path):
         self.assertEqual(resp.status_code, 201)
@@ -212,4 +209,5 @@ class APITest(NotebookTestBase):
         self.assertEqual(saved['path'], 'foo/bar')
         assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb'))
         assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb'))
-        self.assert_404('a.ipynb', 'foo')
\ No newline at end of file
+        with assert_http_error(404):
+            self.nb_api.read('a.ipynb', 'foo')
\ No newline at end of file
diff --git a/IPython/html/services/sessions/tests/test_sessions_api.py b/IPython/html/services/sessions/tests/test_sessions_api.py
index 743789e..e36cdd7 100644
--- a/IPython/html/services/sessions/tests/test_sessions_api.py
+++ b/IPython/html/services/sessions/tests/test_sessions_api.py
@@ -9,7 +9,7 @@ import shutil
 pjoin = os.path.join
 
 from IPython.html.utils import url_path_join
-from IPython.html.tests.launchnotebook import NotebookTestBase
+from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
 from IPython.nbformat.current import new_notebook, write
 
 class SessionAPI(object):
@@ -64,14 +64,6 @@ class SessionAPITest(NotebookTestBase):
             self.sess_api.delete(session['id'])
         shutil.rmtree(pjoin(self.notebook_dir.name, 'foo'))
 
-    def assert_404(self, id):
-        try:
-            self.sess_api.get(id)
-        except requests.HTTPError as e:
-            self.assertEqual(e.response.status_code, 404)
-        else:
-            assert False, "Getting nonexistent session didn't give HTTP error"
-
     def test_create(self):
         sessions = self.sess_api.list().json()
         self.assertEqual(len(sessions), 0)
@@ -101,7 +93,8 @@ class SessionAPITest(NotebookTestBase):
         sessions = self.sess_api.list().json()
         self.assertEqual(sessions, [])
 
-        self.assert_404(sid)
+        with assert_http_error(404):
+            self.sess_api.get(sid)
 
     def test_modify(self):
         newsession = self.sess_api.create('nb1.ipynb', 'foo').json()
diff --git a/IPython/html/tests/launchnotebook.py b/IPython/html/tests/launchnotebook.py
index 661502c..078b7d0 100644
--- a/IPython/html/tests/launchnotebook.py
+++ b/IPython/html/tests/launchnotebook.py
@@ -3,12 +3,12 @@
 import sys
 import time
 import requests
+from contextlib import contextmanager
 from subprocess import Popen, PIPE
 from unittest import TestCase
 
 from IPython.utils.tempdir import TemporaryDirectory
 
-
 class NotebookTestBase(TestCase):
     """A base class for tests that need a running notebook.
     
@@ -67,3 +67,15 @@ class NotebookTestBase(TestCase):
     @classmethod
     def base_url(cls):
         return 'http://localhost:%i/' % cls.port
+
+
+@contextmanager
+def assert_http_error(status):
+    try:
+        yield
+    except requests.HTTPError as e:
+        real_status = e.response.status_code
+        assert real_status == status, \
+                    "Expected status %d, got %d" % (real_status, status)
+    else:
+        assert False, "Expected HTTP error status"
\ No newline at end of file