From 64659a85ec1fd26c20c24d13645fec1860f67a25 Mon Sep 17 00:00:00 2001 From: Dan Granville Date: Wed, 3 May 2023 07:31:32 +0100 Subject: [PATCH] cli: create user with profile (#433) --- invenio_accounts/cli.py | 6 +++++- tests/test_cli.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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"]