From 44bd0dc869accdd0c31e12015f0fcf8c6ad9ae45 Mon Sep 17 00:00:00 2001 From: Ronnie Dutta <61982285+MetRonnie@users.noreply.github.com> Date: Thu, 3 Mar 2022 17:50:18 +0000 Subject: [PATCH] Allow `~user` in universal ID if it's the current user --- cylc/flow/id_cli.py | 6 ++++-- tests/unit/test_id_cli.py | 13 +++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cylc/flow/id_cli.py b/cylc/flow/id_cli.py index f7281cd3f2c..9bdedf937be 100644 --- a/cylc/flow/id_cli.py +++ b/cylc/flow/id_cli.py @@ -26,6 +26,7 @@ UserInputError, WorkflowFilesError, ) +from cylc.flow.hostuserutil import get_user from cylc.flow.id import ( Tokens, contains_multiple_workflows, @@ -47,6 +48,7 @@ validate_workflow_name, ) + FN_CHARS = re.compile(r'[\*\?\[\]\!]') @@ -380,9 +382,9 @@ def _validate_constraint(*tokens_list, constraint=None): def _validate_workflow_ids(*tokens_list, src_path): for ind, tokens in enumerate(tokens_list): - if tokens['user']: + if tokens['user'] and (tokens['user'] != get_user()): raise UserInputError( - "Operating on others users' workflows is not supported" + "Operating on other users' workflows is not supported" ) if not src_path: validate_workflow_name(tokens['workflow']) diff --git a/tests/unit/test_id_cli.py b/tests/unit/test_id_cli.py index 91fbe55c64c..0e95763704e 100644 --- a/tests/unit/test_id_cli.py +++ b/tests/unit/test_id_cli.py @@ -370,12 +370,15 @@ async def test_parse_ids_src_path(src_dir): ), ( ['~alice/foo'], - "Operating on others users' workflows is not supported", + "Operating on other users' workflows is not supported", ), ] ) -async def test_parse_ids_invalid_ids(ids_in, error_msg): +async def test_parse_ids_invalid_ids( + ids_in, error_msg, monkeypatch: pytest.MonkeyPatch +): """It should error for invalid IDs.""" + monkeypatch.setattr('cylc.flow.id_cli.get_user', lambda: 'rincewind') with pytest.raises(Exception) as exc_ctx: await parse_ids_async( *ids_in, @@ -384,6 +387,12 @@ async def test_parse_ids_invalid_ids(ids_in, error_msg): assert error_msg in str(exc_ctx.value) +async def test_parse_ids_current_user(monkeypatch: pytest.MonkeyPatch): + """It should work if the user in the ID is the current user.""" + monkeypatch.setattr('cylc.flow.id_cli.get_user', lambda: 'rincewind') + await parse_ids_async('~rincewind/luggage', constraint='workflows') + + async def test_parse_ids_file(tmp_run_dir): """It should reject IDs that are paths to files.""" tmp_path = tmp_run_dir('x')