def test_set_log_level_with_bad_level_uses_info(set_console_ip, debug_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() qpylib.set_log_level('BAD') qpylib.log('hello debug', 'DEBUG') qpylib.log('hello default info') qpylib.log('hello info', 'INFO') qpylib.log('hello warning', 'WARNING') qpylib.log('hello error', 'ERROR') qpylib.log('hello critical', 'CRITICAL') verify_log_file_content(log_path, [{ 'level': 'INFO', 'text': 'hello default info' }, { 'level': 'INFO', 'text': 'hello info' }, { 'level': 'WARNING', 'text': 'hello warning' }, { 'level': 'ERROR', 'text': 'hello error' }, { 'level': 'CRITICAL', 'text': 'hello critical' }], not_expected_lines=[{ 'level': 'DEBUG', 'text': 'hello debug' }])
def test_all_log_levels_with_set_debug_threshold(info_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() qpylib.set_log_level('DEBUG') qpylib.log('hello debug', 'DEBUG') qpylib.log('hello default info') qpylib.log('hello info', 'INFO') qpylib.log('hello warning', 'WARNING') qpylib.log('hello error', 'ERROR') qpylib.log('hello critical', 'CRITICAL') qpylib.log('hello exception', 'EXCEPTION') verify_log_file_content(log_path, [{ 'level': 'DEBUG', 'text': 'hello debug' }, { 'level': 'INFO', 'text': 'hello default info' }, { 'level': 'INFO', 'text': 'hello info' }, { 'level': 'WARNING', 'text': 'hello warning' }, { 'level': 'ERROR', 'text': 'hello error' }, { 'level': 'CRITICAL', 'text': 'hello critical' }, { 'level': 'ERROR', 'text': 'hello exception' }])
def test_set_log_level_sets_correct_level(info_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() qpylib.set_log_level('DEBUG') assert log_qpylib.QLOGGER.getEffectiveLevel() == logging.DEBUG
def test_set_log_level_with_bad_level_raises_error(info_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() with pytest.raises(ValueError, match="Unknown level: 'BAD'"): qpylib.set_log_level('BAD')
def test_set_log_level_without_create_raises_error(): with pytest.raises( RuntimeError, match= 'You cannot use set_log_level before logging has been initialised' ): qpylib.set_log_level('DEBUG')
def log_level(): level = request.form['level'].upper() levels = ['INFO', 'DEBUG', 'ERROR', 'WARNING', 'CRITICAL'] if any(level in s for s in levels): qpylib.set_log_level(request.form['level']) else: return 'level parameter missing or unsupported - ' + str(levels), 42 return 'log level set to ' + level
def log_level(): level = request.form['level'].upper() levels = ['INFO', 'DEBUG', 'ERROR', 'WARNING', 'CRITICAL'] if any( level in s for s in levels): qpylib.set_log_level(request.form['level']) else: return 'level parameter missing or unsupported - ' + str (levels), 42 return 'log level set to ' + level
def result(): import poll_n_write # Get the form form = request.form # Extract the form data ACCESS_KEY = form.get('access_key') DOMAIN = form.get('domain', '') URL = form.get('url') LEVEL_OVERALL_CHANGE = form.get('level_overall_change', '1') LEVEL_FACTOR_CHANGE = form.get('level_factor_change', '1') LEVEL_NEW_ISSUE_CHANGE = form.get('level_new_issue_change', '1') # If masked access key is present, unmask it using data store if ACCESS_KEY.endswith('**********'): current_config = read_data_store() ACCESS_KEY = current_config['access_key'] # Split portfolio ids if present if form.get('portfolio_ids', '').strip().strip(',').lower() == 'all': PORTFOLIO_IDS = 'all' elif not form.get('portfolio_ids'): PORTFOLIO_IDS = None else: PORTFOLIO_IDS = form.get('portfolio_ids', '').strip().strip(',') PORTFOLIO_IDS = PORTFOLIO_IDS.split(',') if PORTFOLIO_IDS else None FETCH_HISTORICAL_DATA = bool(form.get('fetch_historical_data', False)) MONITOR_CONFIG = { 'fetch_company_overall': form.get('fetch_company_overall', 'no').lower() == 'yes', 'fetch_company_factors': form.get('fetch_company_factors', 'no').lower() == 'yes', 'fetch_company_issues': form.get('fetch_company_issues', 'no').lower() == 'yes', 'fetch_portfolio_overall': form.get('fetch_portfolio_overall', 'no').lower() == 'yes', 'fetch_portfolio_factors': form.get('fetch_portfolio_factors', 'no').lower() == 'yes', 'fetch_portfolio_issues': form.get('fetch_portfolio_issues', 'no').lower() == 'yes', 'issue_level_findings': form.get('issue_level_findings', 'no').lower() == 'yes', 'proxy': form.get('proxy'), 'level': form.get('level', 'info').upper(), 'logLevel': form.get('level'), 'proxy_type': form.get('proxy_type', 'no'), 'host': form.get('host', 'no'), 'port': form.get('port', 'no'), 'username': form.get('username', 'no'), 'password': form.get('password', 'no'), } if MONITOR_CONFIG['proxy'] != 'on': MONITOR_CONFIG['proxy'] = False DIFF_OVERRIDE_CONFIG = { 'diff_override_own_overall': form.get('diff_override_own_overall', 'no').lower() == 'yes', 'diff_override_portfolio_overall': form.get('diff_override_portfolio_overall', 'no').lower() == 'yes', 'diff_override_own_factor': form.get('diff_override_own_factor', 'no').lower() == 'yes', 'diff_override_portfolio_factor': form.get('diff_override_portfolio_factor', 'no').lower() == 'yes', } if MONITOR_CONFIG['proxy']: proxy_dict = { 'proxy_type': MONITOR_CONFIG['proxy_type'], 'host': MONITOR_CONFIG['host'], 'port': MONITOR_CONFIG['port'], 'username': MONITOR_CONFIG['username'], 'password': MONITOR_CONFIG['password'], } else: proxy_dict = {} MONITOR_CONFIG['proxy_type'] = '' MONITOR_CONFIG['host'] = '' MONITOR_CONFIG['port'] = '' MONITOR_CONFIG['username'] = '' MONITOR_CONFIG['password'] = '' try: log_level = MONITOR_CONFIG.get('level', 'INFO') qpylib.set_log_level(log_level) except Exception as err: qpylib.log("Error " + str(err)) raise # Connect to the SecurityScorecard; Start polling and write to the LEEF logger res, msg = poll_n_write.poll_scorecard_n_write( ACCESS_KEY, DOMAIN, URL, LEVEL_OVERALL_CHANGE, LEVEL_FACTOR_CHANGE, LEVEL_NEW_ISSUE_CHANGE, PORTFOLIO_IDS, FETCH_HISTORICAL_DATA, MONITOR_CONFIG, DIFF_OVERRIDE_CONFIG, proxy_dict) # If polling starts successfully, render dloading.html; Otherwise render error_with_back.html if res == True: return render_template('dloading.html', qname="success") else: qpylib.log(msg, level='error') return render_template('error_with_back.html', err=msg)