test_bundler_api.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """Test the bundlers API."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import io
  5. from os.path import join as pjoin
  6. from notebook.tests.launchnotebook import NotebookTestBase
  7. from nbformat import write
  8. from nbformat.v4 import (
  9. new_notebook, new_markdown_cell, new_code_cell, new_output,
  10. )
  11. try:
  12. from unittest.mock import patch
  13. except ImportError:
  14. from mock import patch # py3
  15. def bundle(handler, model):
  16. """Bundler test stub. Echo the notebook path."""
  17. handler.finish(model['path'])
  18. class BundleAPITest(NotebookTestBase):
  19. """Test the bundlers web service API"""
  20. @classmethod
  21. def setup_class(cls):
  22. """Make a test notebook. Borrowed from nbconvert test. Assumes the class
  23. teardown will clean it up in the end."""
  24. super(BundleAPITest, cls).setup_class()
  25. nbdir = cls.notebook_dir
  26. nb = new_notebook()
  27. nb.cells.append(new_markdown_cell(u'Created by test'))
  28. cc1 = new_code_cell(source=u'print(2*6)')
  29. cc1.outputs.append(new_output(output_type="stream", text=u'12'))
  30. nb.cells.append(cc1)
  31. with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w',
  32. encoding='utf-8') as f:
  33. write(nb, f, version=4)
  34. def test_missing_bundler_arg(self):
  35. """Should respond with 400 error about missing bundler arg"""
  36. resp = self.request('GET', 'bundle/fake.ipynb')
  37. self.assertEqual(resp.status_code, 400)
  38. self.assertIn('Missing argument bundler', resp.text)
  39. def test_notebook_not_found(self):
  40. """Shoudl respond with 404 error about missing notebook"""
  41. resp = self.request('GET', 'bundle/fake.ipynb',
  42. params={'bundler': 'fake_bundler'})
  43. self.assertEqual(resp.status_code, 404)
  44. self.assertIn('Not Found', resp.text)
  45. def test_bundler_not_enabled(self):
  46. """Should respond with 400 error about disabled bundler"""
  47. resp = self.request('GET', 'bundle/testnb.ipynb',
  48. params={'bundler': 'fake_bundler'})
  49. self.assertEqual(resp.status_code, 400)
  50. self.assertIn('Bundler fake_bundler not enabled', resp.text)
  51. def test_bundler_import_error(self):
  52. """Should respond with 500 error about failure to load bundler module"""
  53. with patch('notebook.bundler.handlers.BundlerHandler.get_bundler') as mock:
  54. mock.return_value = {'module_name': 'fake_module'}
  55. resp = self.request('GET', 'bundle/testnb.ipynb',
  56. params={'bundler': 'fake_bundler'})
  57. mock.assert_called_with('fake_bundler')
  58. self.assertEqual(resp.status_code, 500)
  59. self.assertIn('Could not import bundler fake_bundler', resp.text)
  60. def test_bundler_invoke(self):
  61. """Should respond with 200 and output from test bundler stub"""
  62. with patch('notebook.bundler.handlers.BundlerHandler.get_bundler') as mock:
  63. mock.return_value = {'module_name': 'notebook.bundler.tests.test_bundler_api'}
  64. resp = self.request('GET', 'bundle/testnb.ipynb',
  65. params={'bundler': 'stub_bundler'})
  66. mock.assert_called_with('stub_bundler')
  67. self.assertEqual(resp.status_code, 200)
  68. self.assertIn('testnb.ipynb', resp.text)