Skip to content

Commit

Permalink
User: Extend cli for 'exists' with recursive flag in order to loop ov…
Browse files Browse the repository at this point in the history
…er schemas

TYPE: Feature
LINK: None
  • Loading branch information
Tschuppi81 authored Apr 16, 2024
1 parent d4483da commit a0a7962
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/onegov/user/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,26 @@ def delete_user(request: 'CoreRequest', app: 'Framework') -> None:
return delete_user


@cli.command(context_settings={'singular': True})
@cli.command(context_settings={'default_selector': '*'})
@click.argument('username')
def exists(username: str) -> 'Callable[[CoreRequest, Framework], None]':
""" Returns 0 if the user exists, 1 if it doesn't. """
@click.option('-r', '--recursive', is_flag=True, default=False)
def exists(username: str, recursive: bool) -> ('Callable[[CoreRequest, '
'Framework], None]'):
""" Returns 0 if the user exists, 1 if it doesn't when recursive equals
to False. If the recursive flag is set, it will loop over all schemas
and print the result for each schema without return value."""

def find_user(request: 'CoreRequest', app: 'Framework') -> None:
users = UserCollection(app.session())

if not users.exists(username):
abort("{} does not exist".format(username))
if users.exists(username):
click.secho(f'{app.schema} {username} exists', fg='green')
else:
click.secho("{} exists".format(username), fg='green')
if recursive:
click.secho(f'{app.schema} {username} does not exist',
fg='yellow')
else:
abort(f'{app.schema} {username} does not exist')

return find_user

Expand Down
83 changes: 83 additions & 0 deletions tests/onegov/user/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_cli(postgres_dsn, session_manager, temporary_directory, redis_url):
}

session_manager.ensure_schema_exists('foo-bar')
# session_manager.ensure_schema_exists('foo-zar')

cfg_path = os.path.join(temporary_directory, 'onegov.yml')

Expand Down Expand Up @@ -374,3 +375,85 @@ def login(username, yubikey=None):
user = session.query(User).filter_by(username=username).one()
assert user.realname == 'Jane Doe'
assert user.phone_number == '0411234567'


def test_cli_exists_recursive(postgres_dsn, session_manager,
temporary_directory, redis_url):

cfg = {
'applications': [
{
'path': '/foo/*',
'application': 'onegov.core.Framework',
'namespace': 'foo',
'configuration': {
'dsn': postgres_dsn,
'redis_url': redis_url
}
}
]
}

session_manager.ensure_schema_exists('foo-bar')
session_manager.ensure_schema_exists('foo-zar')

cfg_path = os.path.join(temporary_directory, 'onegov.yml')

with open(cfg_path, 'w') as f:
f.write(yaml.dump(cfg))

# Add user to bar
runner = CliRunner()
result = runner.invoke(cli, [
'--config', cfg_path,
'--select', '/foo/bar',
'add', 'admin', '[email protected]',
'--password', 'hunterb',
'--no-prompt',
])
assert result.exit_code == 0
assert 'Adding [email protected] to foo/bar' in result.output
assert '[email protected] was added' in result.output

# add user to zar
runner = CliRunner()
result = runner.invoke(cli, [
'--config', cfg_path,
'--select', '/foo/zar',
'add', 'admin', '[email protected]',
'--password', 'hunterz',
'--no-prompt',
])
assert result.exit_code == 0
assert 'Adding [email protected] to foo/zar' in result.output
assert '[email protected] was added' in result.output

# use exits to check if user exists in bar
result = runner.invoke(cli, [
'--config', cfg_path,
'--select', '/foo/bar',
'exists', '[email protected]'
])
assert result.exit_code == 0
assert 'foo-bar [email protected] exists' in result.output
assert 'foo-zar admin@zar exists' not in result.output

# use exits to check if user exists in zar
result = runner.invoke(cli, [
'--config', cfg_path,
'--select', '/foo/zar',
'exists', '[email protected]'
])
assert result.exit_code == 0
assert 'foo-zar [email protected] exists' in result.output
assert 'foo-bar admin@bar exists' not in result.output

# use recursive exists
result = runner.invoke(cli, [
'--config', cfg_path,
'--select', '/foo/*',
'exists', '[email protected]', '-r'
])
assert result.exit_code == 0
assert 'foo-bar [email protected] exists' in result.output
assert 'foo-zar [email protected] does not exist' in result.output

0 comments on commit a0a7962

Please sign in to comment.