diff --git a/acme_srv/helper.py b/acme_srv/helper.py index 32adaded..749940ee 100644 --- a/acme_srv/helper.py +++ b/acme_srv/helper.py @@ -1862,17 +1862,17 @@ def eab_profile_string_check(logger, cahandler, key, value): logger.debug('Helper.eab_profile_string_check() ended') -def request_operation(logger: logging.Logger, headers: Dict[str, str], proxy: Dict[str, str] = {}, timeout: int = 20, url: str = None, method: str = 'GET', payload: Dict[str, str] = None): +def request_operation(logger: logging.Logger, headers: Dict[str, str] = {}, proxy: Dict[str, str] = {}, timeout: int = 20, url: str = None, session=requests, method: str = 'GET', payload: Dict[str, str] = None): """ check if a for a string value taken from profile if its a variable inside a class and apply value """ logger.debug('Helper.api_operation(): method: %s', method) try: if method.lower() == 'get': - api_response = requests.get(url=url, headers=headers, proxies=proxy, timeout=timeout) + api_response = session.get(url=url, headers=headers, proxies=proxy, timeout=timeout) elif method.lower() == 'post': - api_response = requests.post(url=url, headers=headers, proxies=proxy, timeout=timeout, json=payload) + api_response = session.post(url=url, headers=headers, proxies=proxy, timeout=timeout, json=payload) elif method.lower() == 'put': - api_response = requests.put(url=url, headers=headers, proxies=proxy, timeout=timeout, json=payload) + api_response = session.put(url=url, headers=headers, proxies=proxy, timeout=timeout, json=payload) else: logger.error('unknown request method: %s', method) api_response = None @@ -1882,17 +1882,17 @@ def request_operation(logger: logging.Logger, headers: Dict[str, str], proxy: Di try: content = api_response.json() except Exception as err_: - logger.error('CAhandler._api_get() returned error during json parsing: %s', err_) + logger.error('request_operation returned error during json parsing: %s', err_) content = str(err_) else: content = None except Exception as err_: - logger.error('CAhandler._api_get() returned error: %s', err_) + logger.error('request_operation returned error: %s', err_) code = 500 content = str(err_) - logger.debug('Helper.api_operation() ended with: %s', code) + logger.debug('Helper.request_operation() ended with: %s', code) return code, content diff --git a/examples/ca_handler/entrust_ca_handler.py b/examples/ca_handler/entrust_ca_handler.py index b6e69fcf..df5ab70d 100644 --- a/examples/ca_handler/entrust_ca_handler.py +++ b/examples/ca_handler/entrust_ca_handler.py @@ -97,7 +97,7 @@ def _api_get(self, url: str) -> Tuple[int, Dict[str, str]]: 'Content-Type': CONTENT_TYPE } - code, content = request_operation(self.logger, method='get', url=url, headers=headers, proxy=self.proxy, timeout=self.request_timeout, payload=None) + code, content = request_operation(self.logger, session=self.session, method='get', url=url, headers=headers, proxy=self.proxy, timeout=self.request_timeout, payload=None) self.logger.debug('CAhandler._api_get() ended with code: %s', code) return code, content @@ -107,7 +107,7 @@ def _api_post(self, url: str, data: Dict[str, str]) -> Tuple[int, Dict[str, str] headers = { 'Content-Type': CONTENT_TYPE } - code, content = request_operation(self.logger, method='post', url=url, headers=headers, proxy=self.proxy, timeout=self.request_timeout, payload=data) + code, content = request_operation(self.logger, session=self.session, method='post', url=url, headers=headers, proxy=self.proxy, timeout=self.request_timeout, payload=data) self.logger.debug('CAhandler._api_post() ended with code: %s', code) return code, content @@ -117,7 +117,7 @@ def _api_put(self, url: str, data: Dict[str, str]) -> Tuple[int, Dict[str, str]] headers = { 'Content-Type': CONTENT_TYPE } - code, content = request_operation(self.logger, method='put', url=url, headers=headers, proxy=self.proxy, timeout=self.request_timeout, payload=data) + code, content = request_operation(self.logger, session=self.session, method='put', url=url, headers=headers, proxy=self.proxy, timeout=self.request_timeout, payload=data) self.logger.debug('CAhandler._api_put() ended with code: %s', code) return code, content diff --git a/test/test_digicert.py b/test/test_digicert.py index 96d068a9..a2bd402c 100644 --- a/test/test_digicert.py +++ b/test/test_digicert.py @@ -102,7 +102,7 @@ def test_010__api_post(self, mock_req): mock_req.return_value = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual(('status_code', "'str' object is not callable"), self.cahandler._api_post('url', 'data')) - self.assertIn("ERROR:test_a2c:CAhandler._api_post() returned error during json parsing: 'str' object is not callable", lcm.output) + self.assertIn("ERROR:test_a2c:request_operation returned error during json parsing: 'str' object is not callable", lcm.output) @patch('requests.post') def test_011__api_post(self, mock_req): @@ -121,7 +121,7 @@ def test_012__api_post(self, mock_req): mock_req.side_effect = Exception('exc_api_post') with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual((500, 'exc_api_post'), self.cahandler._api_post('url', 'data')) - self.assertIn('ERROR:test_a2c:CAhandler._api_post() returned error: exc_api_post', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error: exc_api_post', lcm.output) @patch.object(requests, 'get') def test_013__api_get(self, mock_req): @@ -141,7 +141,7 @@ def test_014__api_get(self, mock_req): mock_req.return_value = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual(('status_code', "'str' object is not callable"), self.cahandler._api_get('url')) - self.assertIn("ERROR:test_a2c:CAhandler._api_get() returned error during json parsing: 'str' object is not callable", lcm.output) + self.assertIn("ERROR:test_a2c:request_operation returned error during json parsing: 'str' object is not callable", lcm.output) @patch('requests.get') def test_015__api_get(self, mock_req): @@ -151,7 +151,7 @@ def test_015__api_get(self, mock_req): mock_req.side_effect = Exception('exc_api_get') with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual((500, 'exc_api_get'), self.cahandler._api_get('url')) - self.assertIn('ERROR:test_a2c:CAhandler._api_get() returned error: exc_api_get', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error: exc_api_get', lcm.output) @patch.object(requests, 'put') def test_016__api_put(self, mock_req): @@ -171,7 +171,7 @@ def test_017__api_put(self, mock_req): mock_req.return_value = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual(('status_code', "'str' object is not callable"), self.cahandler._api_put('url', 'data')) - self.assertIn("ERROR:test_a2c:CAhandler._api_put() returned error during json parsing: 'str' object is not callable", lcm.output) + self.assertIn("ERROR:test_a2c:request_operation returned error during json parsing: 'str' object is not callable", lcm.output) @patch('requests.put') def test_018__api_put(self, mock_req): @@ -190,7 +190,7 @@ def test_019__api_put(self, mock_req): mock_req.side_effect = Exception('exc_api_put') with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual((500, 'exc_api_put'), self.cahandler._api_put('url', 'data')) - self.assertIn('ERROR:test_a2c:CAhandler._api_put() returned error: exc_api_put', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error: exc_api_put', lcm.output) def test_020__config_check(self): """ test _config_check() """ @@ -653,7 +653,7 @@ def test_051_csr_check(self, mock_dlchk, mock_ehichk): @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_response_parse') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_send') - @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.digicert_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_check') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._config_check') def test_052_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, mock_orderparse): @@ -672,7 +672,7 @@ def test_052_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_response_parse') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_send') - @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.digicert_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_check') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._config_check') def test_053_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, mock_orderparse): @@ -691,7 +691,7 @@ def test_053_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_response_parse') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_send') - @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.digicert_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_check') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._config_check') def test_054_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, mock_orderparse): @@ -710,7 +710,7 @@ def test_054_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_response_parse') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_send') - @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.digicert_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_check') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._config_check') def test_055_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, mock_orderparse): @@ -729,7 +729,7 @@ def test_055_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_response_parse') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._order_send') - @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.digicert_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._csr_check') @patch('examples.ca_handler.digicert_ca_handler.CAhandler._config_check') def test_056_enroll(self, mock_cfgchk, mock_csrchk, mock_cnget, mock_ordersend, mock_orderparse): @@ -762,41 +762,14 @@ def test_058_revoke(self, mock_serial, mock_put): mock_put.return_value = ('code', 'content') self.assertEqual((500, None, 'Failed to parse certificate serial'), self.cahandler.revoke('cert')) - @patch('examples.ca_handler.digicert_ca_handler.csr_san_get') - @patch('examples.ca_handler.digicert_ca_handler.csr_cn_get') - def test_059__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = 'cn' - mock_san_get.return_value = ['foo:san1', 'foo:san2'] - self.assertEqual('cn', self.cahandler._csr_cn_lookup('csr')) - - @patch('examples.ca_handler.digicert_ca_handler.csr_san_get') - @patch('examples.ca_handler.digicert_ca_handler.csr_cn_get') - def test_060__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = None - mock_san_get.return_value = ['foo:san1', 'foo:san2'] - self.assertEqual('san1', self.cahandler._csr_cn_lookup('csr')) - - @patch('examples.ca_handler.digicert_ca_handler.csr_san_get') - @patch('examples.ca_handler.digicert_ca_handler.csr_cn_get') - def test_061__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = None - mock_san_get.return_value = ['foosan1', 'foo:san2'] - with self.assertLogs('test_a2c', level='INFO') as lcm: - self.assertEqual('san2', self.cahandler._csr_cn_lookup('csr')) - self.assertIn('ERROR:test_a2c:CAhandler._csr_cn_lookup() split failed: list index out of range', lcm.output) - - @patch('examples.ca_handler.digicert_ca_handler.csr_san_get') - @patch('examples.ca_handler.digicert_ca_handler.csr_cn_get') - def test_062__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = None - mock_san_get.return_value = None - with self.assertLogs('test_a2c', level='INFO') as lcm: - self.assertFalse(self.cahandler._csr_cn_lookup('csr')) - self.assertIn('ERROR:test_a2c:CAhandler._csr_cn_lookup() no SANs found in CSR', lcm.output) + @patch('examples.ca_handler.digicert_ca_handler.CAhandler._api_put') + @patch('examples.ca_handler.digicert_ca_handler.cert_serial_get') + def test_059_revoke(self, mock_serial, mock_put): + """ test revoke() """ + mock_serial.return_value = 'serial' + mock_put.return_value = (204, 'content') + self.assertEqual((200, None, 'content'), self.cahandler.revoke('cert')) + if __name__ == '__main__': diff --git a/test/test_entrust.py b/test/test_entrust.py index fde69ae7..48b7bea8 100644 --- a/test/test_entrust.py +++ b/test/test_entrust.py @@ -107,7 +107,7 @@ def test_010__api_post(self): self.cahandler.session = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual(('status_code', 'ex_json'), self.cahandler._api_post('url', 'data')) - self.assertIn('ERROR:test_a2c:CAhandler._api_post() returned error during json parsing: ex_json', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error during json parsing: ex_json', lcm.output) def test_011__api_post(self): """ test _api_post() """ @@ -127,7 +127,7 @@ def test_012__api_post(self): self.cahandler.session = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual((500, 'exc_api_post'), self.cahandler._api_post('url', 'data')) - self.assertIn('ERROR:test_a2c:CAhandler._api_post() returned error: exc_api_post', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error: exc_api_post', lcm.output) def test_013__api_get(self): """ test _api_get() """ @@ -151,7 +151,7 @@ def test_014__api_get(self): self.cahandler.session = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual(('status_code', 'ex_json'), self.cahandler._api_get('url')) - self.assertIn('ERROR:test_a2c:CAhandler._api_get() returned error during json parsing: ex_json', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error during json parsing: ex_json', lcm.output) def test_015__api_get(self): """ test _api_get() """ @@ -160,7 +160,7 @@ def test_015__api_get(self): self.cahandler.session = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual((500, 'exc_api_get'), self.cahandler._api_get('url')) - self.assertIn('ERROR:test_a2c:CAhandler._api_get() returned error: exc_api_get', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error: exc_api_get', lcm.output) def test_016__api_put(self): """ test _api_put() """ @@ -185,7 +185,7 @@ def test_017__api_put(self): self.cahandler.session = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual(('status_code', 'ex_json'), self.cahandler._api_put('url', 'data')) - self.assertIn('ERROR:test_a2c:CAhandler._api_put() returned error during json parsing: ex_json', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error during json parsing: ex_json', lcm.output) def test_018__api_put(self): """ test _api_put() """ @@ -205,7 +205,7 @@ def test_019__api_put(self): self.cahandler.session = mockresponse with self.assertLogs('test_a2c', level='INFO') as lcm: self.assertEqual((500, 'exc_api_put'), self.cahandler._api_put('url', 'data')) - self.assertIn('ERROR:test_a2c:CAhandler._api_put() returned error: exc_api_put', lcm.output) + self.assertIn('ERROR:test_a2c:request_operation returned error: exc_api_put', lcm.output) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') def test_020_certificates_get_from_serial(self, mock_api): @@ -242,7 +242,7 @@ def test_023_certificates_get_from_serial(self, mock_api): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_023_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_024_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'foo': 'bar'} mock_eab.return_value = (True, 'handler') @@ -269,7 +269,7 @@ def test_023_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_024_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_025_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -296,7 +296,7 @@ def test_024_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_025_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_026_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'api_url': 'api_url', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -323,7 +323,7 @@ def test_025_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_026_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_027_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'request_timeout': '15', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -350,7 +350,7 @@ def test_026_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_027_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_028_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'request_timeout': 'aa', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -379,7 +379,7 @@ def test_027_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_028_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_029_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'cert_validity_days': '10', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -406,7 +406,7 @@ def test_028_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_029_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_030_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'cert_validity_days': 'aa', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -435,7 +435,7 @@ def test_029_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_030_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_031_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'username': 'username', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -462,7 +462,7 @@ def test_030_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_031_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_032_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'password': 'password', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -489,7 +489,7 @@ def test_031_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_032_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_033_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'organization_name': 'organization_name', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -516,7 +516,7 @@ def test_032_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_033_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_034_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'certtype': 'certtype', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -543,7 +543,7 @@ def test_033_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_034_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_035_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'allowed_domainlist': '["foo", "bar"]', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -570,7 +570,7 @@ def test_034_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_root_load') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_session_load') @patch('examples.ca_handler.entrust_ca_handler.load_config') - def test_035_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): + def test_036_config_load(self, mock_load, mock_session, mock_root, mock_eab, mock_header): """ test load_config() """ mock_load.return_value = {'CAhandler': {'allowed_domainlist': 'bar', 'foo': 'bar'}} mock_eab.return_value = (True, 'handler') @@ -593,14 +593,14 @@ def test_035_config_load(self, mock_load, mock_session, mock_root, mock_eab, moc self.assertEqual('hil', self.cahandler.header_info_field) @patch.dict('os.environ', {'cert_passphrase_var': 'user_var'}) - def test_036_config_passphrase_load(self): + def test_037_config_passphrase_load(self): """ test _config_load - load template with user variable """ config_dic = {'CAhandler': {'cert_passphrase_variable': 'cert_passphrase_var'}} self.cahandler._config_passphrase_load(config_dic) self.assertEqual('user_var', self.cahandler.cert_passphrase) @patch.dict('os.environ', {'cert_passphrase_var': 'user_var'}) - def test_037_config_passphrase_load(self): + def test_038_config_passphrase_load(self): """ test _config_load - load template with user variable """ config_dic = {'CAhandler': {'cert_passphrase_variable': 'does_not_exist'}} with self.assertLogs('test_a2c', level='INFO') as lcm: @@ -609,7 +609,7 @@ def test_037_config_passphrase_load(self): self.assertIn("ERROR:test_a2c:CAhandler._config_passphrase_load() could not load cert_passphrase_variable:'does_not_exist'", lcm.output) @patch.dict('os.environ', {'cert_passphrase_var': 'user_var'}) - def test_038_config_passphrase_load(self): + def test_039_config_passphrase_load(self): """ test _config_load - load template with user variable """ config_dic = {'CAhandler': {'cert_passphrase_variable': 'cert_passphrase_var', 'cert_passphrase': 'cert_passphrase'}} with self.assertLogs('test_a2c', level='INFO') as lcm: @@ -619,7 +619,7 @@ def test_038_config_passphrase_load(self): @patch("builtins.open", mock_open(read_data='cert'), create=True) @patch('os.path.isfile') - def test_039_config_root_load(self, mock_file): + def test_040_config_root_load(self, mock_file): """ _config_root_load() """ mock_file.return_value = True config_dic = {'CAhandler': {'entrust_root_cert': 'root_cert'}} @@ -628,7 +628,7 @@ def test_039_config_root_load(self, mock_file): @patch("builtins.open", mock_open(read_data='cert'), create=True) @patch('os.path.isfile') - def test_040_config_root_load(self, mock_file): + def test_041_config_root_load(self, mock_file): """ _config_root_load() """ mock_file.return_value = False config_dic = {'CAhandler': {'entrust_root_cert': 'root_cert'}} @@ -639,7 +639,7 @@ def test_040_config_root_load(self, mock_file): @patch("builtins.open", mock_open(read_data='cert'), create=True) @patch('os.path.isfile') - def test_041_config_root_load(self, mock_file): + def test_042_config_root_load(self, mock_file): """ _config_root_load() """ mock_file.return_value = False config_dic = {'CAhandler': {'unk': 'root_cert'}} @@ -647,7 +647,7 @@ def test_041_config_root_load(self, mock_file): self.assertIn('290IENlcnRpZmljYXRpb24g', self.cahandler.entrust_root_cert) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_passphrase_load') - def test_042_config_session_load(self, mock_sl): + def test_043_config_session_load(self, mock_sl): """ _config_session_load() """ config_dic = {'CAhandler': {'client_cert': 'client_cert', 'client_key': 'client_key'}} self.cahandler._config_session_load(config_dic) @@ -657,7 +657,7 @@ def test_042_config_session_load(self, mock_sl): @patch('examples.ca_handler.entrust_ca_handler.requests.Session') @patch('examples.ca_handler.entrust_ca_handler.Pkcs12Adapter') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_passphrase_load') - def test_043_config_session_load(self, mock_sl, mock_pkcs12, mock_session): + def test_044_config_session_load(self, mock_sl, mock_pkcs12, mock_session): """ _config_session_load() """ config_dic = {'CAhandler': {'client_cert': 'client_cert'}} mock_session.return_value.__enter__.return_value = Mock() @@ -670,7 +670,7 @@ def test_043_config_session_load(self, mock_sl, mock_pkcs12, mock_session): @patch('examples.ca_handler.entrust_ca_handler.requests.Session') @patch('examples.ca_handler.entrust_ca_handler.Pkcs12Adapter') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_passphrase_load') - def test_044_config_session_load(self, mock_sl, mock_pkcs12, mock_session): + def test_045_config_session_load(self, mock_sl, mock_pkcs12, mock_session): """ _config_session_load() """ config_dic = {'CAhandler': {'foo': 'client_cert'}} with self.assertLogs('test_a2c', level='INFO') as lcm: @@ -680,45 +680,9 @@ def test_044_config_session_load(self, mock_sl, mock_pkcs12, mock_session): self.assertIn('WARNING:test_a2c:CAhandler._config_load() configuration might be incomplete: "client_cert. "client_key" or "client_passphrase[_variable] parameter is missing in config file', lcm.output) self.assertFalse(mock_pkcs12.called) - @patch('examples.ca_handler.entrust_ca_handler.csr_san_get') - @patch('examples.ca_handler.entrust_ca_handler.csr_cn_get') - def test_045__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = 'cn' - mock_san_get.return_value = ['foo:san1', 'foo:san2'] - self.assertEqual('cn', self.cahandler._csr_cn_lookup('csr')) - - @patch('examples.ca_handler.entrust_ca_handler.csr_san_get') - @patch('examples.ca_handler.entrust_ca_handler.csr_cn_get') - def test_046__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = None - mock_san_get.return_value = ['foo:san1', 'foo:san2'] - self.assertEqual('san1', self.cahandler._csr_cn_lookup('csr')) - - @patch('examples.ca_handler.entrust_ca_handler.csr_san_get') - @patch('examples.ca_handler.entrust_ca_handler.csr_cn_get') - def test_047__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = None - mock_san_get.return_value = ['foosan1', 'foo:san2'] - with self.assertLogs('test_a2c', level='INFO') as lcm: - self.assertEqual('san2', self.cahandler._csr_cn_lookup('csr')) - self.assertIn('ERROR:test_a2c:CAhandler._csr_cn_lookup() split failed: list index out of range', lcm.output) - - @patch('examples.ca_handler.entrust_ca_handler.csr_san_get') - @patch('examples.ca_handler.entrust_ca_handler.csr_cn_get') - def test_048__csr_cn_lookup(self, mock_cnget, mock_san_get): - """ test _csr_cn_lookup() """ - mock_cnget.return_value = None - mock_san_get.return_value = None - with self.assertLogs('test_a2c', level='INFO') as lcm: - self.assertFalse(self.cahandler._csr_cn_lookup('csr')) - self.assertIn('ERROR:test_a2c:CAhandler._csr_cn_lookup() no SANs found in CSR', lcm.output) - @patch('examples.ca_handler.entrust_ca_handler.CAhandler._domains_get') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._organizations_get') - def test_049_org_domain_cfg_check(self, mock_org, mock_domain): + def test_046_org_domain_cfg_check(self, mock_org, mock_domain): """ test _org_domain_cfg_check()""" mock_org.return_value = [] with self.assertLogs('test_a2c', level='INFO') as lcm: @@ -728,7 +692,7 @@ def test_049_org_domain_cfg_check(self, mock_org, mock_domain): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._domains_get') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._organizations_get') - def test_050_org_domain_cfg_check(self, mock_org, mock_domain): + def test_047_org_domain_cfg_check(self, mock_org, mock_domain): """ test _org_domain_cfg_check()""" mock_org.return_value = {'foo': 1, 'bar': 2} with self.assertLogs('test_a2c', level='INFO') as lcm: @@ -738,7 +702,7 @@ def test_050_org_domain_cfg_check(self, mock_org, mock_domain): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._domains_get') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._organizations_get') - def test_051_org_domain_cfg_check(self, mock_org, mock_domain): + def test_048_org_domain_cfg_check(self, mock_org, mock_domain): """ test _org_domain_cfg_check()""" mock_org.return_value = {'foo': 1, 'bar': 2} mock_domain.return_value = ['foo1', 'foo2'] @@ -750,7 +714,7 @@ def test_051_org_domain_cfg_check(self, mock_org, mock_domain): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._domains_get') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._organizations_get') - def test_052_org_domain_cfg_check(self, mock_org, mock_domain): + def test_049_org_domain_cfg_check(self, mock_org, mock_domain): """ test _org_domain_cfg_check()""" mock_org.return_value = {'foo': 1, 'bar': 2} mock_domain.return_value = ['foo1', 'foo2'] @@ -762,7 +726,7 @@ def test_052_org_domain_cfg_check(self, mock_org, mock_domain): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._domains_get') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._organizations_get') - def test_053_org_domain_cfg_check(self, mock_org, mock_domain): + def test_050_org_domain_cfg_check(self, mock_org, mock_domain): """ test _org_domain_cfg_check()""" mock_org.return_value = {'foo': 1, 'bar': 2} mock_domain.return_value = ['foo1', 'foo2'] @@ -774,7 +738,7 @@ def test_053_org_domain_cfg_check(self, mock_org, mock_domain): self.assertEqual(['foo3', 'foo4'], self.cahandler.allowed_domainlist) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_054__organizations_get(self, mock_api): + def test_051__organizations_get(self, mock_api): """ test _organizations_get() """ mock_api.return_value = (500, 'foo') self.cahandler.organization_name = 'organization_name' @@ -783,7 +747,7 @@ def test_054__organizations_get(self, mock_api): self.assertIn('ERROR:test_a2c:CAhandler._organizations_get(): malformed response', lcm.output) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_055__organizations_get(self, mock_api): + def test_052__organizations_get(self, mock_api): """ test _organizations_get() """ input_dic = {'organizations': [{'verificationStatus': 'APPROVED', 'name': 'foo', 'clientId': 1}, {'verificationStatus': 'APPROVED', 'name': 'bar', 'clientId': 2}]} mock_api.return_value = (200, input_dic) @@ -791,7 +755,7 @@ def test_055__organizations_get(self, mock_api): self.assertEqual({'foo': 1, 'bar': 2}, self.cahandler._organizations_get()) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_056__organizations_get(self, mock_api): + def test_053__organizations_get(self, mock_api): """ test _organizations_get() """ input_dic = {'organizations': [{'verificationStatus': 'NOTAPPROVED', 'name': 'foo', 'clientId': 1}, {'verificationStatus': 'APPROVED', 'name': 'bar', 'clientId': 2}]} mock_api.return_value = (200, input_dic) @@ -799,7 +763,7 @@ def test_056__organizations_get(self, mock_api): self.assertEqual({'bar': 2}, self.cahandler._organizations_get()) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_057__organizations_get(self, mock_api): + def test_054__organizations_get(self, mock_api): """ test _organizations_get() """ input_dic = {'organizations': [{'name': 'foo', 'clientId': 1}, {'verificationStatus': 'APPROVED', 'name': 'bar', 'clientId': 2}]} mock_api.return_value = (200, input_dic) @@ -807,7 +771,7 @@ def test_057__organizations_get(self, mock_api): self.assertEqual({'bar': 2}, self.cahandler._organizations_get()) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_058__organizations_get(self, mock_api): + def test_055__organizations_get(self, mock_api): """ test _organizations_get() """ input_dic = {'organizations': [{'verificationStatus': 'APPROVED', '_name': 'foo', '_clientId': 1}, {'verificationStatus': 'APPROVED', 'name': 'bar', 'clientId': 2}]} mock_api.return_value = (200, input_dic) @@ -815,7 +779,7 @@ def test_058__organizations_get(self, mock_api): self.assertEqual({'bar': 2}, self.cahandler._organizations_get()) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_059__domains_get(self, mock_api): + def test_056__domains_get(self, mock_api): """ test _organizations_get() """ mock_api.return_value = (500, 'foo') with self.assertLogs('test_a2c', level='INFO') as lcm: @@ -823,14 +787,14 @@ def test_059__domains_get(self, mock_api): self.assertIn('ERROR:test_a2c:CAhandler._domains_get(): malformed response', lcm.output) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_060__domains_get(self, mock_api): + def test_057__domains_get(self, mock_api): """ test _organizations_get() """ input_dic = {'domains': [{'verificationStatus': 'APPROVED', 'domainName': 'foo.bar', 'clientId': 1}, {'verificationStatus': 'APPROVED', 'domainName': 'bar.foo', 'clientId': 2}]} mock_api.return_value = (200, input_dic) self.assertEqual(['foo.bar', 'bar.foo'], self.cahandler._domains_get(1)) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_061__domains_get(self, mock_api): + def test_058__domains_get(self, mock_api): """ test _organizations_get() """ input_dic = {'domains': [{'verificationStatus': 'NOTAPPROVED', 'domainName': 'foo.bar', 'clientId': 1}, {'verificationStatus': 'APPROVED', 'domainName': 'bar.foo', 'clientId': 2}]} mock_api.return_value = (200, input_dic) @@ -838,46 +802,46 @@ def test_061__domains_get(self, mock_api): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_062__domains_get(self, mock_api): + def test_059__domains_get(self, mock_api): """ test _organizations_get() """ input_dic = {'domains': [{'Status': 'APPROVED', 'domainName': 'foo.bar', 'clientId': 1}, {'verificationStatus': 'APPROVED', 'domainName': 'bar.foo', 'clientId': 2}]} mock_api.return_value = (200, input_dic) self.assertEqual(['bar.foo'], self.cahandler._domains_get(1)) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_063__domains_get(self, mock_api): + def test_060__domains_get(self, mock_api): """ test _organizations_get() """ input_dic = {'domains': [{'verificationStatus': 'APPROVED', 'Name': 'foo.bar', 'clientId': 1}, {'verificationStatus': 'APPROVED', 'domainName': 'bar.foo', 'clientId': 2}]} mock_api.return_value = (200, input_dic) self.assertEqual(['bar.foo'], self.cahandler._domains_get(1)) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_064_credential_check(self, mock_api): + def test_061_credential_check(self, mock_api): """ test _organizations_get() """ mock_api.return_value = (500, 'foo') self.assertEqual('Connection to Entrust API failed: foo', self.cahandler.credential_check()) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_065_credential_check(self, mock_api): + def test_062_credential_check(self, mock_api): """ test _organizations_get() """ mock_api.return_value = (200, 'foo') self.assertFalse(self.cahandler.credential_check()) - def test_066_oonfig_check(self): + def test_063_oonfig_check(self): """ test _config_check() """ self.cahandler.api_url = None with self.assertLogs('test_a2c', level='INFO') as lcm: self.cahandler._config_check() self.assertIn('ERROR:test_a2c:CAhandler._config_check() ended with error: api_url parameter in missing in config file', lcm.output) - def test_067_oonfig_check(self): + def test_064_oonfig_check(self): """ test _config_check() """ self.cahandler.api_url = 'api_url' with self.assertLogs('test_a2c', level='INFO') as lcm: self.cahandler._config_check() self.assertIn('ERROR:test_a2c:CAhandler._config_check() ended with error: username parameter in missing in config file', lcm.output) - def test_068_oonfig_check(self): + def test_065_oonfig_check(self): """ test _config_check() """ self.cahandler.api_url = 'api_url' self.cahandler.username = 'username' @@ -885,7 +849,7 @@ def test_068_oonfig_check(self): self.cahandler._config_check() self.assertIn('ERROR:test_a2c:CAhandler._config_check() ended with error: password parameter in missing in config file', lcm.output) - def test_069_oonfig_check(self): + def test_066_oonfig_check(self): """ test _config_check() """ self.cahandler.api_url = 'api_url' self.cahandler.username = 'username' @@ -894,7 +858,7 @@ def test_069_oonfig_check(self): self.cahandler._config_check() self.assertIn('ERROR:test_a2c:CAhandler._config_check() ended with error: organization_name parameter in missing in config file', lcm.output) - def test_070_oonfig_check(self): + def test_067_oonfig_check(self): """ test _config_check() """ self.cahandler.api_url = 'api_url' self.cahandler.username = 'username' @@ -907,7 +871,7 @@ def test_070_oonfig_check(self): @patch('examples.ca_handler.entrust_ca_handler.CAhandler.credential_check') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_check') @patch('examples.ca_handler.entrust_ca_handler.eab_profile_header_info_check') - def test_071_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): + def test_068_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): """ test _enroll_check() """ mock_eab.return_value = 'mock_eab_error' mock_config.return_value = 'mock_config_error' @@ -926,7 +890,7 @@ def test_071_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org @patch('examples.ca_handler.entrust_ca_handler.CAhandler.credential_check') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_check') @patch('examples.ca_handler.entrust_ca_handler.eab_profile_header_info_check') - def test_072_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): + def test_069_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): """ test _enroll_check() """ mock_eab.return_value = False mock_config.return_value = 'mock_config_error' @@ -945,7 +909,7 @@ def test_072_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org @patch('examples.ca_handler.entrust_ca_handler.CAhandler.credential_check') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_check') @patch('examples.ca_handler.entrust_ca_handler.eab_profile_header_info_check') - def test_073_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): + def test_070_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): """ test _enroll_check() """ mock_eab.return_value = False mock_config.return_value = False @@ -964,7 +928,7 @@ def test_073_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org @patch('examples.ca_handler.entrust_ca_handler.CAhandler.credential_check') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_check') @patch('examples.ca_handler.entrust_ca_handler.eab_profile_header_info_check') - def test_074_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): + def test_071_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): """ test _enroll_check() """ mock_eab.return_value = False mock_config.return_value = False @@ -983,7 +947,7 @@ def test_074_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org @patch('examples.ca_handler.entrust_ca_handler.CAhandler.credential_check') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._config_check') @patch('examples.ca_handler.entrust_ca_handler.eab_profile_header_info_check') - def test_075_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): + def test_072_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org, mock_domain): """ test _enroll_check() """ mock_eab.return_value = False mock_config.return_value = False @@ -1000,7 +964,7 @@ def test_075_enroll_check(self, mock_eab, mock_config, mock_credential, mock_org @patch('examples.ca_handler.entrust_ca_handler.CAhandler._certificates_get_from_serial') @patch('examples.ca_handler.entrust_ca_handler.cert_serial_get') @patch('examples.ca_handler.entrust_ca_handler.header_info_get') - def test_076__trackingid_get(self, mock_header, mock_serial, mock_cert): + def test_073__trackingid_get(self, mock_header, mock_serial, mock_cert): """ test _trackingid_get() """ mock_header.return_value = [{'poll_identifier': 'tracking_id'}, {'poll_identifier': 'tracking_id2'}] self.assertEqual('tracking_id', self.cahandler._trackingid_get('csr')) @@ -1011,7 +975,7 @@ def test_076__trackingid_get(self, mock_header, mock_serial, mock_cert): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._certificates_get_from_serial') @patch('examples.ca_handler.entrust_ca_handler.cert_serial_get') @patch('examples.ca_handler.entrust_ca_handler.header_info_get') - def test_077__trackingid_get(self, mock_header, mock_serial, mock_cert): + def test_074__trackingid_get(self, mock_header, mock_serial, mock_cert): """ test _trackingid_get() """ mock_header.return_value = [{'identifier': 'tracking_id1'}, {'poll_identifier': 'tracking_id2'}] self.assertEqual('tracking_id2', self.cahandler._trackingid_get('csr')) @@ -1021,7 +985,7 @@ def test_077__trackingid_get(self, mock_header, mock_serial, mock_cert): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._certificates_get_from_serial') @patch('examples.ca_handler.entrust_ca_handler.cert_serial_get') @patch('examples.ca_handler.entrust_ca_handler.header_info_get') - def test_078__trackingid_get(self, mock_header, mock_serial, mock_cert): + def test_075__trackingid_get(self, mock_header, mock_serial, mock_cert): """ test _trackingid_get() """ mock_header.return_value = [] mock_serial.return_value = 'serial' @@ -1033,7 +997,7 @@ def test_078__trackingid_get(self, mock_header, mock_serial, mock_cert): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._certificates_get_from_serial') @patch('examples.ca_handler.entrust_ca_handler.cert_serial_get') @patch('examples.ca_handler.entrust_ca_handler.header_info_get') - def test_079__trackingid_get(self, mock_header, mock_serial, mock_cert): + def test_076__trackingid_get(self, mock_header, mock_serial, mock_cert): """ test _trackingid_get() """ mock_header.return_value = [] mock_serial.return_value = 'serial' @@ -1045,7 +1009,7 @@ def test_079__trackingid_get(self, mock_header, mock_serial, mock_cert): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._certificates_get_from_serial') @patch('examples.ca_handler.entrust_ca_handler.cert_serial_get') @patch('examples.ca_handler.entrust_ca_handler.header_info_get') - def test_080__trackingid_get(self, mock_header, mock_serial, mock_cert): + def test_077__trackingid_get(self, mock_header, mock_serial, mock_cert): """ test _trackingid_get() """ mock_header.return_value = [] mock_serial.return_value = 'serial' @@ -1056,7 +1020,7 @@ def test_080__trackingid_get(self, mock_header, mock_serial, mock_cert): @patch('examples.ca_handler.entrust_ca_handler.b64_encode') @patch('examples.ca_handler.entrust_ca_handler.cert_pem2der') - def test_081_response_parse(self, mock_der, mock_enc): + def test_078_response_parse(self, mock_der, mock_enc): """ test _rsponse_parse() """ mock_der.return_value = 'cert_data' mock_enc.return_value = 'mock_enc' @@ -1068,7 +1032,7 @@ def test_081_response_parse(self, mock_der, mock_enc): @patch('examples.ca_handler.entrust_ca_handler.b64_encode') @patch('examples.ca_handler.entrust_ca_handler.cert_pem2der') - def test_082_response_parse(self, mock_der, mock_enc): + def test_079_response_parse(self, mock_der, mock_enc): """ test _rsponse_parse() """ mock_der.return_value = 'cert_data' mock_enc.return_value = 'mock_enc' @@ -1080,7 +1044,7 @@ def test_082_response_parse(self, mock_der, mock_enc): @patch('examples.ca_handler.entrust_ca_handler.b64_encode') @patch('examples.ca_handler.entrust_ca_handler.cert_pem2der') - def test_083_response_parse(self, mock_der, mock_enc): + def test_080_response_parse(self, mock_der, mock_enc): """ test _rsponse_parse() """ mock_der.return_value = 'cert_data' mock_enc.return_value = 'mock_enc' @@ -1092,7 +1056,7 @@ def test_083_response_parse(self, mock_der, mock_enc): @patch('examples.ca_handler.entrust_ca_handler.b64_encode') @patch('examples.ca_handler.entrust_ca_handler.cert_pem2der') - def test_084_response_parse(self, mock_der, mock_enc): + def test_081_response_parse(self, mock_der, mock_enc): """ test _rsponse_parse() """ mock_der.return_value = 'cert_data' mock_enc.return_value = 'mock_enc' @@ -1104,7 +1068,7 @@ def test_084_response_parse(self, mock_der, mock_enc): @patch('examples.ca_handler.entrust_ca_handler.b64_encode') @patch('examples.ca_handler.entrust_ca_handler.cert_pem2der') - def test_085_response_parse(self, mock_der, mock_enc): + def test_082_response_parse(self, mock_der, mock_enc): """ test _rsponse_parse() """ mock_der.return_value = 'cert_data' mock_enc.return_value = 'mock_enc' @@ -1115,9 +1079,9 @@ def test_085_response_parse(self, mock_der, mock_enc): self.assertTrue(mock_enc.called) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._response_parse') - @patch('examples.ca_handler.entrust_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.entrust_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_post') - def test_086_enroll(self, mock_req, mock_cn, mock_parse): + def test_083_enroll(self, mock_req, mock_cn, mock_parse): """ test _enroll() """ mock_cn.return_value = 'cn' mock_req.return_value = (201, 'response') @@ -1128,9 +1092,9 @@ def test_086_enroll(self, mock_req, mock_cn, mock_parse): self.assertTrue(mock_parse.called) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._response_parse') - @patch('examples.ca_handler.entrust_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.entrust_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_post') - def test_087_enroll(self, mock_req, mock_cn, mock_parse): + def test_084_enroll(self, mock_req, mock_cn, mock_parse): """ test _enroll() """ mock_cn.return_value = 'cn' mock_req.return_value = (404, 'response') @@ -1141,9 +1105,9 @@ def test_087_enroll(self, mock_req, mock_cn, mock_parse): self.assertFalse(mock_parse.called) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._response_parse') - @patch('examples.ca_handler.entrust_ca_handler.CAhandler._csr_cn_lookup') + @patch('examples.ca_handler.entrust_ca_handler.csr_cn_lookup') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_post') - def test_088_enroll(self, mock_req, mock_cn, mock_parse): + def test_085_enroll(self, mock_req, mock_cn, mock_parse): """ test _enroll() """ mock_cn.return_value = 'cn' mock_req.return_value = (404, {'errors': 'response, response2'}) @@ -1155,7 +1119,7 @@ def test_088_enroll(self, mock_req, mock_cn, mock_parse): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._enroll') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._enroll_check') - def test_089_enroll(self, mock_chk, mock_enroll): + def test_086_enroll(self, mock_chk, mock_enroll): """ test enroll() """ mock_chk.return_value = None mock_enroll.return_value = ('mock_err', 'mock_bundle', 'mock_raw', 'mock_poll') @@ -1163,7 +1127,7 @@ def test_089_enroll(self, mock_chk, mock_enroll): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._enroll') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._enroll_check') - def test_090_enroll(self, mock_chk, mock_enroll): + def test_087_enroll(self, mock_chk, mock_enroll): """ test enroll() """ mock_chk.return_value = 'mock_chk' mock_enroll.return_value = ('mock_err', 'mock_bundle', 'mock_raw', 'mock_poll') @@ -1171,7 +1135,7 @@ def test_090_enroll(self, mock_chk, mock_enroll): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_post') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._trackingid_get') - def test_091_revoke(self, mock_track, mock_req): + def test_088_revoke(self, mock_track, mock_req): """ test revoke() """ mock_track.return_value = 'tracking_id' mock_req.return_value = (200, 'response') @@ -1179,7 +1143,7 @@ def test_091_revoke(self, mock_track, mock_req): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_post') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._trackingid_get') - def test_092_revoke(self, mock_track, mock_req): + def test_089_revoke(self, mock_track, mock_req): """ test revoke() """ mock_track.return_value = 'tracking_id' mock_req.return_value = (500, 'response') @@ -1187,14 +1151,14 @@ def test_092_revoke(self, mock_track, mock_req): @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_post') @patch('examples.ca_handler.entrust_ca_handler.CAhandler._trackingid_get') - def test_093_revoke(self, mock_track, mock_req): + def test_090_revoke(self, mock_track, mock_req): """ test revoke() """ mock_track.return_value = None mock_req.return_value = (200, 'response') self.assertEqual((500, 'urn:ietf:params:acme:error:serverInternal', 'Failed to get tracking id'), self.cahandler.revoke('csr')) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_094_certificates_get(self, mock_req): + def test_091_certificates_get(self, mock_req): """ test certificates_get() """ mock_req.return_value = (500, 'response') with self.assertLogs('test_a2c', level='INFO') as lcm: @@ -1202,7 +1166,7 @@ def test_094_certificates_get(self, mock_req): self.assertIn('ERROR:test_a2c:CAhandler.certificates_get() failed with code: 500', lcm.output) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_095_certificates_get(self, mock_req): + def test_092_certificates_get(self, mock_req): """ test certificates_get() """ content = {'certificates': [1, 2, 3, 4], 'summary': {'total': 4}} mock_req.return_value = (200, content) @@ -1211,7 +1175,7 @@ def test_095_certificates_get(self, mock_req): self.assertIn('INFO:test_a2c:fetching certs offset: 0, limit: 200, total: 1, buffered: 0', lcm.output) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_096_certificates_get(self, mock_req): + def test_093_certificates_get(self, mock_req): """ test certificates_get() """ response1 = (200, {'certificates': [1, 2, 3, 4], 'summary': {'total': 8}}) response2 = (200, {'certificates': [5, 6, 7, 8]}) @@ -1222,7 +1186,7 @@ def test_096_certificates_get(self, mock_req): self.assertIn('INFO:test_a2c:fetching certs offset: 200, limit: 200, total: 8, buffered: 4', lcm.output) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_097_certificates_get(self, mock_req): + def test_094_certificates_get(self, mock_req): """ test certificates_get() """ response1 = (200, {'certificates': [1, 2, 3, 4]}) response2 = (200, {'certificates': [5, 6, 7, 8]}) @@ -1232,7 +1196,7 @@ def test_097_certificates_get(self, mock_req): self.assertEqual('Certificates lookup failed: did not get any total value', str(err.exception)) @patch('examples.ca_handler.entrust_ca_handler.CAhandler._api_get') - def test_098_certificates_get(self, mock_req): + def test_095_certificates_get(self, mock_req): """ test certificates_get() """ response1 = (200, {'certificates': [1, 2, 3, 4], 'summary': {'total': 9}}) response2 = (200, {'certificates': [5, 6, 7, 8]}) diff --git a/test/test_helper.py b/test/test_helper.py index cec6edda..eef1df18 100644 --- a/test/test_helper.py +++ b/test/test_helper.py @@ -26,7 +26,7 @@ def setUp(self): """ setup unittest """ import logging logging.basicConfig(level=logging.CRITICAL) - from acme_srv.helper import b64decode_pad, b64_decode, b64_encode, b64_url_encode, b64_url_recode, convert_string_to_byte, convert_byte_to_string, decode_message, decode_deserialize, get_url, generate_random_string, signature_check, validate_email, uts_to_date_utc, date_to_uts_utc, load_config, cert_serial_get, cert_san_get, cert_san_pyopenssl_get, cert_dates_get, build_pem_file, date_to_datestr, datestr_to_date, dkeys_lower, csr_cn_get, cert_pubkey_get, csr_pubkey_get, url_get, url_get_with_own_dns, dns_server_list_load, csr_san_get, csr_san_byte_get, csr_extensions_get, fqdn_resolve, fqdn_in_san_check, sha256_hash, sha256_hash_hex, cert_der2pem, cert_pem2der, cert_extensions_get, csr_dn_get, logger_setup, logger_info, print_debug, jwk_thumbprint_get, allowed_gai_family, patched_create_connection, validate_csr, servercert_get, txt_get, proxystring_convert, proxy_check, handle_exception, ca_handler_load, eab_handler_load, hooks_load, error_dic_get, _logger_nonce_modify, _logger_certificate_modify, _logger_token_modify, _logger_challenges_modify, config_check, cert_issuer_get, cert_cn_get, string_sanitize, pembundle_to_list, certid_asn1_get, certid_check, certid_hex_get, v6_adjust, ipv6_chk, ip_validate, header_info_get, encode_url, uts_now, cert_ski_get, cert_ski_pyopenssl_get, cert_aki_get, cert_aki_pyopenssl_get, validate_fqdn, validate_ip, validate_identifier, header_info_field_validate, header_info_lookup, config_eab_profile_load, config_headerinfo_load, domainlist_check, allowed_domainlist_check, eab_profile_string_check, eab_profile_list_check, eab_profile_check, eab_profile_header_info_check, cert_extensions_py_openssl_get, cryptography_version_get, cn_validate, csr_subject_get, eab_profile_subject_string_check, eab_profile_subject_check, csr_cn_lookup + from acme_srv.helper import b64decode_pad, b64_decode, b64_encode, b64_url_encode, b64_url_recode, convert_string_to_byte, convert_byte_to_string, decode_message, decode_deserialize, get_url, generate_random_string, signature_check, validate_email, uts_to_date_utc, date_to_uts_utc, load_config, cert_serial_get, cert_san_get, cert_san_pyopenssl_get, cert_dates_get, build_pem_file, date_to_datestr, datestr_to_date, dkeys_lower, csr_cn_get, cert_pubkey_get, csr_pubkey_get, url_get, url_get_with_own_dns, dns_server_list_load, csr_san_get, csr_san_byte_get, csr_extensions_get, fqdn_resolve, fqdn_in_san_check, sha256_hash, sha256_hash_hex, cert_der2pem, cert_pem2der, cert_extensions_get, csr_dn_get, logger_setup, logger_info, print_debug, jwk_thumbprint_get, allowed_gai_family, patched_create_connection, validate_csr, servercert_get, txt_get, proxystring_convert, proxy_check, handle_exception, ca_handler_load, eab_handler_load, hooks_load, error_dic_get, _logger_nonce_modify, _logger_certificate_modify, _logger_token_modify, _logger_challenges_modify, config_check, cert_issuer_get, cert_cn_get, string_sanitize, pembundle_to_list, certid_asn1_get, certid_check, certid_hex_get, v6_adjust, ipv6_chk, ip_validate, header_info_get, encode_url, uts_now, cert_ski_get, cert_ski_pyopenssl_get, cert_aki_get, cert_aki_pyopenssl_get, validate_fqdn, validate_ip, validate_identifier, header_info_field_validate, header_info_lookup, config_eab_profile_load, config_headerinfo_load, domainlist_check, allowed_domainlist_check, eab_profile_string_check, eab_profile_list_check, eab_profile_check, eab_profile_header_info_check, cert_extensions_py_openssl_get, cryptography_version_get, cn_validate, csr_subject_get, eab_profile_subject_string_check, eab_profile_subject_check, csr_cn_lookup, request_operation self.logger = logging.getLogger('test_a2c') self.allowed_gai_family = allowed_gai_family self.b64_decode = b64_decode @@ -127,6 +127,7 @@ def setUp(self): self.eab_profile_subject_string_check = eab_profile_subject_string_check self.eab_profile_subject_check = eab_profile_subject_check self.csr_cn_lookup = csr_cn_lookup + self.request_operation = request_operation def test_001_helper_b64decode_pad(self): """ test b64decode_pad() method with a regular base64 encoded string """ @@ -3106,6 +3107,118 @@ def test_062_csr_cn_lookup(self, mock_cnget, mock_san_get): self.assertFalse(self.csr_cn_lookup(self.logger, 'csr')) self.assertIn('ERROR:test_a2c:CAhandler._csr_cn_lookup() no SANs found in CSR', lcm.output) + @patch('acme_srv.helper.requests.put') + @patch('acme_srv.helper.requests.post') + @patch('acme_srv.helper.requests.get') + def test_063_request_operation(self, mock_get, mock_post, mock_put): + """ test request_operation() """ + mockresponse_get = Mock() + mockresponse_get.status_code = 'status_code' + mockresponse_get.json = lambda: {'get': 'get'} + mock_get.return_value = mockresponse_get + mockresponse_post = Mock() + mockresponse_post.status_code = 'status_code' + mockresponse_post.json = lambda: {'post': 'post'} + mock_post.return_value = mockresponse_post + mockresponse_put = Mock() + mockresponse_put.status_code = 'status_code' + mockresponse_put.json = lambda: {'put': 'put'} + mock_put.return_value = mockresponse_put + self.assertEqual(('status_code', {'get': 'get'}), self.request_operation(logger=self.logger, url='foo', method='get')) + self.assertTrue(mock_get.called) + self.assertFalse(mock_post.called) + self.assertFalse(mock_put.called) + + @patch('acme_srv.helper.requests.put') + @patch('acme_srv.helper.requests.post') + @patch('acme_srv.helper.requests.get') + def test_064_request_operation(self, mock_get, mock_post, mock_put): + """ test request_operation() """ + mockresponse_get = Mock() + mockresponse_get.status_code = 'status_code' + mockresponse_get.json = lambda: {'get': 'get'} + mock_get.return_value = mockresponse_get + mockresponse_post = Mock() + mockresponse_post.status_code = 'status_code' + mockresponse_post.json = lambda: {'post': 'post'} + mock_post.return_value = mockresponse_post + mockresponse_put = Mock() + mockresponse_put.status_code = 'status_code' + mockresponse_put.json = lambda: {'put': 'put'} + mock_put.return_value = mockresponse_put + self.assertEqual(('status_code', {'post': 'post'}), self.request_operation(logger=self.logger, url='foo', method='post')) + self.assertFalse(mock_get.called) + self.assertTrue(mock_post.called) + self.assertFalse(mock_put.called) + + @patch('acme_srv.helper.requests.put') + @patch('acme_srv.helper.requests.post') + @patch('acme_srv.helper.requests.get') + def test_065_request_operation(self, mock_get, mock_post, mock_put): + """ test request_operation() """ + mockresponse_get = Mock() + mockresponse_get.status_code = 'status_code' + mockresponse_get.json = lambda: {'get': 'get'} + mock_get.return_value = mockresponse_get + mockresponse_post = Mock() + mockresponse_post.status_code = 'status_code' + mockresponse_post.json = lambda: {'post': 'post'} + mock_post.return_value = mockresponse_post + mockresponse_put = Mock() + mockresponse_put.status_code = 'status_code' + mockresponse_put.json = lambda: {'put': 'put'} + mock_put.return_value = mockresponse_put + self.assertEqual(('status_code', {'put': 'put'}), self.request_operation(logger=self.logger, url='foo', method='put')) + self.assertFalse(mock_get.called) + self.assertFalse(mock_post.called) + self.assertTrue(mock_put.called) + + @patch('acme_srv.helper.requests.put') + @patch('acme_srv.helper.requests.post') + @patch('acme_srv.helper.requests.get') + def test_066_request_operation(self, mock_get, mock_post, mock_put): + """ test request_operation() """ + mockresponse_get = Mock() + mockresponse_get.status_code = 'status_code' + mockresponse_get.json = 'string' + mock_get.return_value = mockresponse_get + self.assertEqual(('status_code', "'str' object is not callable"), self.request_operation(logger=self.logger, url='foo', method='get')) + self.assertTrue(mock_get.called) + self.assertFalse(mock_post.called) + self.assertFalse(mock_put.called) + + @patch('acme_srv.helper.requests.put') + @patch('acme_srv.helper.requests.post') + @patch('acme_srv.helper.requests.get') + def test_067_request_operation(self, mock_get, mock_post, mock_put): + """ test request_operation() """ + mockresponse_get = Mock() + mockresponse_get.status_code = 'status_code' + mockresponse_get.json = 'string' + mockresponse_get.text = None + mock_get.return_value = mockresponse_get + self.assertEqual(('status_code', None), self.request_operation(logger=self.logger, url='foo', method='get')) + self.assertTrue(mock_get.called) + self.assertFalse(mock_post.called) + self.assertFalse(mock_put.called) + + @patch('acme_srv.helper.requests.put') + @patch('acme_srv.helper.requests.post') + @patch('acme_srv.helper.requests.get') + def test_068_request_operation(self, mock_get, mock_post, mock_put): + """ test request_operation() """ + mockresponse_get = Mock() + mockresponse_get.status_code = 'status_code' + mockresponse_get.json = 'string' + mockresponse_get.text = None + mock_get.return_value = mockresponse_get + with self.assertLogs('test_a2c', level='INFO') as lcm: + self.assertEqual((500, "'NoneType' object has no attribute 'status_code'"), self.request_operation(logger=self.logger, url='foo', method='unknown')) + self.assertIn("ERROR:test_a2c:request_operation returned error: 'NoneType' object has no attribute 'status_code'", lcm.output) + self.assertFalse(mock_get.called) + self.assertFalse(mock_post.called) + self.assertFalse(mock_put.called) + if __name__ == '__main__': unittest.main()