diff --git a/invenio_accounts/cli.py b/invenio_accounts/cli.py index 28c709f2..b511a199 100644 --- a/invenio_accounts/cli.py +++ b/invenio_accounts/cli.py @@ -8,6 +8,7 @@ """Command Line Interface for accounts.""" +import json from datetime import datetime from functools import wraps @@ -48,9 +49,10 @@ def roles(): @click.password_option() @click.option("-a", "--active", default=False, is_flag=True) @click.option("-c", "--confirm", default=False, is_flag=True) +@click.option("-p", "--profile") @with_appcontext @commit -def users_create(email, password, active, confirm): +def users_create(email, password, active, confirm, profile): """Create a user.""" kwargs = dict(email=email, password=password, active="y" if active else "") @@ -61,6 +63,8 @@ def users_create(email, password, active, confirm): kwargs["active"] = active if confirm: kwargs["confirmed_at"] = datetime.utcnow() + if profile: + kwargs["user_profile"] = json.loads(profile) _datastore.create_user(**kwargs) click.secho("User created successfully.", fg="green") kwargs["password"] = "****" diff --git a/tests/test_cli.py b/tests/test_cli.py index eb9a7d93..f1d3dc3b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -31,6 +31,45 @@ def test_cli_createuser(app): result = runner.invoke(users_create, ["not-an-email", "--password", "123456"]) assert result.exit_code == 2 + # Create user with profile + result = runner.invoke( + users_create, + [ + "--password", + "AValidPassword66!", + "--profile", + '{"full_name" : "Test User"}', + "test1@example.com", + ], + ) + assert result.exit_code == 0 + + # Create user with profile that is not valid JSON + result = runner.invoke( + users_create, + [ + "--password", + "AValidPassword66!", + "--profile", + "profile", + "test2@example.com", + ], + ) + assert result.exit_code != 0 + + # Create user with JSON profile that will not validate + result = runner.invoke( + users_create, + [ + "--password", + "AValidPassword66!", + "--profile", + '{"full_name" : "test_user", "extra_data" : "not_allowed"}', + "test3@example.com", + ], + ) + assert result.exit_code != 0 + # Create user result = runner.invoke( users_create, ["info@inveniosoftware.org", "--password", "123456"]