Ejemplo n.º 1
0
def test_add_host(client):
    """
    There are two ways to add a host, one by specifying the exact IP for the
    host to use, two by specifying the subnet for the host to be on and allowing
    DDI to find a free IP on the subnet and assigning it to the host.
    """
    recorder = Betamax(client)

    with recorder.use_cassette('ddi_add_host'):
        ip_result = add_host(building='TEST',
                             department='TEST',
                             contact='Test User',
                             ip='172.23.23.4',
                             phone='555-1212',
                             name=ddi_host,
                             session=client,
                             url=ddi_url,
                             comment='Test Comment')
        subnet_result = add_host(building='TEST',
                                 department='TEST',
                                 contact='Test User',
                                 subnet='172.23.23.0',
                                 phone='555-1212',
                                 name=ddi_host2,
                                 session=client,
                                 url=ddi_url,
                                 comment='Test Comment')

    assert isinstance(ip_result, dict)
    assert jsend.is_success(ip_result)

    assert isinstance(subnet_result, dict)
    assert jsend.is_success(subnet_result)
Ejemplo n.º 2
0
    def test_jsend_to_stringify_f(self):
        ret_json_string = jsend.success().stringify()
        try:
            jsend.is_success(ret_json_string)
        except TypeError:
            return

        self.fail()
Ejemplo n.º 3
0
def add_cname(cname: str, host: str, session: object, url: str):
    """
    Add a cname to a given host.

    :param str cname: The CNAME to add.
    :param str host: The host FQDN.
    :param object session: The requests session object.
    :param str url: The full URL of the DDI server.
    :return: The response as JSON
    :rtype: list

    """
    logger.debug('Add CNAME: %s called on host: %s', cname, host)

    host_data = get_host(host, session, url)

    if jsend.is_success(host_data):
        entry = host_data['data']['results'][0]

        payload = {'ip_id': entry['ip_id'], 'ip_name': cname}
        r = session.put(url + 'rest/ip_alias_add', json=payload)

        result = get_exceptions(r)

        logger.debug('Add cname result code: %s, JSON: %s', r.status_code, result)

        return result
    else:
        return host_data
Ejemplo n.º 4
0
def get_free_ipv4(subnet: str, session: object, url: str):
    """
    Get a free IP address in a given subnet ID.
    :param str subnet: The subnet ID to get the free IP for (e.g. 172.23.23.0).
    :param object session: the requests session object
    :param url: The full URL of the DDI server.
    :return: The JSON response in JSEND format.
    :rtype: dict
    """
    logger.debug('Getting free IP for subnet: %s', subnet)

    r = get_subnet_info(subnet, session, url)

    if jsend.is_success(r):
        subnet_id = r['data']['results'][0]['subnet_id']

        payload = {'subnet_id': subnet_id}

        r = session.get(url + '/rpc/ip_find_free_address', params=payload)

        result = get_exceptions(r)

        return result

    else:
        logger.debug('Failed: Getting free IP for subnet: %s', subnet)
        return r
Ejemplo n.º 5
0
def delete_host(fqdn: str, session: object, url: str):
    """
    Delete a given host by ip_id.

    :param str fqdn: The FQDN of the host object to delete.
    :param object session: The requests session object.
    :param str url: The URL of the DDI server.
    :return: The JSON result of the operation.
    :rtype: str
    """

    h = get_host(fqdn, session, url)

    if jsend.is_success(h):
        ip_id = h['data']['results'][0]['ip_id']

        logger.debug('Deleting host: %s with ip_id: %s', fqdn, ip_id)

        payload = {'ip_id': ip_id}
        r = session.delete(url + 'rest/ip_delete', params=payload)

        result = get_exceptions(r)

        return result
    else:
        return h
def test_get_fields_by_address_name_spellings(client):
    """
        Test get_fields_by_address
        with Streets with contentious spellings
        e.g. Bay Shore Blvd or Bayshore Blvd
        EAS says Bay Shore, the street signs say Bayshore and people use both.
    """
    address = "99 bay shore blvd"

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert len(content['data']['parcels']) > 0

    address = "99 bayshore blvd"

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content_oneword = json.loads(response.content)

    assert content_oneword == content
Ejemplo n.º 7
0
def test_delete_cname(client):
    recorder = Betamax(client)
    with recorder.use_cassette('ddi_delete_cname'):
        result = delete_cname(cname=ddi_cname, session=client, url=ddi_url)

    assert isinstance(result, dict)
    assert jsend.is_success(result)
Ejemplo n.º 8
0
def add(ctx, building, comment, contact, department, ip, phone, subnet, host):
    """
    Add a single host entry into DDI. Specify the subnet (-s) using the
    subnet ID (e.g. 172.23.23.0) to automatically receive a free ip, otherwise
    specify the exact IP to use.
    """

    logger.debug('Add operation called for host: %s at ip %s', host, ip)

    r = add_host(building,
                 department,
                 contact,
                 phone,
                 host,
                 ctx.obj['session'],
                 ctx.obj['url'],
                 comment=comment,
                 ip=ip,
                 subnet=subnet)

    if ctx.obj['json']:
        click.echo(json.dumps(r, indent=2, sort_keys=True))
    elif jsend.is_success(r):
        click.echo(f'Host: {host} added.')
    else:
        click.echo(f'Host {host} addition failed.')
        ctx.exit(1)
Ejemplo n.º 9
0
def delete_cname(cname: str, session: object, url: str):
    """
    Delete a CNAME from a host.

    :param str cname: The CNAME to add.
    :param str host: The host FQDN.
    :param object session: The requests session object.
    :param str url: The full URL of the DDI server.
    :return: The response as JSON
    :rtype: list
    """
    logger.debug('Delete cname: %s called.', cname)

    host_data = get_cname_info(cname, session, url)

    if jsend.is_success(host_data):
        entry = host_data['data']['results'][0]

        payload = {'ip_id': entry['ip_id'], 'ip_name': cname}

        r = session.delete(url + 'rest/ip_alias_delete', json=payload)

        result = get_exceptions(r)

        logger.debug('Delete cname result code: %s, JSON: %s', r.status_code, result)

        return result
    else:
        return host_data
def test_get_fields_by_address_numbered_streets(client):
    """
        Test get_fields_by_address with
        * The potential for leading zeros in the numbered streets and avenues
            e.g. 01st St versus 1st St e.g. 101 1st
        * Numbered streets being spelled out – First St, etc.
    """
    address = '101 01st'

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert len(content['data']['parcels']) > 0

    # The potential for leading zeros in the numbered streets and avenues
    # e.g. 01st St versus 1st St e.g. 101 1st
    address = '101 1st'

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content_nonzero = json.loads(response.content)

    assert content_nonzero == content

    # Numbered streets being spelled out – First St, etc.
    address = '101 First'

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content_spelled = json.loads(response.content)

    assert content_spelled == content

    address = '111 Tenth'

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert len(content['data']['parcels']) > 0
def test_arcgis_welcome(client):
    """Test arcgis welcome"""
    response = client.simulate_get('/arcgis/welcome')
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
Ejemplo n.º 12
0
def test_get_cname(client):
    recorder = Betamax(client)
    with recorder.use_cassette('ddi_get_cname'):
        result = get_cname_info(cname=ddi_cname, session=client, url=ddi_url)

    assert isinstance(result, dict)
    assert jsend.is_success(result)
    assert result['data']['results'][0]['name'] == ddi_host
def test_get_permit_list_legacy(client):
    """Test get_permit_list legacy format"""
    response = client.simulate_get('/list/retail_legacy')
    assert response.status_code == 200

    response_json = json.loads(response.content)
    assert jsend.is_success(response_json)
    assert isinstance(response_json['data'], dict)
Ejemplo n.º 14
0
def test_records_index_default(client):
    """Test records index (default state) """
    response = client.simulate_get('/records/')
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert content['data']['message'] == falcon.HTTP_200
Ejemplo n.º 15
0
def test_delete_host(client):
    recorder = Betamax(client)

    with recorder.use_cassette('ddi_delete_host'):
        result = delete_host(fqdn=ddi_host, session=client, url=ddi_url)

    assert isinstance(result, dict)
    assert jsend.is_success(result)
    assert 'ret_oid' in result['data']['results'][0]
Ejemplo n.º 16
0
def test_record_article_11(client):
    """ Test record article_11 field
    """
    response = client.simulate_get('/records/0350004',
                                   params={'fields': 'article_11'})
    assert response.status_code == 200

    content = json.loads(response.content)
    assert jsend.is_success(content)
    assert content['data']['article_11']
Ejemplo n.º 17
0
def test_record_ceqacode(client):
    """ Test record ceqacode field
    """
    response = client.simulate_get('/records/1279021',
                                   params={'fields': 'ceqacode'})
    assert response.status_code == 200

    content = json.loads(response.content)
    assert jsend.is_success(content)
    assert content['data']['ceqacode']
Ejemplo n.º 18
0
def test_record_building_area(client):
    """ Test record building_area field
    """
    response = client.simulate_get('/records/3512008',
                                   params={'fields': 'building_area'})
    assert response.status_code == 200

    content = json.loads(response.content)
    assert jsend.is_success(content)
    assert 'building_area' in content['data']
def test_get_fields_by_address_no_result_example(client):
    """Test get_fields_by_address with base address suggestion"""
    address = '1650 mission street #100'

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert len(content['data']['parcels']) == 0
Ejemplo n.º 20
0
def test_record_empty(client):
    """ Test empty record
    """
    response = client.simulate_get('/records/123',
                                   params={'fields': 'year_built'})
    assert response.status_code == 200

    content = json.loads(response.content)
    assert jsend.is_success(content)
    assert 'year_built' in content['data']
    assert not content['data']['year_built']
Ejemplo n.º 21
0
def test_record_year_built(client):
    """ Test record year_built field
    """
    parcel = '1279021'
    response = client.simulate_get('/records/' + parcel,
                                   params={'fields': 'year_built'})
    assert response.status_code == 200

    content = json.loads(response.content)
    assert jsend.is_success(content)
    assert content['data']['year_built']
Ejemplo n.º 22
0
def test_decode():
    cli = app.test_client()
    src = 'aGVsbG8gd29ybGQ='
    r = cli.post('/api/decode',
                 headers={'X-Base64-Access-Key': 'helloworld'},
                 content_type='application/json',
                 data=json.dumps({'src': src}))
    assert 200 == r.status_code
    ret = r.get_json()
    assert jsend.is_success(ret)
    assert ret['data']['result'] == 'hello world'
def test_arcgis_parcels_default(client):
    """Test arcgis parcels
        default state
    """
    response = client.simulate_get('/arcgis/parcels')
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert content['data']['message'] == 'parcels'
Ejemplo n.º 24
0
def add(ctx, host, cname):
    """Add a single CNAME entry to an existing host."""

    r = add_cname(cname, host, ctx.obj['session'], ctx.obj['url'])

    if ctx.obj['json']:
        click.echo(json.dumps(r, indent=2, sort_keys=True))
    elif jsend.is_success(r):
        click.echo(f'CNAME: {cname} added to host: {host}.')
    else:
        click.echo(f'CNAME: {cname} addition to host {host} failed.')
        ctx.exit(1)
Ejemplo n.º 25
0
def test_record_combo(client):
    """ Test record retrieval for multiple fields """
    response = client.simulate_get(
        '/records/3512008',
        params={'fields': 'year_built,building_area,ceqacode'})
    assert response.status_code == 200

    content = json.loads(response.content)
    assert jsend.is_success(content)
    assert 'year_built' in content['data']
    assert 'building_area' in content['data']
    assert 'ceqacode' in content['data']
def test_arcgis_parcels_empty(client):
    """Test arcgis parcels
        empty state
    """
    response = client.simulate_get('/arcgis/parcels',
                                   params={'address': '1234'})
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert content['data']['parcels'] == {}
Ejemplo n.º 27
0
def delete(ctx, cname):
    """Delete a single CNAME entry for a host."""

    r = delete_cname(cname, ctx.obj['session'], ctx.obj['url'])

    if ctx.obj['json']:
        click.echo(json.dumps(r, indent=2, sort_keys=True))
    elif jsend.is_success(r):
        click.echo(f'CNAME: {cname} deleted.')
    else:
        click.echo(f'CNAME delete failed for: {cname}')
        ctx.exit(1)
def test_get_fields_by_address_unit_number(client):
    """
        Test get_fields_by_address with number sign(#) as unit type
    """
    address = '77 van ness ave #100'

    response = get_parcels_by_address_response(client, address)
    assert response.status_code == 200

    content = json.loads(response.content)

    assert jsend.is_success(content)
    assert len(content['data']['parcels']) == 1
Ejemplo n.º 29
0
def info(ctx, ips):
    """Provide information on the given IPv4 address(es)."""

    logger.debug('Info operation called on IPs: %s.', ips)
    for ip in ips:
        r = get_ipv4_info(ip, ctx.obj['session'], ctx.obj['url'])
        if ctx.obj['json']:
            click.echo(json.dumps(r, indent=2, sort_keys=True))
        elif jsend.is_success(r):
            echo_host_info(r)
        else:
            click.echo('Request failed, enable debugging for more.')
            ctx.exit(1)
Ejemplo n.º 30
0
def test_get_subnet_info(client):
    recorder = Betamax(client)

    with recorder.use_cassette('ddi_get_subnet'):
        result = get_subnet_info(subnet=subnet, session=client, url=ddi_url)
        failed_result = get_subnet_info(subnet=errant_subnet, session=client, url=ddi_url)

    assert isinstance(result, dict)
    assert jsend.is_success(result)
    assert 'subnet_id' in result['data']['results'][0]

    assert isinstance(failed_result, dict)
    assert jsend.is_fail(failed_result)