def nic_update(): """ Update a dynamic DNS record """ if request.args.get('offline'): return NOT_SUPPORTED # Check for a user agent user_agent = request.user_agent.string if not user_agent: print("WTF") if not user_agent or user_agent in app.config['BAD_USER_AGENTS']: return BAD_USER_AGENT try: hostname = request.args.get('hostname') resource_record = route53.find_resource_record(hostname) except ValueError: return NO_HOST except: return GENERAL_ERROR if not resource_record: return NO_HOST # TODO - A sanity check of the value would be good, but good luck doing it # easily in anything before Python 3.3+ which got 'ipaddress' module # TODO - Comma separated values should be allowed myip = request.args.get('myip', request.remote_addr) if myip == resource_record['ResourceRecords'][0]['Value']: return NO_CHANGE % myip try: if not route53.update_resource_record(resource_record, myip): return GENERAL_ERROR except: return GENERAL_ERROR return IP_CHANGED % myip
def test_find_resource_record(self): """ Test find_resource_record functionality """ client = MockRoute53Client() hostname = "www.google.com" # Test some invalid inputs with self.assertRaises(ValueError): find_resource_record(None, client=client) with self.assertRaises(ValueError): find_resource_record('', client=client) # Go right case resource_record = find_resource_record(hostname, client=client) self.assertEqual(resource_record['Name'], hostname) # Test a go wrong case when there's a Route 53 error with patch.object(client, 'list_resource_record_sets') as mocked: mocked.side_effect = RuntimeError("Route 53 Error") with self.assertRaises(Route53Exception): find_resource_record(hostname, client=client) # Test a go wrong case where the returned result is truncated truncated_result = client._resource_records_response(hostname, 1) truncated_result['IsTruncated'] = True with patch.object(client, 'list_resource_record_sets') as mocked: mocked.return_value = truncated_result with self.assertRaises(AssertionError): find_resource_record(hostname, client=client) # Test a go wrong case where there are multiple resource record results multi_result = client._resource_records_response(hostname, 1) multi_result['ResourceRecordSets'] += new_resource_record(hostname, 'whoops') with patch.object(client, 'list_resource_record_sets') as mocked: mocked.return_value = multi_result with self.assertRaises(AssertionError): find_resource_record(hostname, client=client)