|
1 | 1 | import os
|
2 | 2 | import pytest
|
| 3 | +from pathlib import Path |
3 | 4 | import tornado
|
4 | 5 |
|
5 | 6 | from .utils import expected_http_error
|
|
10 | 11 | new_output)
|
11 | 12 |
|
12 | 13 |
|
13 |
| -async def test_hidden_files(jp_fetch, jp_serverapp, jp_root_dir): |
14 |
| - not_hidden = [ |
15 |
| - u'å b', |
16 |
| - u'å b/ç. d', |
17 |
| - ] |
18 |
| - hidden = [ |
19 |
| - u'.å b', |
20 |
| - u'å b/.ç d', |
21 |
| - ] |
22 |
| - dirs = not_hidden + hidden |
23 |
| - |
24 |
| - for d in dirs: |
25 |
| - path = jp_root_dir / d.replace('/', os.sep) |
26 |
| - path.mkdir(parents=True, exist_ok=True) |
27 |
| - path.joinpath('foo').write_text('foo') |
28 |
| - path.joinpath('.foo').write_text('.foo') |
29 |
| - |
30 |
| - for d in not_hidden: |
31 |
| - r = await jp_fetch( |
32 |
| - 'files', d, 'foo', |
33 |
| - method='GET' |
34 |
| - ) |
35 |
| - assert r.body.decode() == 'foo' |
36 |
| - |
37 |
| - with pytest.raises(tornado.httpclient.HTTPClientError) as e: |
38 |
| - await jp_fetch( |
39 |
| - 'files', d, '.foo', |
40 |
| - method='GET' |
41 |
| - ) |
42 |
| - assert expected_http_error(e, 404) |
43 |
| - |
44 |
| - for d in hidden: |
45 |
| - for foo in ('foo', '.foo'): |
46 |
| - with pytest.raises(tornado.httpclient.HTTPClientError) as e: |
47 |
| - await jp_fetch( |
48 |
| - 'files', d, foo, |
49 |
| - method='GET' |
50 |
| - ) |
51 |
| - assert expected_http_error(e, 404) |
| 14 | +@pytest.fixture(params=[ |
| 15 | + [False, ['å b']], |
| 16 | + [False, ['å b', 'ç. d']], |
| 17 | + [True, ['.å b']], |
| 18 | + [True, ['å b', '.ç d']] |
| 19 | +]) |
| 20 | +def maybe_hidden(request): |
| 21 | + return request.param |
| 22 | + |
| 23 | + |
| 24 | +async def fetch_expect_200(jp_fetch, *path_parts): |
| 25 | + r = await jp_fetch('files', *path_parts, method='GET') |
| 26 | + assert (r.body.decode() == path_parts[-1]), path_parts |
| 27 | + |
| 28 | + |
| 29 | +async def fetch_expect_404(jp_fetch, *path_parts): |
| 30 | + with pytest.raises(tornado.httpclient.HTTPClientError) as e: |
| 31 | + await jp_fetch('files', *path_parts, method='GET') |
| 32 | + assert expected_http_error(e, 404), path_parts |
| 33 | + |
| 34 | + |
| 35 | +async def test_hidden_files(jp_fetch, jp_serverapp, jp_root_dir, maybe_hidden): |
| 36 | + is_hidden, path_parts = maybe_hidden |
| 37 | + path = Path(jp_root_dir, *path_parts) |
| 38 | + path.mkdir(parents=True, exist_ok=True) |
| 39 | + |
| 40 | + foos = ['foo', '.foo'] |
| 41 | + for foo in foos: |
| 42 | + (path / foo).write_text(foo) |
| 43 | + |
| 44 | + if is_hidden: |
| 45 | + for foo in foos: |
| 46 | + await fetch_expect_404(jp_fetch, *path_parts, foo) |
| 47 | + else: |
| 48 | + await fetch_expect_404(jp_fetch, *path_parts, '.foo') |
| 49 | + await fetch_expect_200(jp_fetch, *path_parts, 'foo') |
52 | 50 |
|
53 | 51 | jp_serverapp.contents_manager.allow_hidden = True
|
54 | 52 |
|
55 |
| - for d in not_hidden: |
56 |
| - r = await jp_fetch( |
57 |
| - 'files', d, 'foo', |
58 |
| - method='GET' |
59 |
| - ) |
60 |
| - assert r.body.decode() == 'foo' |
61 |
| - |
62 |
| - r = await jp_fetch( |
63 |
| - 'files', d, '.foo', |
64 |
| - method='GET' |
65 |
| - ) |
66 |
| - assert r.body.decode() == '.foo' |
67 |
| - |
68 |
| - for d in hidden: |
69 |
| - for foo in ('foo', '.foo'): |
70 |
| - r = await jp_fetch( |
71 |
| - 'files', d, foo, |
72 |
| - method='GET' |
73 |
| - ) |
74 |
| - assert r.body.decode() == foo |
| 53 | + for foo in foos: |
| 54 | + await fetch_expect_200(jp_fetch, *path_parts, foo) |
75 | 55 |
|
76 | 56 |
|
77 | 57 | async def test_contents_manager(jp_fetch, jp_serverapp, jp_root_dir):
|
|
0 commit comments