Exemplo n.º 1
0
def test_info(workspace, publ_type, publication):
    ensure_publication(workspace, publ_type, publication)
    wms_url = f"http://localhost:8000/geoserver/{workspace}{settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX}/ows"
    wfs_url = f"http://localhost:8000/geoserver/{workspace}/wfs"
    headers = data.HEADERS.get(data.PUBLICATIONS[(workspace, publ_type, publication)][data.TEST_DATA].get('users_can_write', [None])[0])
    style = data.PUBLICATIONS[(workspace, publ_type, publication)][data.TEST_DATA]['style_type']

    info = process_client.get_workspace_publication(publ_type, workspace, publication, headers=headers)
    with app.app_context():
        info_internal = layer_util.get_layer_info(workspace, publication)
        expected_style_url = url_for('rest_workspace_layer_style.get', workspace=workspace, layername=publication,
                                     internal=False)

    file_type = info_internal['file']['file_type']
    item_keys = get_layer_info_keys(file_type)

    assert set(info.keys()) == item_keys, f'info={info}'
    assert info['wms'].get('url') == wms_url, f'r_json={info}, wms_url={wms_url}'
    assert 'url' in info['wms'], f'info={info}'
    assert info.get('file', dict()).get('file_type') == data.PUBLICATIONS[(workspace, publ_type, publication)][data.TEST_DATA].get('file_type')
    if 'wfs' in info:
        assert info['wfs'].get('url') == wfs_url, f'r_json={info}, wfs_url={wfs_url}'

    assert info_internal['style_type'] == style, f'info_internal={info_internal}'
    assert info['style']['type'] == style, info.get('style')
    external_style_url = info['style']['url']
    assert external_style_url == expected_style_url, (info, external_style_url)
def test_style_correctly_saved(source_style_file_path, layer_suffix,
                               expected_style_file_template,
                               expected_style_type):
    workspace = 'test_style_correctly_saved_workspace'
    layer = 'test_style_correctly_saved_layer' + layer_suffix
    expected_style_file = expected_style_file_template.format(
        workspace=workspace,
        layer=layer) if expected_style_file_template else None
    process_client.publish_workspace_layer(workspace,
                                           layer,
                                           style_file=source_style_file_path)
    assert_style_file(workspace, layer, expected_style_file)
    with app.app_context():
        info = layman_util.get_publication_info(workspace,
                                                process_client.LAYER_TYPE,
                                                layer,
                                                context={
                                                    'keys':
                                                    ['style_type', 'style'],
                                                })
    assert info['style_type'] == expected_style_type

    process_client.delete_workspace_layer(workspace, layer)
    process_client.publish_workspace_layer(workspace, layer)

    with app.app_context():
        info = layer_util.get_layer_info(workspace, layer)
    assert info['style_type'] == 'sld'
    assert info['style']['type'] == 'sld', info.get('style')
    assert info['style']['url'], info.get('style')

    assert_style_file(workspace, layer, None)

    process_client.patch_workspace_layer(workspace,
                                         layer,
                                         style_file=source_style_file_path)
    assert_style_file(workspace, layer, expected_style_file)
    with app.app_context():
        info = layer_util.get_layer_info(workspace, layer)
    assert info['style_type'] == expected_style_type

    process_client.delete_workspace_layer(workspace, layer)
Exemplo n.º 3
0
def patch_layer(workspace, layername, title, description, access_rights=None):
    geoserver_workspace = get_geoserver_workspace(workspace)
    info = layer_util.get_layer_info(workspace, layername, context={'keys': ['style_type'], })
    if info['style_type'] == 'sld':
        gs_util.patch_feature_type(geoserver_workspace, layername, title=title, description=description, auth=settings.LAYMAN_GS_AUTH)
        clear_cache(workspace)

    if access_rights and access_rights.get('read'):
        security_read_roles = gs_common.layman_users_to_geoserver_roles(access_rights['read'])
        gs_util.ensure_layer_security_roles(geoserver_workspace, layername, security_read_roles, 'r', settings.LAYMAN_GS_AUTH)

    if access_rights and access_rights.get('write'):
        security_write_roles = gs_common.layman_users_to_geoserver_roles(access_rights['write'])
        gs_util.ensure_layer_security_roles(geoserver_workspace, layername, security_write_roles, 'w', settings.LAYMAN_GS_AUTH)
Exemplo n.º 4
0
def ensure_wfs_t_attributes(attribs):
    app.logger.info(f"GET WFS check_xml_for_attribute attribs={attribs}")
    editable_attribs = set(attr for attr in attribs
                           if authz.can_i_edit(LAYER_TYPE, attr[0], attr[1]))
    created_attributes = db.ensure_attributes(editable_attribs)
    if created_attributes:
        changed_layers = {(workspace, layer)
                          for workspace, layer, _ in created_attributes}
        qgis_changed_layers = {
            (workspace, layer)
            for workspace, layer in changed_layers
            if layer_util.get_layer_info(
                workspace, layer, context={
                    'keys': ['style_type'],
                })['style_type'] == 'qml'
        }
        for workspace, layer in qgis_changed_layers:
            qgis_wms.save_qgs_file(workspace, layer)
        gs_reset(settings.LAYMAN_GS_AUTH)
Exemplo n.º 5
0
def test_missing_attribute(style_file, ):
    username = '******'
    layername = 'inexisting_attribute_layer'
    layername2 = 'inexisting_attribute_layer2'

    authn_headers = get_authz_headers(username)

    process_client.ensure_reserved_username(username, headers=authn_headers)
    process_client.publish_workspace_layer(
        username,
        layername,
        file_paths=[
            'tmp/naturalearth/110m/cultural/ne_110m_admin_0_countries.geojson',
        ],
        style_file=style_file,
        headers=authn_headers,
    )
    process_client.publish_workspace_layer(
        username,
        layername2,
        file_paths=[
            'tmp/naturalearth/110m/cultural/ne_110m_admin_0_countries.geojson',
        ],
        style_file=style_file,
        headers=authn_headers,
    )

    with app.app_context():
        style_type = layer_util.get_layer_info(username,
                                               layername,
                                               context={
                                                   'keys': ['style_type'],
                                               })['style_type']

    def wfs_post(workspace, attr_names_list, data_xml):
        with app.app_context():
            wfs_url = f"http://{settings.LAYMAN_SERVER_NAME}/geoserver/{workspace}/wfs"
            old_db_attributes = {}
            old_wfs_properties = {}
            for layer, attr_names in attr_names_list:
                # test that all attr_names are not yet presented in DB table
                old_db_attributes[layer] = db.get_all_column_names(
                    workspace, layer)
                for attr_name in attr_names:
                    assert attr_name not in old_db_attributes[
                        layer], f"old_db_attributes={old_db_attributes[layer]}, attr_name={attr_name}"
                layer_schema = get_wfs_schema(wfs_url,
                                              typename=f"{workspace}:{layer}",
                                              version=geoserver_wfs.VERSION,
                                              headers=authn_headers)
                old_wfs_properties[layer] = sorted(
                    layer_schema['properties'].keys())
                if style_type == 'qml':
                    assert qgis_wms.get_layer_info(workspace, layer)
                    old_qgis_attributes = qgis_util.get_layer_attribute_names(
                        workspace, layer)
                    assert all(
                        attr_name not in old_qgis_attributes
                        for attr_name in attr_names), (attr_names,
                                                       old_qgis_attributes)

            process_client.post_wfst(data_xml,
                                     headers=authn_headers,
                                     workspace=username)

            new_db_attributes = {}
            new_wfs_properties = {}
            for layer, attr_names in attr_names_list:
                # test that exactly all attr_names were created in DB table
                new_db_attributes[layer] = db.get_all_column_names(
                    workspace, layer)
                for attr_name in attr_names:
                    assert attr_name in new_db_attributes[
                        layer], f"new_db_attributes={new_db_attributes[layer]}, attr_name={attr_name}"
                assert set(attr_names).union(set(
                    old_db_attributes[layer])) == set(new_db_attributes[layer])

                # test that exactly all attr_names were distinguished also in WFS feature type
                layer_schema = get_wfs_schema(wfs_url,
                                              typename=f"{workspace}:{layer}",
                                              version=geoserver_wfs.VERSION,
                                              headers=authn_headers)
                new_wfs_properties[layer] = sorted(
                    layer_schema['properties'].keys())
                for attr_name in attr_names:
                    assert attr_name in new_wfs_properties[
                        layer], f"new_wfs_properties={new_wfs_properties[layer]}, attr_name={attr_name}"
                assert set(attr_names).union(set(old_wfs_properties[layer])) == set(new_wfs_properties[layer]),\
                    set(new_wfs_properties[layer]).difference(set(attr_names).union(set(old_wfs_properties[layer])))
                if style_type == 'qml':
                    assert qgis_wms.get_layer_info(workspace, layer)
                    new_qgis_attributes = qgis_util.get_layer_attribute_names(
                        workspace, layer)
                    assert all(
                        attr_name in new_qgis_attributes
                        for attr_name in attr_names), (attr_names,
                                                       new_qgis_attributes)
                else:
                    assert not qgis_wms.get_layer_info(workspace, layer)

    attr_names = ['inexisting_attribute_attr', 'inexisting_attribute_attr1a']
    data_xml = data_wfs.get_wfs20_insert_points_new_attr(
        username, layername, attr_names)
    wfs_post(username, [(layername, attr_names)], data_xml)

    attr_names2 = ['inexisting_attribute_attr2']
    data_xml = data_wfs.get_wfs20_update_points_new_attr(
        username, layername, attr_names2)
    wfs_post(username, [(layername, attr_names2)], data_xml)

    attr_names3 = ['inexisting_attribute_attr3']
    data_xml = data_wfs.get_wfs20_update_points_new_attr(
        username, layername, attr_names3, with_attr_namespace=True)
    wfs_post(username, [(layername, attr_names3)], data_xml)

    attr_names4 = ['inexisting_attribute_attr4']
    data_xml = data_wfs.get_wfs20_update_points_new_attr(username,
                                                         layername,
                                                         attr_names4,
                                                         with_filter=True)
    wfs_post(username, [(layername, attr_names4)], data_xml)

    attr_names5 = ['inexisting_attribute_attr5']
    data_xml = data_wfs.get_wfs20_replace_points_new_attr(
        username, layername, attr_names5)
    wfs_post(username, [(layername, attr_names5)], data_xml)

    attr_names_i1 = ['inexisting_attribute_attr_complex_i1']
    attr_names_i2 = ['inexisting_attribute_attr_complex_i2']
    attr_names_u = ['inexisting_attribute_attr_complex_u']
    attr_names_r = ['inexisting_attribute_attr_complex_r']
    attr_names_complex = [(layername, attr_names_i1 + attr_names_r),
                          (layername2, attr_names_i2 + attr_names_u)]
    data_xml = data_wfs.get_wfs20_complex_new_attr(
        workspace=username,
        layername1=layername,
        layername2=layername2,
        attr_names_insert1=attr_names_i1,
        attr_names_insert2=attr_names_i2,
        attr_names_update=attr_names_u,
        attr_names_replace=attr_names_r)
    wfs_post(username, attr_names_complex, data_xml)

    attr_names6 = ['inexisting_attribute_attr6']
    data_xml = data_wfs.get_wfs10_insert_points_new_attr(
        username, layername, attr_names6)
    wfs_post(username, [(layername, attr_names6)], data_xml)

    attr_names7 = ['inexisting_attribute_attr7']
    data_xml = data_wfs.get_wfs11_insert_points_new_attr(
        username, layername, attr_names7)
    wfs_post(username, [(layername, attr_names7)], data_xml)

    attr_names8 = ['inexisting_attribute_attr8']
    data_xml = data_wfs.get_wfs10_update_points_new(username,
                                                    layername,
                                                    attr_names8,
                                                    with_attr_namespace=True)
    wfs_post(username, [(layername, attr_names8)], data_xml)

    attr_names9 = ['inexisting_attribute_attr9']
    data_xml = data_wfs.get_wfs10_update_points_new(username,
                                                    layername,
                                                    attr_names9,
                                                    with_filter=True)
    wfs_post(username, [(layername, attr_names9)], data_xml)

    attr_names10 = ['inexisting_attribute_attr10']
    data_xml = data_wfs.get_wfs11_insert_polygon_new_attr(
        username, layername, attr_names10)
    wfs_post(username, [(layername, attr_names10)], data_xml)

    process_client.delete_workspace_layer(username,
                                          layername,
                                          headers=authn_headers)
    process_client.delete_workspace_layer(username,
                                          layername2,
                                          headers=authn_headers)
Exemplo n.º 6
0
def test_update_style_type_in_db():
    workspace = 'test_update_style_type_in_db_workspace'
    map = 'test_update_style_type_in_db_map'
    TestLayerDef = namedtuple('TestLayerDef', [
        'name',
        'style_file',
        'expected_style',
    ])
    layers = [
        TestLayerDef(
            'test_update_style_type_in_db_none_layer',
            '',
            'sld',
        ),
        TestLayerDef(
            'test_update_style_type_in_db_sld_layer',
            'sample/style/generic-blue_sld.xml',
            'sld',
        ),
        TestLayerDef(
            'test_update_style_type_in_db_sld110_layer',
            'sample/style/sld_1_1_0.xml',
            'sld',
        ),
        # This should not happened, because before this release, it was not possible to upload QGIS files
        TestLayerDef(
            'test_update_style_type_in_db_qgis_layer',
            'sample/style/small_layer.qml',
            'sld',
        ),
    ]

    process_client.publish_workspace_map(workspace, map)
    for layer in layers:
        process_client.publish_workspace_layer(
            workspace,
            layer.name,
            style_file=layer.style_file,
        )

    set_column_null = f"""update {DB_SCHEMA}.publications set style_type = null"""

    with app.app_context():
        db_util.run_statement(set_column_null)
        map_info = map_util.get_map_info(workspace, map)
        assert map_info['style_type'] is None
        for layer in layers:
            layer_info = layer_util.get_layer_info(workspace, layer.name)
            assert layer_info['style_type'] is None

        upgrade_v1_10.update_style_type_in_db()

        map_info = map_util.get_map_info(workspace, map)
        assert map_info['style_type'] is None
        for layer in layers:
            layer_info = layer_util.get_layer_info(workspace, layer.name)
            assert layer_info['style_type'] == layer.expected_style

    process_client.delete_workspace_map(workspace, map)
    for layer in layers:
        process_client.delete_workspace_layer(
            workspace,
            layer.name,
        )