Skip to content

Commit

Permalink
Check, that no user is named workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Mar 2, 2021
1 parent 60f610e commit b4e2841
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/layman/upgrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
upgrade_v1_9.geoserver_everyone_rights_repair,
upgrade_v1_9.geoserver_remove_users_for_public_workspaces]),
((1, 10, 0), [upgrade_v1_10.alter_schema,
upgrade_v1_10.check_usernames_for_wms_suffix,
upgrade_v1_10.check_workspace_names,
upgrade_v1_10.migrate_layers_to_wms_workspace,
upgrade_v1_10.migrate_maps_on_wms_workspace,
upgrade_v1_10.migrate_metadata_records,
Expand Down
20 changes: 15 additions & 5 deletions src/layman/upgrade/upgrade_v1_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ def alter_schema():
db_schema = settings.LAYMAN_PRIME_SCHEMA
add_column = f'''
DO $$ BEGIN
CREATE TYPE enum_style_type AS ENUM ('sld', 'qml');
CREATE TYPE {db_schema}.enum_style_type AS ENUM ('sld', 'qml');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
ALTER TABLE {db_schema}.publications ADD COLUMN IF NOT EXISTS
style_type enum_style_type;'''
style_type {db_schema}.enum_style_type;'''
db_util.run_statement(add_column)
logger.info(f' DONE - alter DB prime schema')


def check_usernames_for_wms_suffix():
logger.info(f' Starting - checking users with {settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX} suffix')
def check_workspace_names():
logger.info(f' Starting - checking workspace names - for {settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX} suffix, '
f'for `{settings.REST_WORKSPACES_PREFIX}` name')
workspaces = prime_db_schema.get_workspaces()
for workspace in workspaces:
if workspace.endswith(settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX):
Expand All @@ -51,7 +52,16 @@ def check_usernames_for_wms_suffix():
data={'workspace': workspace,
}
)
logger.info(f' DONE - checking users with {settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX} suffix')
if settings.REST_WORKSPACES_PREFIX in workspaces:
raise LaymanError(f"A workspace has reserved name '{settings.REST_WORKSPACES_PREFIX}'. "
f"In that case, please downgrade to the previous minor release version of Layman and contact Layman "
f"contributors. One way how to do that is to create an issue in Layman repository: "
f"https://github.com/jirik/layman/issues/",
data={'workspace': settings.REST_WORKSPACES_PREFIX
}
)
logger.info(f' DONE - checking workspace names - for {settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX} suffix, '
f'for `{settings.REST_WORKSPACES_PREFIX}` name')


def migrate_layers_to_wms_workspace(workspace=None):
Expand Down
24 changes: 19 additions & 5 deletions src/layman/upgrade/upgrade_v1_10_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,32 @@ def publications_constraint():


@pytest.mark.usefixtures('ensure_layman')
def test_check_usernames_for_wms_suffix():
username = 'test_check_usernames_for_wms_suffix'
username_wms = 'test_check_usernames_for_wms_suffix' + settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX
def test_check_usernames_wms_suffix():
username = 'test_check_usernames_wms_suffix'
username_wms = 'test_check_usernames_wms_suffix' + settings.LAYMAN_GS_WMS_WORKSPACE_POSTFIX

with app.app_context():
prime_db_schema.ensure_workspace(username)
upgrade_v1_10.check_usernames_for_wms_suffix()
upgrade_v1_10.check_workspace_names()

prime_db_schema.ensure_workspace(username_wms)
with pytest.raises(LaymanError) as exc_info:
upgrade_v1_10.check_usernames_for_wms_suffix()
upgrade_v1_10.check_workspace_names()
assert exc_info.value.data['workspace'] == username_wms
prime_db_schema.delete_workspace(username)
prime_db_schema.delete_workspace(username_wms)


@pytest.mark.usefixtures('ensure_layman')
def test_check_usernames_workspaces():
username = 'workspaces'

with app.app_context():
prime_db_schema.ensure_workspace(username)
with pytest.raises(LaymanError) as exc_info:
upgrade_v1_10.check_workspace_names()
assert exc_info.value.data['workspace'] == username
prime_db_schema.delete_workspace(username)


@pytest.fixture()
Expand Down

0 comments on commit b4e2841

Please sign in to comment.