OLD | NEW |
1 import os | 1 import os |
2 import sys | 2 import sys |
3 import runpy | 3 import runpy |
4 | 4 |
5 import mock | 5 import mock |
6 import pytest | 6 import pytest |
7 import urllib2 | 7 import urllib2 |
8 | 8 |
9 from .conftest import ROOTPATH | 9 from .conftest import ROOTPATH |
10 from .utils import get_dir_contents, run_test_server | 10 from .utils import get_dir_contents, run_test_server |
11 from cms.sources import FileSource | 11 from cms.sources import FileSource |
| 12 from cms.bin.test_server import DynamicServerHandler |
12 | 13 |
13 | 14 |
14 def get_expected_outputs(test_type): | 15 def get_expected_outputs(test_type): |
15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') | 16 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') |
16 outputs = get_dir_contents(expected_out_path) | 17 outputs = get_dir_contents(expected_out_path) |
17 for filename in list(outputs): | 18 for filename in list(outputs): |
18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") | 19 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") |
19 # There are cases where we need to test outputs which differ depending | 20 # There are cases where we need to test outputs which differ depending |
20 # on how they are generated; either statically or dynamically | 21 # on how they are generated; either statically or dynamically |
21 if filename.endswith('@' + test_type): | 22 if filename.endswith('@' + test_type): |
(...skipping 11 matching lines...) Expand all Loading... |
33 | 34 |
34 @pytest.fixture(scope='session') | 35 @pytest.fixture(scope='session') |
35 def static_output(request, temp_site): | 36 def static_output(request, temp_site): |
36 static_out_path = os.path.join(temp_site, 'static_out') | 37 static_out_path = os.path.join(temp_site, 'static_out') |
37 sys.argv = ['filler', temp_site, static_out_path] | 38 sys.argv = ['filler', temp_site, static_out_path] |
38 with mock.patch('cms.sources.FileSource.version', 1): | 39 with mock.patch('cms.sources.FileSource.version', 1): |
39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | 40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') |
40 return static_out_path | 41 return static_out_path |
41 | 42 |
42 | 43 |
43 @pytest.fixture(scope='module') | 44 @pytest.fixture(scope='function') |
44 def dynamic_server(temp_site): | 45 def dynamic_server_werkzeug(temp_site): |
45 with run_test_server(temp_site) as ts: | 46 with run_test_server(str(temp_site)) as ts: |
46 yield ts | 47 yield ts |
47 | 48 |
48 | 49 |
| 50 @pytest.fixture(scope='function') |
| 51 def dynamic_server_builtins(temp_site, tmpdir): |
| 52 werkzeug_dir = tmpdir.mkdir('werkzeug') |
| 53 werkzeug_dir.join('__init__.py').write('raise ImportError') |
| 54 werkzeug_dir.join('serving.py').write('raise ImportError') |
| 55 |
| 56 new_env = dict(os.environ) |
| 57 new_env['PYTHONPATH'] = os.pathsep.join([str(tmpdir), |
| 58 os.getenv('PYTHONPATH', '')]) |
| 59 |
| 60 with run_test_server(str(temp_site), new_env) as ts: |
| 61 yield ts |
| 62 |
| 63 |
49 @pytest.fixture(scope='session') | 64 @pytest.fixture(scope='session') |
50 def output_pages(static_output): | 65 def output_pages(static_output): |
51 return get_dir_contents(static_output) | 66 return get_dir_contents(static_output) |
52 | 67 |
53 | 68 |
54 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 69 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
55 def test_static(output_pages, filename, expected_output): | 70 def test_static(output_pages, filename, expected_output): |
56 if expected_output.startswith('## MISSING'): | 71 if expected_output.startswith('## MISSING'): |
57 assert filename not in output_pages | 72 assert filename not in output_pages |
58 else: | 73 else: |
59 assert expected_output == output_pages[filename] | 74 assert expected_output == output_pages[filename] |
60 | 75 |
61 | 76 |
62 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | |
63 def test_dynamic(dynamic_server, filename, expected_output): | |
64 response = urllib2.urlopen(dynamic_server + filename) | |
65 assert expected_output == response.read().strip() | |
66 | |
67 | |
68 def test_cache(output_pages): | 77 def test_cache(output_pages): |
69 source = FileSource(os.path.join('test_site')) | 78 source = FileSource(os.path.join('test_site')) |
70 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 79 assert source.get_cache_dir() == os.path.join('test_site', 'cache') |
| 80 |
| 81 |
| 82 @pytest.mark.slowtest |
| 83 def test_dynamic_werkzeug_good_page(dynamic_server_werkzeug): |
| 84 filename, expected_output = dynamic_expected_outputs[0] |
| 85 response = urllib2.urlopen(dynamic_server_werkzeug + filename) |
| 86 |
| 87 assert expected_output in response.read().strip() |
| 88 |
| 89 |
| 90 @pytest.mark.slowtest |
| 91 def test_dynamic_werkzeug_not_found(dynamic_server_werkzeug): |
| 92 filename = 'en/no-page-here' |
| 93 exp_msg = 'Not Found' |
| 94 |
| 95 with pytest.raises(urllib2.HTTPError) as err: |
| 96 urllib2.urlopen(dynamic_server_werkzeug + filename) |
| 97 |
| 98 assert exp_msg in str(err.value) |
| 99 |
| 100 |
| 101 @pytest.mark.slowtest |
| 102 def test_dynamic_builtins_good_page(dynamic_server_builtins): |
| 103 filename, expected_output = dynamic_expected_outputs[0] |
| 104 response = urllib2.urlopen(dynamic_server_builtins + filename) |
| 105 |
| 106 assert expected_output in response.read().strip() |
| 107 |
| 108 |
| 109 @pytest.mark.slowtest |
| 110 def test_dynamic_builtins_not_found(dynamic_server_builtins): |
| 111 filename = 'en/no-page-here' |
| 112 exp_msg = 'Not Found' |
| 113 |
| 114 with pytest.raises(urllib2.HTTPError) as err: |
| 115 urllib2.urlopen(dynamic_server_builtins + filename) |
| 116 |
| 117 assert exp_msg in str(err.value) |
| 118 |
| 119 |
| 120 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
| 121 def test_dynamic_server_handler(filename, expected_output, temp_site): |
| 122 def cleanup(page): |
| 123 return page.replace(os.linesep, '').strip() |
| 124 |
| 125 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) |
| 126 environ = {'PATH_INFO': filename} |
| 127 |
| 128 generated_page = handler(environ, lambda x, y: None) |
| 129 |
| 130 assert cleanup(expected_output) == cleanup(generated_page[0]) |
| 131 |
| 132 |
| 133 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) |
| 134 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): |
| 135 handler = DynamicServerHandler('localhost', 5000, |
| 136 str(temp_site_with_conflicts)) |
| 137 environ = {'PATH_INFO': page} |
| 138 |
| 139 with pytest.raises(Exception) as err: |
| 140 handler(environ, lambda x, y: None) |
| 141 |
| 142 assert str(err.value) == 'The requested page conflicts with another page.' |
OLD | NEW |