Unit Tests for create_txt_record, delete_txt_record, wait_for_dns_change

This commit is contained in:
Kush Bavishi 2019-08-07 13:04:38 -07:00
parent a7c2b970b0
commit 6e84e1fd59
1 changed files with 76 additions and 41 deletions

View File

@ -374,50 +374,85 @@ class TestAcme(unittest.TestCase):
result = ultradns.get_ultradns_token() result = ultradns.get_ultradns_token()
self.assertTrue(len(result) > 0) self.assertTrue(len(result) > 0)
@patch("lemur.plugins.lemur_acme.ultradns.get_zone_name")
@patch("lemur.plugins.lemur_acme.ultradns._post")
@patch("lemur.plugins.lemur_acme.ultradns.current_app") @patch("lemur.plugins.lemur_acme.ultradns.current_app")
def test_create_txt_record(self, mock_current_app, mock__post, mock_get_zone_name): def test_create_txt_record(self, mock_current_app):
domain = "test.example.com" domain = "_acme_challenge.test.example.com"
token = "ABCDEFGHIJ"
account_number = "1234567890"
path = "a/b/c"
paginate_response = [{'properties': {'name': 'example.com.', 'accountName': 'netflix', 'type': 'PRIMARY',
'dnssecStatus': 'UNSIGNED', 'status': 'ACTIVE', 'resourceRecordCount': 9,
'lastModifiedDateTime': '2017-06-14T06:45Z'}, 'registrarInfo': {
'nameServers': {'missing': ['pdns154.ultradns.com.', 'pdns154.ultradns.net.', 'pdns154.ultradns.biz.',
'pdns154.ultradns.org.']}}, 'inherit': 'ALL'},
{'properties': {'name': 'test.example.com.', 'accountName': 'netflix', 'type': 'PRIMARY',
'dnssecStatus': 'UNSIGNED', 'status': 'ACTIVE', 'resourceRecordCount': 9,
'lastModifiedDateTime': '2017-06-14T06:45Z'}, 'registrarInfo': {
'nameServers': {'missing': ['pdns154.ultradns.com.', 'pdns154.ultradns.net.',
'pdns154.ultradns.biz.', 'pdns154.ultradns.org.']}},
'inherit': 'ALL'},
{'properties': {'name': 'example2.com.', 'accountName': 'netflix', 'type': 'SECONDARY',
'dnssecStatus': 'UNSIGNED', 'status': 'ACTIVE', 'resourceRecordCount': 9,
'lastModifiedDateTime': '2017-06-14T06:45Z'}, 'registrarInfo': {
'nameServers': {'missing': ['pdns154.ultradns.com.', 'pdns154.ultradns.net.',
'pdns154.ultradns.biz.', 'pdns154.ultradns.org.']}},
'inherit': 'ALL'}]
ultradns._paginate = Mock(path, "zones")
ultradns._paginate.side_effect = [[paginate_response]]
mock_current_app.logger.debug = Mock()
ultradns._post = Mock()
log_data = {
"function": "create_txt_record",
"fqdn": domain,
"token": token,
"message": "TXT record created"
}
result = ultradns.create_txt_record(domain, token, account_number)
mock_current_app.logger.debug.assert_called_with(log_data)
@patch("lemur.plugins.lemur_acme.ultradns.current_app")
@patch("lemur.extensions.metrics")
def test_delete_txt_record(self, mock_metrics, mock_current_app):
domain = "_acme_challenge.test.example.com"
token = "ABCDEFGHIJ" token = "ABCDEFGHIJ"
account_number = "1234567890" account_number = "1234567890"
change_id = (domain, token) change_id = (domain, token)
mock_current_app.logger.debug = Mock()
mock_get_zone_name = Mock(domain, account_number, return_value="example.com")
path = "a/b/c" path = "a/b/c"
params = { paginate_response = [{'properties': {'name': 'example.com.', 'accountName': 'netflix', 'type': 'PRIMARY',
"test": "Test" 'dnssecStatus': 'UNSIGNED', 'status': 'ACTIVE', 'resourceRecordCount': 9,
} 'lastModifiedDateTime': '2017-06-14T06:45Z'}, 'registrarInfo': {
mock__post = Mock(path, params) 'nameServers': {'missing': ['pdns154.ultradns.com.', 'pdns154.ultradns.net.', 'pdns154.ultradns.biz.',
result = ultradns.create_txt_record(domain, token, account_number) 'pdns154.ultradns.org.']}}, 'inherit': 'ALL'},
self.assertEqual(type(change_id), type(result)) {'properties': {'name': 'test.example.com.', 'accountName': 'netflix', 'type': 'PRIMARY',
'dnssecStatus': 'UNSIGNED', 'status': 'ACTIVE', 'resourceRecordCount': 9,
# @patch("lemur.plugins.lemur_acme.ultradns.get_zone_name") 'lastModifiedDateTime': '2017-06-14T06:45Z'}, 'registrarInfo': {
# @patch("lemur.plugins.lemur_acme.ultradns._get") 'nameServers': {'missing': ['pdns154.ultradns.com.', 'pdns154.ultradns.net.',
# @patch("lemur.plugins.lemur_acme.ultradns._delete") 'pdns154.ultradns.biz.', 'pdns154.ultradns.org.']}},
# @patch("lemur.plugins.lemur_acme.ultradns._post") 'inherit': 'ALL'},
# @patch("lemur.plugins.lemur_acme.ultradns.current_app") {'properties': {'name': 'example2.com.', 'accountName': 'netflix', 'type': 'SECONDARY',
# def test_delete_txt_record(self, mock_get_zone_name): 'dnssecStatus': 'UNSIGNED', 'status': 'ACTIVE', 'resourceRecordCount': 9,
# domain = "test.example.com" 'lastModifiedDateTime': '2017-06-14T06:45Z'}, 'registrarInfo': {
# token = "ABCDEFGHIJ" 'nameServers': {'missing': ['pdns154.ultradns.com.', 'pdns154.ultradns.net.',
# account_number = "1234567890" 'pdns154.ultradns.biz.', 'pdns154.ultradns.org.']}},
# change_id = (domain, token) 'inherit': 'ALL'}]
# mock_get_zone_name = Mock(domain, account_number, return_value="example.com") ultradns._paginate = Mock(path, "zones")
ultradns._paginate.side_effect = [[paginate_response]]
# @patch("lemur.plugins.lemur_acme.ultradns.get_authoritative_nameserver") mock_current_app.logger.debug = Mock()
# @patch("lemur.plugins.lemur_acme.ultradns._has_dns_propagated") ultradns._post = Mock()
# @patch("lemur.plugins.lemur_acme.ultradns.current_app") ultradns._get = Mock()
# def test_wait_for_dns_change(self, mock_current_app, mock_has_dns_propagated, mock_get_authoritative_nameserver): ultradns._get.return_value = {'zoneName': 'test.example.com.com',
# domain = "test.example.com" 'rrSets': [{'ownerName': '_acme-challenge.test.example.com.',
# token = "ABCDEFGHIJ" 'rrtype': 'TXT (16)', 'ttl': 5, 'rdata': ['ABCDEFGHIJ']}],
# account_number = "1234567890" 'queryInfo': {'sort': 'OWNER', 'reverse': False, 'limit': 100},
# change_id = (domain, token) 'resultInfo': {'totalCount': 1, 'offset': 0, 'returnedCount': 1}}
# mock_current_app.logger.debug = Mock() ultradns._delete = Mock()
# result = ultradns.wait_for_dns_change(change_id, token) mock_metrics.send = Mock()
# self.assertEqual(result, true) mock_current_app.logger.debug.assert_not_called()
mock_metrics.send.assert_not_called()
# def test_has_dns_propagated(self):
@patch("lemur.extensions.metrics")
def test_wait_for_dns_change(self, mock_metrics):
ultradns._has_dns_propagated = Mock(return_value=True)
ultradns.get_authoritative_nameserver = Mock(return_value="0.0.0.0")
mock_metrics.send = Mock()
mock_metrics.send.assert_not_called()