def test_parse_settings_type_errors(self): """Test settings values that should raise a TypeError.""" # CHROME_ARGS setting must be an iterable of strings, not a string with override_settings(CHROMEPDF={'CHROME_ARGS': '--no-sandbox'}): with self.assertRaises(TypeError): _output = parse_settings() # chrome_args parameter must be an iterable of strings, not a string with override_settings(CHROMEPDF={'CHROME_ARGS': []}): with self.assertRaises(TypeError): _output = parse_settings(chrome_args='--no-sandbox')
def test_parse_settings_tuples(self): """CHROME_ARGS/chrome_args may be a tuple.""" # Test CHROME_ARGS setting as a tuple with override_settings(CHROMEPDF={'CHROME_ARGS': ('--no-sandbox',)}): output = parse_settings() self.assertEqual(output['chrome_args'], ('--no-sandbox',)) # Test chrome_args parameter as a tuple with override_settings(CHROMEPDF={'CHROME_ARGS': []}): output = parse_settings(chrome_args=('--no-sandbox',)) self.assertEqual(output['chrome_args'], ('--no-sandbox',))
def test_parse_settings_overridden_settings(self): """Test result of parse_settings() function, with overridden settings passed.""" output = parse_settings() self.assertEqual(4, len(output)) self.assertEqual(output['chrome_path'], CHROME_PATH_SETTING_VAL) self.assertEqual(output['chromedriver_path'], CHROMEDRIVER_PATH_SETTING_VAL) self.assertEqual(output['chromedriver_downloads'], False) self.assertEqual(output['chrome_args'], ['--no-sandbox'])
def test_parse_settings_empty_paths(self): """Test result of parse_settings() function, where falsey values of the wrong type are converted to correct ones.""" output = parse_settings(chrome_path='', chromedriver_path='', chromedriver_downloads=None, chrome_args=None) # parsed values should be converted to None instead of '' self.assertEqual(4, len(output)) self.assertEqual(output['chrome_path'], None) self.assertEqual(output['chromedriver_path'], None) self.assertEqual(output['chromedriver_downloads'], False) self.assertEqual(output['chrome_args'], [])
def test_parse_settings_kwargs_and_overridden_settings_falsey(self): """ Overridden values should take priority even if they are falsey. """ output = parse_settings(chrome_path=None, chromedriver_path=None, chromedriver_downloads=False, chrome_args=[]) self.assertEqual(4, len(output)) self.assertEqual(output.get('chrome_path'), None) self.assertEqual(output.get('chromedriver_path'), None) self.assertEqual(output.get('chromedriver_downloads'), False) self.assertEqual(output.get('chrome_args'), [])
def test_parse_settings_kwargs_and_overridden_settings(self): """ Test result of parse_settings() function, with kwargs and overridden settings passed. Override values should take priority over django settings values. """ output = parse_settings(chromedriver_downloads=False, chrome_path=CHROME_PATH_KWARG_VAL, chromedriver_path=CHROMEDRIVER_PATH_KWARG_VAL, chrome_args=['yes-sandbox']) self.assertEqual(4, len(output)) self.assertEqual(output.get('chrome_path'), CHROME_PATH_KWARG_VAL) self.assertEqual(output.get('chromedriver_path'), CHROMEDRIVER_PATH_KWARG_VAL) self.assertEqual(output.get('chromedriver_downloads'), False) self.assertEqual(output.get('chrome_args'), ['yes-sandbox'])
def test_parse_settings_defaults_no_chromepdf(self): """Test parse_settings() where defaults get used, and settings.CHROMEPDF does not exist.""" # Yes, you can simulate the absense of a setting via @override_settings() and then del. See Django docs. # "You can also simulate the absence of a setting by deleting it after settings have been overridden." del settings.CHROMEPDF output = parse_settings() # compare to values of 'DEFAULT_SETTINGS'. # hardcode the values so tests will fail if defaults get changed by accident. self.assertEqual(4, len(output)) self.assertEqual(output['chrome_path'], None) self.assertEqual(output['chromedriver_path'], None) self.assertEqual(output['chromedriver_downloads'], True) self.assertEqual(output['chrome_args'], [])
def test_parse_settings_kwargs(self): """Test result of parse_settings() function, with only kwargs passed and settings.CHROMEPDF does not exist.""" # Yes, you can simulate the absense of a setting via @override_settings() and then del. See Django docs. # "You can also simulate the absence of a setting by deleting it after settings have been overridden." del settings.CHROMEPDF output = parse_settings(chrome_path=CHROME_PATH_KWARG_VAL, chromedriver_path=CHROMEDRIVER_PATH_KWARG_VAL, chromedriver_downloads=False, chrome_args=['--no-sandbox']) self.assertEqual(4, len(output)) self.assertEqual(output.get('chrome_path'), CHROME_PATH_KWARG_VAL) self.assertEqual(output.get('chromedriver_path'), CHROMEDRIVER_PATH_KWARG_VAL) self.assertEqual(output.get('chromedriver_downloads'), False) self.assertEqual(output.get('chrome_args'), ['--no-sandbox'])
def __init__(self, **kwargs): # load settings, combining those from **kwargs as well as the Django settings. settings = parse_settings(**kwargs) self._chrome_path = settings['chrome_path'] self._chromedriver_path = settings['chromedriver_path'] self._chromedriver_downloads = settings['chromedriver_downloads'] # download chromedriver if we have chrome, and downloads are enabled if self._chrome_path is not None and self._chromedriver_path is None and self._chromedriver_downloads: chrome_version = get_chrome_version(self._chrome_path) self._chromedriver_path = download_chromedriver_version( chrome_version) self._webdriver_kwargs = { 'chrome_args': settings['chrome_args'], 'chrome_path': self._chrome_path, 'chromedriver_path': self._chromedriver_path, }