| LEFT | RIGHT |
| 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 | |
| 8 | 7 |
| 9 from .conftest import ROOTPATH | 8 from .conftest import ROOTPATH |
| 10 from .utils import get_dir_contents, run_test_server | 9 from .utils import get_dir_contents, exception_test |
| 11 from cms.sources import FileSource | 10 from cms.sources import FileSource |
| 12 from cms.bin.test_server import DynamicServerHandler | 11 from cms.bin.test_server import DynamicServerHandler |
| 13 | 12 |
| 14 | 13 |
| 15 def get_expected_outputs(test_type): | 14 def get_expected_outputs(test_type): |
| 16 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') | 15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') |
| 17 outputs = get_dir_contents(expected_out_path) | 16 outputs = get_dir_contents(expected_out_path) |
| 18 for filename in list(outputs): | 17 for filename in list(outputs): |
| 19 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") | 18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") |
| 20 # There are cases where we need to test outputs which differ depending | 19 # There are cases where we need to test outputs which differ depending |
| (...skipping 13 matching lines...) Expand all Loading... |
| 34 | 33 |
| 35 @pytest.fixture(scope='session') | 34 @pytest.fixture(scope='session') |
| 36 def static_output(request, temp_site): | 35 def static_output(request, temp_site): |
| 37 static_out_path = os.path.join(temp_site, 'static_out') | 36 static_out_path = os.path.join(temp_site, 'static_out') |
| 38 sys.argv = ['filler', temp_site, static_out_path] | 37 sys.argv = ['filler', temp_site, static_out_path] |
| 39 with mock.patch('cms.sources.FileSource.version', 1): | 38 with mock.patch('cms.sources.FileSource.version', 1): |
| 40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | 39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') |
| 41 return static_out_path | 40 return static_out_path |
| 42 | 41 |
| 43 | 42 |
| 44 @pytest.fixture(scope='function') | |
| 45 def dynamic_server_werkzeug(temp_site): | |
| 46 with run_test_server(str(temp_site)) as ts: | |
| 47 yield ts | |
| 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 | |
| 64 @pytest.fixture(scope='session') | 43 @pytest.fixture(scope='session') |
| 65 def output_pages(static_output): | 44 def output_pages(static_output): |
| 66 return get_dir_contents(static_output) | 45 return get_dir_contents(static_output) |
| 67 | 46 |
| 68 | 47 |
| 69 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
| 70 def test_static(output_pages, filename, expected_output): | 49 def test_static(output_pages, filename, expected_output): |
| 71 if expected_output.startswith('## MISSING'): | 50 if expected_output.startswith('## MISSING'): |
| 72 assert filename not in output_pages | 51 assert filename not in output_pages |
| 73 else: | 52 else: |
| 74 assert expected_output == output_pages[filename] | 53 assert expected_output == output_pages[filename] |
| 75 | 54 |
| 76 | 55 |
| 77 def test_cache(output_pages): | 56 def test_cache(output_pages): |
| 78 source = FileSource(os.path.join('test_site')) | 57 source = FileSource(os.path.join('test_site')) |
| 79 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 58 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 | 59 |
| 119 | 60 |
| 120 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
| 121 def test_dynamic_server_handler(filename, expected_output, temp_site): | 62 def test_dynamic_server_handler(filename, expected_output, temp_site): |
| 122 def cleanup(page): | 63 def cleanup(page): |
| 123 return page.replace(os.linesep, '').strip() | 64 return page.replace(os.linesep, '').strip() |
| 124 | 65 |
| 125 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 66 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) |
| 126 environ = {'PATH_INFO': filename} | 67 environ = {'PATH_INFO': filename} |
| 127 | 68 |
| 128 generated_page = handler(environ, lambda x, y: None) | 69 generated_page = handler(environ, lambda x, y: None) |
| 129 | 70 |
| 130 assert cleanup(expected_output) == cleanup(generated_page[0]) | 71 assert cleanup(expected_output) == cleanup(generated_page[0]) |
| 131 | 72 |
| 132 | 73 |
| 133 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 74 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) |
| 134 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): | 75 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): |
| 135 handler = DynamicServerHandler('localhost', 5000, | 76 handler = DynamicServerHandler('localhost', 5000, |
| 136 str(temp_site_with_conflicts)) | 77 str(temp_site_with_conflicts)) |
| 137 environ = {'PATH_INFO': page} | 78 environ = {'PATH_INFO': page} |
| 79 exp_msg = 'The requested page conflicts with another page.' |
| 138 | 80 |
| 139 with pytest.raises(Exception) as err: | 81 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) |
| 140 handler(environ, lambda x, y: None) | |
| 141 | |
| 142 assert str(err.value) == 'The requested page conflicts with another page.' | |
| LEFT | RIGHT |