 Issue 29912588:
  Issue 7019 - [CMS] Refactor `test_server.py`  (Closed)
    
  
    Issue 29912588:
  Issue 7019 - [CMS] Refactor `test_server.py`  (Closed) 
  | Left: | ||
| Right: | 
| 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, exception_test | 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') | |
| 
Vasily Kuznetsov
2018/10/22 14:36:48
Perhaps we can add a comment here explaining what
 
Tudor Avram
2018/10/23 16:44:35
Done.
 | |
| 53 werkzeug_dir.join('__init__.py').write('raise ImportError') | |
| 54 werkzeug_dir.join('serving.py').write('raise ImportError') | |
| 
Vasily Kuznetsov
2018/10/22 14:36:47
It seems that this line is actually not necessary.
 
Tudor Avram
2018/10/23 16:44:35
Done.
 | |
| 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 | |
| 
Vasily Kuznetsov
2018/10/22 14:36:48
It seems that now these four slowtests with relate
 
Tudor Avram
2018/10/23 16:44:34
Done.
 | |
| 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 exception_test(urllib2.urlopen, urllib2.HTTPError, exp_msg, | |
| 96 dynamic_server_werkzeug + filename) | |
| 97 | |
| 98 | |
| 99 @pytest.mark.slowtest | |
| 100 def test_dynamic_builtins_good_page(dynamic_server_builtins): | |
| 101 filename, expected_output = dynamic_expected_outputs[0] | |
| 102 response = urllib2.urlopen(dynamic_server_builtins + filename) | |
| 103 | |
| 104 assert expected_output in response.read().strip() | |
| 105 | |
| 106 | |
| 107 @pytest.mark.slowtest | |
| 108 def test_dynamic_builtins_not_found(dynamic_server_builtins): | |
| 109 filename = 'en/no-page-here' | |
| 110 exp_msg = 'Not Found' | |
| 111 | |
| 112 exception_test(urllib2.urlopen, urllib2.HTTPError, exp_msg, | |
| 113 dynamic_server_builtins + filename) | |
| 114 | 59 | 
| 115 | 60 | 
| 116 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 
| 117 def test_dynamic_server_handler(filename, expected_output, temp_site): | 62 def test_dynamic_server_handler(filename, expected_output, temp_site): | 
| 118 def cleanup(page): | 63 def cleanup(page): | 
| 119 return page.replace(os.linesep, '').strip() | 64 return page.replace(os.linesep, '').strip() | 
| 120 | 65 | 
| 121 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 66 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 
| 122 environ = {'PATH_INFO': filename} | 67 environ = {'PATH_INFO': filename} | 
| 123 | 68 | 
| 124 generated_page = handler(environ, lambda x, y: None) | 69 generated_page = handler(environ, lambda x, y: None) | 
| 125 | 70 | 
| 126 assert cleanup(expected_output) == cleanup(generated_page[0]) | 71 assert cleanup(expected_output) == cleanup(generated_page[0]) | 
| 127 | 72 | 
| 128 | 73 | 
| 129 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 74 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 
| 130 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): | 
| 131 handler = DynamicServerHandler('localhost', 5000, | 76 handler = DynamicServerHandler('localhost', 5000, | 
| 132 str(temp_site_with_conflicts)) | 77 str(temp_site_with_conflicts)) | 
| 133 environ = {'PATH_INFO': page} | 78 environ = {'PATH_INFO': page} | 
| 134 exp_msg = 'The requested page conflicts with another page.' | 79 exp_msg = 'The requested page conflicts with another page.' | 
| 135 | 80 | 
| 136 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) | 81 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) | 
| LEFT | RIGHT |