Skip to content

Commit

Permalink
Merge pull request #11 from Domain-Connect/feature/templatetestcases
Browse files Browse the repository at this point in the history
Variable extraction for groups & bugfix on empty host from variable in pointsTo/target
  • Loading branch information
pawel-kow committed Aug 6, 2023
2 parents ed6f40b + d98fc5a commit 31e2f1c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
27 changes: 25 additions & 2 deletions Test.py
Expand Up @@ -66,12 +66,14 @@ def TestSig(title, provider_id, service_id, qs, sig, key, ignore_signature, expe
_testResults.Fail()


def TestRecordsException(title, template_records, zone_records, domain, host, params, exception, verbose=False):
def TestRecordsException(title, template_records, zone_records, domain, host, params, exception, verbose=False,
redirect_records=None):
print(bcolors.OKBLUE + "Test: " + bcolors.ENDC + title)

try:
new_records = []
new_records, deleted_records, final_records = process_records(template_records, zone_records, domain, host, params, [])
new_records, deleted_records, final_records = process_records(template_records, zone_records, domain, host,
params, [], redirect_records=redirect_records)
_testResults.Fail()
except exception as e:
_testResults.Pass(str(e))
Expand Down Expand Up @@ -384,6 +386,27 @@ def ExceptionTests():
template_records = [{'type': 'CNAME', 'host': '@', 'pointsTo': 'foo.com', 'ttl': 400}]
TestRecordsException("CNAME at Apex Test", template_records, zone_records, 'foo.com', '', {}, InvalidData)

template_records = [{'type': 'CNAME', 'host': 'foo', 'pointsTo': '', 'ttl': 600}]
TestRecordsException("CNAME empty pointsTo", template_records, zone_records, 'foo.com', '', {}, InvalidData)

template_records = [{'type': 'CNAME', 'host': 'foo', 'pointsTo': '%var%', 'ttl': 600}]
TestRecordsException("CNAME empty pointsTo from variable", template_records, zone_records, 'foo.com', '', {'var': ''}, InvalidData)

template_records = [{'type': 'CNAME', 'host': 'foo', 'pointsTo': '%var%', 'ttl': 600}]
TestRecordsException("CNAME empty pointsTo from missing parameter", template_records, zone_records, 'foo.com', '', {}, MissingParameter)

template_records = [{'type': 'A', 'host': '@', 'pointsTo': '', 'ttl': 600}]
TestRecordsException("A empty pointsTo from variable", template_records, zone_records, 'foo.com', '', {}, InvalidData)

redir_template = [
{'type': 'A', 'pointsTo': '127.0.0.1', 'ttl': 600},
{'type': 'AAAA', 'pointsTo': '::1', 'ttl': 600}
]

template_records = [{'type': 'REDIR301', 'host': '@', 'target': '', 'ttl': 600}]
TestRecordsException("REDIR301 empty target from variable", template_records, zone_records, 'foo.com', '', {},
InvalidData, redirect_records=redir_template)


def SigTests():
sig = 'LyCE+7H0zr/XHaxX36pdD1eSQENRiGTFxm79m7A5NLDPiUKLe71IrsEgnDLN76ndQcLTZlr4+HhpWzKZKyFl9ieEpNzZlDHRp35H83Erhm0eDctUmI1Zct51alZ8RuTL+aa29WC+AM7+gSpnL/AHl9mxckyeEuFFqXcl/3ShwK2F9x/7r+cICefiUEzsZN3EuqOvwqQkBSqcdVy/ohjNAG/InYAYSX+0fUK9UNQfQYkuPqOAptPRjX+hUnYsXUk/eQq16aX7TzhZm+eEq+En+oiEgh7qps1yvGbJm6QXKovan/sqng40R6FBP3R3dvfZC6QrfCUtGpQ8c0D0S5oLBw=='
Expand Down
29 changes: 20 additions & 9 deletions domainconnectzone/DomainConnect.py
Expand Up @@ -166,7 +166,7 @@ def resolve_variables(input_, domain, host, params, recordKey):
return '@' # Bind format for "example.com."

# For a pointsTo or target they are never relative; so we fill in the values
elif recordKey in ['target', 'pointsTo']:
elif recordKey in ['target', 'pointsTo'] and input_ == '@':
if host:
return host + '.' + domain
else:
Expand Down Expand Up @@ -740,11 +740,13 @@ def prompt_variables(template_record, value, params):
#
# Will prompt for the variable values in each record in the template
#
def prompt_records(template_records):
def prompt_records(template_records, group=None):

params = {}

for template_record in template_records:
if group is not None and ('groupId' not in template_record or template_record['groupId'] != group):
continue
template_record_type = template_record['type']

if template_record_type in ['A', 'AAAA', 'MX', 'CNAME', 'NS', 'TXT', 'SPFM', 'REDIR301', 'REDIR302']:
Expand Down Expand Up @@ -784,6 +786,12 @@ def __init__(self, template_path=None):
if os.path.isfile(schema_path) and os.access(schema_path, os.R_OK):
with open(schema_path, 'r') as f:
self._schema = json.load(f)
else:
self._schema = None

@property
def schema(self):
return self._schema

@property
def templates(self):
Expand Down Expand Up @@ -857,19 +865,22 @@ def create_template(self, template):
json.dump(template, f, indent=2)

@staticmethod
def get_variable_names(template, variables=None):
def get_variable_names(template, variables=None, group=None):
global raw_input
try:
old_raw_input = raw_input
raw_input = lambda: ''
params = prompt_records(template['records'])
params = prompt_records(template['records'], group)
finally:
raw_input = old_raw_input
pars = {
'domain': '',
'host': '',
'group': ''
}
if group is None:
pars = {
'domain': '',
'host': '',
'group': ''
}
else:
pars = {}
params = {**pars, **params}
if variables is not None:
for label in params:
Expand Down

0 comments on commit 31e2f1c

Please sign in to comment.