test_nbconvert_handlers.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # coding: utf-8
  2. import io
  3. import json
  4. import os
  5. from os.path import join as pjoin
  6. import shutil
  7. import requests
  8. from notebook.utils import url_path_join
  9. from notebook.tests.launchnotebook import NotebookTestBase, assert_http_error
  10. from nbformat import write
  11. from nbformat.v4 import (
  12. new_notebook, new_markdown_cell, new_code_cell, new_output,
  13. )
  14. from ipython_genutils.testing.decorators import onlyif_cmds_exist
  15. try: #PY3
  16. from base64 import encodebytes
  17. except ImportError: #PY2
  18. from base64 import encodestring as encodebytes
  19. class NbconvertAPI(object):
  20. """Wrapper for nbconvert API calls."""
  21. def __init__(self, request):
  22. self.request = request
  23. def _req(self, verb, path, body=None, params=None):
  24. response = self.request(verb,
  25. url_path_join('nbconvert', path),
  26. data=body, params=params,
  27. )
  28. response.raise_for_status()
  29. return response
  30. def from_file(self, format, path, name, download=False):
  31. return self._req('GET', url_path_join(format, path, name),
  32. params={'download':download})
  33. def from_post(self, format, nbmodel):
  34. body = json.dumps(nbmodel)
  35. return self._req('POST', format, body)
  36. def list_formats(self):
  37. return self._req('GET', '')
  38. png_green_pixel = encodebytes(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00'
  39. b'\x00\x00\x01\x00\x00x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDAT'
  40. b'\x08\xd7c\x90\xfb\xcf\x00\x00\x02\\\x01\x1e.~d\x87\x00\x00\x00\x00IEND\xaeB`\x82'
  41. ).decode('ascii')
  42. class APITest(NotebookTestBase):
  43. def setUp(self):
  44. nbdir = self.notebook_dir
  45. if not os.path.isdir(pjoin(nbdir, 'foo')):
  46. subdir = pjoin(nbdir, 'foo')
  47. os.mkdir(subdir)
  48. # Make sure that we clean this up when we're done.
  49. # By using addCleanup this will happen correctly even if we fail
  50. # later in setUp.
  51. @self.addCleanup
  52. def cleanup_dir():
  53. shutil.rmtree(subdir, ignore_errors=True)
  54. nb = new_notebook()
  55. nb.cells.append(new_markdown_cell(u'Created by test ³'))
  56. cc1 = new_code_cell(source=u'print(2*6)')
  57. cc1.outputs.append(new_output(output_type="stream", text=u'12'))
  58. cc1.outputs.append(new_output(output_type="execute_result",
  59. data={'image/png' : png_green_pixel},
  60. execution_count=1,
  61. ))
  62. nb.cells.append(cc1)
  63. with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',
  64. encoding='utf-8') as f:
  65. write(nb, f, version=4)
  66. self.nbconvert_api = NbconvertAPI(self.request)
  67. @onlyif_cmds_exist('pandoc')
  68. def test_from_file(self):
  69. r = self.nbconvert_api.from_file('html', 'foo', 'testnb.ipynb')
  70. self.assertEqual(r.status_code, 200)
  71. self.assertIn(u'text/html', r.headers['Content-Type'])
  72. self.assertIn(u'Created by test', r.text)
  73. self.assertIn(u'print', r.text)
  74. r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb')
  75. self.assertIn(u'text/x-python', r.headers['Content-Type'])
  76. self.assertIn(u'print(2*6)', r.text)
  77. @onlyif_cmds_exist('pandoc')
  78. def test_from_file_404(self):
  79. with assert_http_error(404):
  80. self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')
  81. @onlyif_cmds_exist('pandoc')
  82. def test_from_file_download(self):
  83. r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb', download=True)
  84. content_disposition = r.headers['Content-Disposition']
  85. self.assertIn('attachment', content_disposition)
  86. self.assertIn('testnb.py', content_disposition)
  87. @onlyif_cmds_exist('pandoc')
  88. def test_from_file_zip(self):
  89. r = self.nbconvert_api.from_file('latex', 'foo', 'testnb.ipynb', download=True)
  90. self.assertIn(u'application/zip', r.headers['Content-Type'])
  91. self.assertIn(u'.zip', r.headers['Content-Disposition'])
  92. @onlyif_cmds_exist('pandoc')
  93. def test_from_post(self):
  94. nbmodel = self.request('GET', 'api/contents/foo/testnb.ipynb').json()
  95. r = self.nbconvert_api.from_post(format='html', nbmodel=nbmodel)
  96. self.assertEqual(r.status_code, 200)
  97. self.assertIn(u'text/html', r.headers['Content-Type'])
  98. self.assertIn(u'Created by test', r.text)
  99. self.assertIn(u'print', r.text)
  100. r = self.nbconvert_api.from_post(format='python', nbmodel=nbmodel)
  101. self.assertIn(u'text/x-python', r.headers['Content-Type'])
  102. self.assertIn(u'print(2*6)', r.text)
  103. @onlyif_cmds_exist('pandoc')
  104. def test_from_post_zip(self):
  105. nbmodel = self.request('GET', 'api/contents/foo/testnb.ipynb').json()
  106. r = self.nbconvert_api.from_post(format='latex', nbmodel=nbmodel)
  107. self.assertIn(u'application/zip', r.headers['Content-Type'])
  108. self.assertIn(u'.zip', r.headers['Content-Disposition'])