def test_get_instance_token(self): token = "1234567890ABCDEFG" auth1 = authinfo.ApiAuth(usertoken=token) auth2 = authinfo.ApiAuth.get_instance(b"MTIzNDU2Nzg5MEFCQ0RFRkc6") self.assertEqual(auth1, auth2, "ApiAuth.get_instance fail for token.") self.assertEqual(auth1.token, auth2.token, "ApiAuth.token fail.")
def test_get_token_property_with_password(self): auth = authinfo.ApiAuth("username", "password") try: token = auth.token self.fail("A 'NotImplementedError' should be raised. Token=%s" % token) except Exception as e: if not isinstance(e, NotImplementedError): self.fail("A 'NotImplementedError' should be raised. Got: %s" % e)
def test_not_implemented_plugin(self): auth = authinfo.ApiAuth("username", "password") hostname = "hostname" plugin = dnsupdater.DnsUpdaterPlugin(auth, hostname) try: plugin.update_dns("10.1.1.1") self.fail("Not implemented plugin should fail: " "'NoneType' object has no attribute 'format'") except AttributeError as e: self.assertEqual(str(e), "'NoneType' object has no attribute " "'format'", "_get_base_url() should return 'NoneType'") except Exception as e: self.fail("_get_base_url() should return 'AttributeError'. " "Got %s:%s" % (type(e).__name__, e))
def execute_update(args): """Execute the update based on command line args and returns a dictionary with 'execution result, ''response code', 'response info' and 'process friendly message'. """ provider_class = getattr(dnsupdater, dnsupdater.AVAILABLE_PLUGINS.get(args.provider)) updater_options = {} process_message = None auth = None if args.store: # --store argument if provider_class.auth_type == 'T': user_arg = args.usertoken or utils.read_input( "Paste your auth token: ") auth = authinfo.ApiAuth(usertoken=user_arg) else: user_arg = args.usertoken or utils.read_input( "Type your username: "******"Type your password: "******"Auth info stored." else: update_ddns = True # informations arguments elif args.usertoken and args.hostname: if provider_class.auth_type == 'T': auth = authinfo.ApiAuth(args.usertoken) else: auth = authinfo.ApiAuth(args.usertoken, args.password) update_ddns = True exec_result = EXECUTION_RESULT_OK elif args.hostname: if authinfo.exists(args.provider, args.config): auth = authinfo.load(args.provider, args.config) update_ddns = True exec_result = EXECUTION_RESULT_OK else: update_ddns = False exec_result = EXECUTION_RESULT_NOK process_message = "No stored auth information found for " \ "provider: '%s'" % args.provider else: # no arguments update_ddns = False exec_result = EXECUTION_RESULT_NOK process_message = "Warning: The hostname to be updated must be " \ "provided.\nUsertoken and password can be either " \ "provided via command line or stored with --store " \ "option.\nExecute noipy --help for more details." if update_ddns and args.provider == 'generic': if args.url: if not URL_RE.match(args.url): process_message = "Malformed URL." exec_result = EXECUTION_RESULT_NOK update_ddns = False else: updater_options['url'] = args.url else: process_message = "Must use --url if --provider is 'generic' " \ "(default)" exec_result = EXECUTION_RESULT_NOK update_ddns = False response_code = None response_text = None if update_ddns: ip_address = args.ip if args.ip else utils.get_ip() if not ip_address: process_message = "Unable to get IP address. Check connection." exec_result = EXECUTION_RESULT_NOK elif ip_address == utils.get_dns_ip(args.hostname): process_message = "No update required." else: updater = provider_class(auth, args.hostname, updater_options) print("Updating hostname '%s' with IP address %s " "[provider: '%s']..." % (args.hostname, ip_address, args.provider)) response_code, response_text = updater.update_dns(ip_address) process_message = updater.status_message proc_result = { 'exec_result': exec_result, 'response_code': response_code, 'response_text': response_text, 'process_message': process_message, } return proc_result
def test_dns_plugin_status_message(self): auth = authinfo.ApiAuth("username", "password") hostname = "hostname" plugin = dnsupdater.DnsUpdaterPlugin(auth, hostname) # badauth code plugin.last_ddns_response = "badauth" expected_message = "ERROR: Invalid username or password (%s)." \ % plugin.last_ddns_response self.assertEqual(plugin.status_message, expected_message, "Expected 'badauth' status code.") # good <IP> code plugin.last_ddns_response = "good 1.1.1.1" expected_message = "SUCCESS: DNS hostname IP (1.1.1.1) successfully " \ "updated." self.assertEqual(plugin.status_message, expected_message, "Expected 'good <1.1.1.1>' status code.") # nochg <IP> code plugin.last_ddns_response = "nochg 1.1.1.1" expected_message = "SUCCESS: IP address (1.1.1.1) is up to date, " \ "nothing was changed. Additional 'nochg' updates " \ "may be considered abusive." self.assertEqual(plugin.status_message, expected_message, "Expected 'nochg <1.1.1.1>' status code.") # !donator code plugin.last_ddns_response = "!donator" expected_message = "ERROR: Update request include a feature that is " \ "not available to informed user." self.assertEqual(plugin.status_message, expected_message, "Expected '!donator' status code.") # notfqdn code plugin.last_ddns_response = "notfqdn" expected_message = "ERROR: The hostname specified is not a " \ "fully-qualified domain name (not in the form " \ "hostname.dyndns.org or domain.com)." self.assertEqual(plugin.status_message, expected_message, "Expected 'notfqdn' status code.") # nohost code plugin.last_ddns_response = "nohost" expected_message = "ERROR: Hostname specified does not exist in this" \ " user account." self.assertEqual(plugin.status_message, expected_message, "Expected 'nohost' status code.") # numhost code plugin.last_ddns_response = "numhost" expected_message = "ERROR: Too many hosts (more than 20) specified " \ "in an update. Also returned if trying to update " \ "a round robin (which is not allowed)." self.assertEqual(plugin.status_message, expected_message, "Expected 'numhost' status code.") # abuse code plugin.last_ddns_response = "abuse" expected_message = "ERROR: Username/hostname is blocked due to " \ "update abuse." self.assertEqual(plugin.status_message, expected_message, "Expected 'abuse' status code.") # badagent code plugin.last_ddns_response = "badagent" expected_message = "ERROR: User agent not sent or HTTP method not " \ "permitted." self.assertEqual(plugin.status_message, expected_message, "Expected 'badagent' status code.") # dnserr code plugin.last_ddns_response = "dnserr" expected_message = "ERROR: DNS error encountered." self.assertEqual(plugin.status_message, expected_message, "Expected 'dnserr' status code.") # 911 code plugin.last_ddns_response = "911" expected_message = "ERROR: Problem on server side. Retry update in a" \ " few minutes." self.assertEqual(plugin.status_message, expected_message, "Expected '911' status code.") # OK code plugin.last_ddns_response = "OK" expected_message = "SUCCESS: DNS hostname successfully updated." self.assertEqual(plugin.status_message, expected_message, "Expected 'OK' status code.") # KO code plugin.last_ddns_response = "KO" expected_message = "ERROR: Hostname and/or token incorrect." self.assertEqual(plugin.status_message, expected_message, "Expected 'KO' status code.") # Unknown code plugin.last_ddns_response = "UNKNOWN_CODE" expected_message = "ERROR: Ooops! Something went wrong !!!" self.assertEqual(plugin.status_message, expected_message, "Expected 'Ooops' warning message.")
def test_get_instance_password(self): auth1 = authinfo.ApiAuth("username", "password") auth2 = authinfo.ApiAuth.get_instance(b"dXNlcm5hbWU6cGFzc3dvcmQ=") self.assertEqual(auth1, auth2, "ApiAuth.get_instance fail for " "password.")