##// END OF EJS Templates
Merge pull request #7181 from jhamrick/extension-traitlet...
Merge pull request #7181 from jhamrick/extension-traitlet Add extension traitlet to force dotted names

File last commit:

r18613:380a2e94
r19474:7b625264 merge
Show More
test_files.py
152 lines | 5.2 KiB | text/x-python | PythonLexer
MinRK
test /files/ gives 403 on hidden files
r13175 # coding: utf-8
"""Test the /files/ handler."""
import io
import os
from unicodedata import normalize
pjoin = os.path.join
import requests
Manuel Riel
add tests for file download via ContentsManager
r18152 import json
MinRK
don't use nbformat.current in IPython.html...
r18607 from IPython.nbformat import write
from IPython.nbformat.v4 import (new_notebook,
MinRK
update html/js to nbformat 4
r18584 new_markdown_cell, new_code_cell,
Manuel Riel
add tests for file download via ContentsManager
r18152 new_output)
MinRK
test /files/ gives 403 on hidden files
r13175
from IPython.html.utils import url_path_join
from .launchnotebook import NotebookTestBase
from IPython.utils import py3compat
Manuel Riel
add tests for file download via ContentsManager
r18152
MinRK
test /files/ gives 403 on hidden files
r13175 class FilesTest(NotebookTestBase):
def test_hidden_files(self):
not_hidden = [
u'Ã¥ b',
MinRK
test 'files/' redirects...
r13564 u'å b/ç. d',
MinRK
test /files/ gives 403 on hidden files
r13175 ]
hidden = [
u'.Ã¥ b',
MinRK
test 'files/' redirects...
r13564 u'å b/.ç d',
MinRK
test /files/ gives 403 on hidden files
r13175 ]
dirs = not_hidden + hidden
nbdir = self.notebook_dir.name
for d in dirs:
MinRK
s/os.path.sep/os.sep/
r13182 path = pjoin(nbdir, d.replace('/', os.sep))
MinRK
test /files/ gives 403 on hidden files
r13175 if not os.path.exists(path):
os.mkdir(path)
MinRK
Windows testing fixes
r13178 with open(pjoin(path, 'foo'), 'w') as f:
f.write('foo')
with open(pjoin(path, '.foo'), 'w') as f:
f.write('.foo')
MinRK
test /files/ gives 403 on hidden files
r13175 url = self.base_url()
for d in not_hidden:
MinRK
s/os.path.sep/os.sep/
r13182 path = pjoin(nbdir, d.replace('/', os.sep))
MinRK
test /files/ gives 403 on hidden files
r13175 r = requests.get(url_path_join(url, 'files', d, 'foo'))
r.raise_for_status()
MinRK
use request.text for Python 3 (content is always bytes)
r13565 self.assertEqual(r.text, 'foo')
MinRK
test /files/ gives 403 on hidden files
r13175 r = requests.get(url_path_join(url, 'files', d, '.foo'))
Brian E. Granger
Fixing test_files tests.
r15103 self.assertEqual(r.status_code, 404)
MinRK
test /files/ gives 403 on hidden files
r13175
for d in hidden:
MinRK
s/os.path.sep/os.sep/
r13182 path = pjoin(nbdir, d.replace('/', os.sep))
MinRK
test /files/ gives 403 on hidden files
r13175 for foo in ('foo', '.foo'):
r = requests.get(url_path_join(url, 'files', d, foo))
Brian E. Granger
Fixing test_files tests.
r15103 self.assertEqual(r.status_code, 404)
MinRK
test 'files/' redirects...
r13564
Manuel Riel
add tests for file download via ContentsManager
r18152 def test_contents_manager(self):
"make sure ContentsManager returns right files (ipynb, bin, txt)."
nbdir = self.notebook_dir.name
base = self.base_url()
MinRK
update html/js to nbformat 4
r18584 nb = new_notebook(
cells=[
new_markdown_cell(u'Created by test ³'),
new_code_cell("print(2*6)", outputs=[
new_output("stream", text="12"),
])
]
)
Manuel Riel
add tests for file download via ContentsManager
r18152
with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w',
encoding='utf-8') as f:
Min RK
fix backward `f, nb` args for nbformat.write
r18613 write(nb, f, version=4)
Manuel Riel
add tests for file download via ContentsManager
r18152
with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
Manuel Riel
correctly handle base64 and json, improve bin-file test
r18161 f.write(b'\xff' + os.urandom(5))
Manuel Riel
add tests for file download via ContentsManager
r18152 f.close()
with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
f.write(u'foobar')
f.close()
r = requests.get(url_path_join(base, 'files', 'testnb.ipynb'))
self.assertEqual(r.status_code, 200)
self.assertIn('print(2*6)', r.text)
json.loads(r.text)
r = requests.get(url_path_join(base, 'files', 'test.bin'))
self.assertEqual(r.status_code, 200)
self.assertEqual(r.headers['content-type'], 'application/octet-stream')
Manuel Riel
simplify bin-file test
r18187 self.assertEqual(r.content[:1], b'\xff')
Manuel Riel
correctly handle base64 and json, improve bin-file test
r18161 self.assertEqual(len(r.content), 6)
Manuel Riel
add tests for file download via ContentsManager
r18152
r = requests.get(url_path_join(base, 'files', 'test.txt'))
self.assertEqual(r.status_code, 200)
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(r.text, 'foobar')
Min RK
use ?download=1 to trigger download in /files/...
r18556
def test_download(self):
nbdir = self.notebook_dir.name
base = self.base_url()
text = 'hello'
with open(pjoin(nbdir, 'test.txt'), 'w') as f:
f.write(text)
r = requests.get(url_path_join(base, 'files', 'test.txt'))
disposition = r.headers.get('Content-Disposition', '')
self.assertNotIn('attachment', disposition)
Manuel Riel
add tests for file download via ContentsManager
r18152
Min RK
use ?download=1 to trigger download in /files/...
r18556 r = requests.get(url_path_join(base, 'files', 'test.txt') + '?download=1')
disposition = r.headers.get('Content-Disposition', '')
self.assertIn('attachment', disposition)
self.assertIn('filename="test.txt"', disposition)
Manuel Riel
add tests for file download via ContentsManager
r18152
MinRK
test 'files/' redirects...
r13564 def test_old_files_redirect(self):
"""pre-2.0 'files/' prefixed links are properly redirected"""
nbdir = self.notebook_dir.name
base = self.base_url()
os.mkdir(pjoin(nbdir, 'files'))
os.makedirs(pjoin(nbdir, 'sub', 'files'))
for prefix in ('', 'sub'):
with open(pjoin(nbdir, prefix, 'files', 'f1.txt'), 'w') as f:
f.write(prefix + '/files/f1')
with open(pjoin(nbdir, prefix, 'files', 'f2.txt'), 'w') as f:
f.write(prefix + '/files/f2')
with open(pjoin(nbdir, prefix, 'f2.txt'), 'w') as f:
f.write(prefix + '/f2')
with open(pjoin(nbdir, prefix, 'f3.txt'), 'w') as f:
f.write(prefix + '/f3')
url = url_path_join(base, 'notebooks', prefix, 'files', 'f1.txt')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
MinRK
use request.text for Python 3 (content is always bytes)
r13565 self.assertEqual(r.text, prefix + '/files/f1')
MinRK
test 'files/' redirects...
r13564
url = url_path_join(base, 'notebooks', prefix, 'files', 'f2.txt')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
MinRK
use request.text for Python 3 (content is always bytes)
r13565 self.assertEqual(r.text, prefix + '/files/f2')
MinRK
test 'files/' redirects...
r13564
url = url_path_join(base, 'notebooks', prefix, 'files', 'f3.txt')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
MinRK
use request.text for Python 3 (content is always bytes)
r13565 self.assertEqual(r.text, prefix + '/f3')
MinRK
test 'files/' redirects...
r13564