Skip to content

Commit

Permalink
cli: create user with profile (inveniosoftware#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfdan authored May 3, 2023
1 parent 4792370 commit 64659a8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion invenio_accounts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

"""Command Line Interface for accounts."""

import json
from datetime import datetime
from functools import wraps

Expand Down Expand Up @@ -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 "")

Expand All @@ -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"] = "****"
Expand Down
39 changes: 39 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}',
"[email protected]",
],
)
assert result.exit_code == 0

# Create user with profile that is not valid JSON
result = runner.invoke(
users_create,
[
"--password",
"AValidPassword66!",
"--profile",
"<json>profile</json>",
"[email protected]",
],
)
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"}',
"[email protected]",
],
)
assert result.exit_code != 0

# Create user
result = runner.invoke(
users_create, ["[email protected]", "--password", "123456"]
Expand Down

0 comments on commit 64659a8

Please sign in to comment.