def test_region_add_product(client, db_session_mock):
    model.Region.query.get.return_value = model.Region(
        id=1,
        name='test',
        location='RDU',
        description='',
        banner='',
        enabled=True,
        user_quota_id=None,
        total_quota_id=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=7,
        owner_group='00000000-0000-0000-0000-000000000000',
        users_group=None,
        tower_id=1,
        openstack_url='https://openstack.example.com:13000',
        openstack_credentials='kv/example/openstack',
        openstack_project='rhub',
        openstack_domain_name='Default',
        openstack_domain_id='default',
        openstack_networks=['provider_net_rhub'],
        openstack_keyname='rhub_key',
        satellite_hostname='satellite.example.com',
        satellite_insecure=False,
        satellite_credentials='kv/example/satellite',
        dns_server_hostname='ns.example.com',
        dns_server_zone='example.com.',
        dns_server_key='kv/example/key',
        vault_server='https://vault.example.com/',
        download_server='https://download.example.com',
        products_relation=[],
    )

    model.Product.query.get.return_value = model.Product(
        id=10,
        name='dummy',
        description='dummy',
        tower_template_name_create='dummy',
        tower_template_name_delete='dummy',
        parameters=[],
    )

    model.RegionProduct.query.filter.return_value.count.return_value = 0

    rv = client.post(
        f'{API_BASE}/lab/region/1/products',
        headers={'Authorization': 'Bearer foobar'},
        json={'id': 10},
    )

    assert rv.status_code == 204

    db_session_mock.add.assert_called()
    db_session_mock.commit.assert_called()

    region_product = db_session_mock.add.call_args.args[0]
    assert region_product.region_id == 1
    assert region_product.product_id == 10
    assert region_product.enabled is True
Exemple #2
0
def _create_test_region():
    return model.Region(
        id=1,
        name='test',
        location='RDU',
        description='',
        banner='',
        enabled=True,
        user_quota_id=None,
        total_quota_id=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=None,
        owner_group='00000000-0000-0000-0000-000000000000',
        users_group=None,
        tower_id=1,
        openstack_url='https://openstack.example.com:13000',
        openstack_credentials='kv/example/openstack',
        openstack_project='rhub',
        openstack_domain_name='Default',
        openstack_domain_id='default',
        openstack_networks=['provider_net_rhub'],
        openstack_keyname='rhub_key',
        satellite_hostname='satellite.example.com',
        satellite_insecure=False,
        satellite_credentials='kv/example/satellite',
        dns_server_hostname='ns.example.com',
        dns_server_zone='example.com.',
        dns_server_key='kv/example/key',
        vault_server='https://vault.example.com/',
        download_server='https://download.example.com',
    )
def test_update_region(client):
    region = model.Region(
        id=1,
        name='test',
        location='RDU',
        description='',
        banner='',
        enabled=True,
        user_quota_id=None,
        total_quota_id=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=7,
        owner_group='00000000-0000-0000-0000-000000000000',
        users_group=None,
        tower_id=1,
        openstack_url='https://openstack.example.com:13000',
        openstack_credentials='kv/example/openstack',
        openstack_project='rhub',
        openstack_domain_name='Default',
        openstack_domain_id='default',
        openstack_networks=['provider_net_rhub'],
        openstack_keyname='rhub_key',
        satellite_hostname='satellite.example.com',
        satellite_insecure=False,
        satellite_credentials='kv/example/satellite',
        dns_server_hostname='ns.example.com',
        dns_server_zone='example.com.',
        dns_server_key='kv/example/key',
        vault_server='https://vault.example.com/',
        download_server='https://download.example.com',
    )
    model.Region.query.get.return_value = region

    rv = client.patch(
        f'{API_BASE}/lab/region/1',
        headers={'Authorization': 'Bearer foobar'},
        json={
            'name': 'new',
            'description': 'new desc',
        },
    )

    model.Region.query.get.assert_called_with(1)

    assert region.name == 'new'
    assert region.description == 'new desc'

    assert rv.status_code == 200
def test_delete_region(client, keycloak_mock, db_session_mock):
    group_id = '00000000-0000-0000-0000-000000000000'
    region = model.Region(
        id=1,
        name='test',
        location='RDU',
        description='',
        banner='',
        enabled=True,
        user_quota_id=None,
        total_quota_id=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=7,
        owner_group=group_id,
        users_group=None,
        tower_id=1,
    )
    model.Region.query.get.return_value = region
    model.RegionProduct.query.filter.return_value.count.return_value = 0

    keycloak_mock.group_get.return_value = {
        'id': group_id,
        'name': 'test-owners',
    }
    keycloak_mock.group_delete.return_value = group_id

    rv = client.delete(
        f'{API_BASE}/lab/region/1',
        headers={'Authorization': 'Bearer foobar'},
    )

    model.Region.query.get.assert_called_with(1)
    db_session_mock.delete.assert_called_with(region)

    keycloak_mock.group_delete.assert_called_with(group_id)

    assert rv.status_code == 204
def test_region_list_products(client):
    products_relation = [
        model.RegionProduct(
            region_id=1,
            product_id=1,
            product=model.Product(
                id=1,
                name='dummy',
                description='dummy',
                enabled=True,
                tower_template_name_create='dummy',
                tower_template_name_delete='dummy',
                parameters=[],
                flavors={},
            ),
            enabled=True,
        ),
    ]

    model.Region.query.get.return_value = model.Region(
        id=1,
        name='test',
        location='RDU',
        description='',
        banner='',
        enabled=True,
        user_quota_id=None,
        total_quota_id=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=7,
        owner_group='00000000-0000-0000-0000-000000000000',
        users_group=None,
        tower_id=1,
        openstack_url='https://openstack.example.com:13000',
        openstack_credentials='kv/example/openstack',
        openstack_project='rhub',
        openstack_domain_name='Default',
        openstack_domain_id='default',
        openstack_networks=['provider_net_rhub'],
        openstack_keyname='rhub_key',
        satellite_hostname='satellite.example.com',
        satellite_insecure=False,
        satellite_credentials='kv/example/satellite',
        dns_server_hostname='ns.example.com',
        dns_server_zone='example.com.',
        dns_server_key='kv/example/key',
        vault_server='https://vault.example.com/',
        download_server='https://download.example.com',
        products_relation=products_relation)

    rv = client.get(
        f'{API_BASE}/lab/region/1/products',
        headers={'Authorization': 'Bearer foobar'},
    )

    assert rv.status_code == 200
    assert rv.json == [
        {
            'id': 1,
            'region_id': 1,
            'product_id': 1,
            'product': {
                'id': 1,
                'name': 'dummy',
                'description': 'dummy',
                'enabled': True,
                'tower_template_name_create': 'dummy',
                'tower_template_name_delete': 'dummy',
                'parameters': [],
                'flavors': {},
            },
            'enabled': True,
            '_href': ANY,
        },
    ]
def test_update_region_credentials(client, vault_mock):
    region = model.Region(
        id=1,
        name='test',
        location='RDU',
        description='',
        banner='',
        enabled=True,
        user_quota_id=None,
        total_quota_id=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=7,
        owner_group='00000000-0000-0000-0000-000000000000',
        users_group=None,
        tower_id=1,
        openstack_url='https://openstack.example.com:13000',
        openstack_credentials='kv/example/openstack',
        openstack_project='rhub',
        openstack_domain_name='Default',
        openstack_domain_id='default',
        openstack_networks=['provider_net_rhub'],
        openstack_keyname='rhub_key',
        satellite_hostname='satellite.example.com',
        satellite_insecure=False,
        satellite_credentials='kv/example/satellite',
        dns_server_hostname='ns.example.com',
        dns_server_zone='example.com.',
        dns_server_key='kv/example/key',
        vault_server='https://vault.example.com/',
        download_server='https://download.example.com',
    )
    model.Region.query.get.return_value = region

    rv = client.patch(
        f'{API_BASE}/lab/region/1',
        headers={'Authorization': 'Bearer foobar'},
        json={
            'openstack': {
                'credentials': {
                    'username': '******',
                    'password': '******',
                },
            },
            'satellite': {
                'credentials': {
                    'username': '******',
                    'password': '******',
                },
            },
            'dns_server': {
                'key': {
                    'name': 'new-rndc-key',
                    'secret': 'abc=',
                },
            },
        },
    )

    model.Region.query.get.assert_called_with(1)

    assert rv.status_code == 200

    # Check that path didn't change
    assert region.openstack_credentials == 'kv/example/openstack'
    assert region.satellite_credentials == 'kv/example/satellite'
    assert region.dns_server_key == 'kv/example/key'

    vault_mock.write.assert_any_call(
        'kv/example/openstack',
        {
            'username': '******',
            'password': '******'
        },
    )

    vault_mock.write.assert_any_call(
        'kv/example/satellite',
        {
            'username': '******',
            'password': '******'
        },
    )

    vault_mock.write.assert_any_call(
        'kv/example/key',
        {
            'name': 'new-rndc-key',
            'secret': 'abc='
        },
    )
def test_to_dict(keycloak_mock):
    region = model.Region(
        id=1,
        name='test',
        location='RDU',
        description='desc',
        banner='ban',
        enabled=True,
        user_quota_id=1,
        user_quota=model.Quota(
            num_vcpus=40,
            ram_mb=200000,
            num_volumes=40,
            volumes_gb=540,
        ),
        total_quota_id=None,
        total_quota=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=7,
        owner_group='00000000-0000-0000-0000-000000000000',
        users_group=None,
        tower_id=1,
        openstack_url='https://openstack.example.com:13000',
        openstack_credentials='kv/example/openstack',
        openstack_project='rhub',
        openstack_domain_name='Default',
        openstack_domain_id='default',
        openstack_networks=['provider_net_rhub'],
        openstack_keyname='rhub_key',
        satellite_hostname='satellite.example.com',
        satellite_insecure=False,
        satellite_credentials='kv/example/satellite',
        dns_server_hostname='ns.example.com',
        dns_server_zone='example.com.',
        dns_server_key='kv/example/key',
        vault_server='https://vault.example.com/',
        download_server='https://download.example.com',
    )

    keycloak_mock.group_get.return_value = {'name': 'foobar-group'}

    assert region.to_dict() == {
        'id': 1,
        'name': 'test',
        'location': 'RDU',
        'description': 'desc',
        'banner': 'ban',
        'enabled': True,
        'user_quota': {
            'num_vcpus': 40,
            'ram_mb': 200000,
            'num_volumes': 40,
            'volumes_gb': 540,
        },
        'total_quota': None,
        'lifespan_length': None,
        'reservations_enabled': True,
        'reservation_expiration_max': 7,
        'owner_group': '00000000-0000-0000-0000-000000000000',
        'owner_group_name': 'foobar-group',
        'users_group': None,
        'users_group_name': None,
        'tower_id': 1,
        'openstack': {
            'url': 'https://openstack.example.com:13000',
            'credentials': 'kv/example/openstack',
            'project': 'rhub',
            'domain_name': 'Default',
            'domain_id': 'default',
            'networks': ['provider_net_rhub'],
            'keyname': 'rhub_key',
        },
        'satellite': {
            'hostname': 'satellite.example.com',
            'insecure': False,
            'credentials': 'kv/example/satellite',
        },
        'dns_server': {
            'hostname': 'ns.example.com',
            'zone': 'example.com.',
            'key': 'kv/example/key',
        },
        'vault_server': 'https://vault.example.com/',
        'download_server': 'https://download.example.com',
    }
def test_get_region(client, keycloak_mock):
    model.Region.query.get.return_value = model.Region(
        id=1,
        name='test',
        location='RDU',
        description='',
        banner='',
        enabled=True,
        user_quota_id=None,
        total_quota_id=None,
        lifespan_length=None,
        reservations_enabled=True,
        reservation_expiration_max=7,
        owner_group='00000000-0000-0000-0000-000000000000',
        users_group=None,
        tower_id=1,
        openstack_url='https://openstack.example.com:13000',
        openstack_credentials='kv/example/openstack',
        openstack_project='rhub',
        openstack_domain_name='Default',
        openstack_domain_id='default',
        openstack_networks=['provider_net_rhub'],
        openstack_keyname='rhub_key',
        satellite_hostname='satellite.example.com',
        satellite_insecure=False,
        satellite_credentials='kv/example/satellite',
        dns_server_hostname='ns.example.com',
        dns_server_zone='example.com.',
        dns_server_key='kv/example/key',
        vault_server='https://vault.example.com/',
        download_server='https://download.example.com',
    )

    keycloak_mock.group_get.return_value = {'name': 'foobar-group'}

    rv = client.get(
        f'{API_BASE}/lab/region/1',
        headers={'Authorization': 'Bearer foobar'},
    )

    model.Region.query.get.assert_called_with(1)

    assert rv.status_code == 200
    assert rv.json == {
        'id': 1,
        'name': 'test',
        'location': 'RDU',
        'description': '',
        'banner': '',
        'enabled': True,
        'user_quota': None,
        'total_quota': None,
        'lifespan_length': None,
        'reservations_enabled': True,
        'reservation_expiration_max': 7,
        'owner_group': '00000000-0000-0000-0000-000000000000',
        'owner_group_name': 'foobar-group',
        'users_group': None,
        'users_group_name': None,
        'tower_id': 1,
        'openstack': {
            'url': 'https://openstack.example.com:13000',
            'credentials': 'kv/example/openstack',
            'project': 'rhub',
            'domain_name': 'Default',
            'domain_id': 'default',
            'networks': ['provider_net_rhub'],
            'keyname': 'rhub_key',
        },
        'satellite': {
            'hostname': 'satellite.example.com',
            'insecure': False,
            'credentials': 'kv/example/satellite',
        },
        'dns_server': {
            'hostname': 'ns.example.com',
            'zone': 'example.com.',
            'key': 'kv/example/key',
        },
        'vault_server': 'https://vault.example.com/',
        'download_server': 'https://download.example.com',
        '_href': ANY,
    }