Example #1
0
    def test_invalid_hostnames(self):
        "Verify is_valid_hostname detects invalid hostnames"

        hostnames = [
            '-no-starting-dash.com', '.localhost', 'localhost.no-end-dash-'
        ]
        for hostname in hostnames:
            assert not utils.is_valid_hostname(hostname)
Example #2
0
    def test_valid_hostnames(self):
        "Verify is_valid_hostname"

        hostnames = [
            'battlemidget.lol', 'www.battlemidget.lol', 'localhost',
            'localhost.localdomain', '_underscores-is_ok', 'double--dash_is_ok'
        ]
        for hostname in hostnames:
            assert utils.is_valid_hostname(hostname)
Example #3
0
 def _has_correct_endpoint(self):
     """ Validates that a ip address or url is passed.
     If url, check to make sure it ends in the /MAAS endpoint
     """
     field = self.form.field('endpoint')
     endpoint = field.value
     # Is URL?
     if endpoint.startswith('http'):
         url = urlparse(endpoint)
         if not url.netloc:
             return (False,
                     "Unable to determine the web address, "
                     "please use the format of "
                     "http://maas-server.com:5240/MAAS")
         else:
             if 'MAAS' not in url.path:
                 field.value = urljoin(url.geturl(), "MAAS")
             return (True, None)
     elif is_valid_hostname(endpoint):
         # Looks like we just have a domain name
         field.value = urljoin("http://{}:5240".format(endpoint),
                               "MAAS")
         return (True, None)
     else:
         try:
             # Check if valid IPv4 address, add default scheme, api
             # endpoint
             ip = endpoint.split(':')
             port = '5240'
             if len(ip) == 2:
                 ip, port = ip
             else:
                 ip = ip.pop()
             ipaddress.ip_address(ip)
             field.value = urljoin(
                 "http://{}:{}".format(ip, port), "MAAS")
             return (True, None)
         except ValueError:
             # Pass through to end so we can let the user know to use the
             # proper http://maas-server.com/MAAS url
             pass
     return (False,
             "Unable to validate that this entry is "
             "the correct format. Please use  the format of "
             "http://maas-server.com:5240/MAAS")