Skip to content

Commit

Permalink
feat(refactor: user
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikstranneheim committed Jun 28, 2023
1 parent 6a0d3c1 commit cff05ee
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
35 changes: 24 additions & 11 deletions tests/cli/test_cli_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
base,
cancel,
delete,
get_user_from_db,
get_users_from_db,
ls_cmd,
scan,
set_analysis_completed,
set_analysis_status,
unarchive_user,
user,
unarchive_user, add_user_to_db,
)


Expand Down Expand Up @@ -205,47 +205,60 @@ def test_delete_ongoing_force(cli_runner, trailblazer_context, caplog):
assert "Deleting" in caplog.text


def test_user_not_in_database(cli_runner, trailblazer_context: Dict[str, MockStore], caplog):
"""Test user when user is not in the database."""
def test_get_user_not_in_database(cli_runner, trailblazer_context: Dict[str, MockStore], caplog):
"""Test getting user when user is not in the database."""
# GIVEN populated Trailblazer database

caplog.set_level("ERROR")

# WHEN looking for a user that is not added
cli_runner.invoke(user, ["not_in_db@disco.com"], obj=trailblazer_context)
cli_runner.invoke(get_user_from_db, ["not_in_db@disco.com"], obj=trailblazer_context)

# THEN log should inform that user was not found
assert "not found" in caplog.text


def test_user_in_database(
def test_get_user_in_database(
cli_runner, trailblazer_context: Dict[str, MockStore], caplog, user_email: str, username: str
):
"""Test user when user is in the database."""
"""Test getting user when user is in the database."""
# GIVEN populated Trailblazer database
caplog.set_level("INFO")

# WHEN looking for a user that is added
cli_runner.invoke(user, [user_email], obj=trailblazer_context)
cli_runner.invoke(get_user_from_db, [user_email], obj=trailblazer_context)

# THEN log informs the user about their full name
assert username in caplog.text


def test_user_added(cli_runner, trailblazer_context: Dict[str, MockStore], caplog):
"""Test user when adding user."""
def test_add_user(cli_runner, trailblazer_context: Dict[str, MockStore], caplog):
"""Test adding a user to the database."""
# GIVEN populated Trailblazer database

caplog.set_level("INFO")

# WHEN adding new user
cli_runner.invoke(
user, ["harriers@disco.com", "--name", "Harry DuBois"], obj=trailblazer_context
add_user_to_db, ["harriers@disco.com", "--name", "Harry DuBois"], obj=trailblazer_context
)

# THEN log should inform that user is added
assert "New user added" in caplog.text

def test_add_user_when_already_exists(cli_runner, trailblazer_context: Dict[str, MockStore], caplog, user_email: str, username: str):
"""Test adding a user to the database when the user already exists."""
# GIVEN populated Trailblazer database

caplog.set_level("INFO")

# WHEN adding new user
cli_runner.invoke(
add_user_to_db, [user_email, "--name", username], obj=trailblazer_context
)

# THEN log should inform that user is added
assert f"User with email {user_email} found" in caplog.text

def test_users(
cli_runner, trailblazer_context: Dict[str, MockStore], caplog, user_email: str, username: str
Expand Down
36 changes: 22 additions & 14 deletions trailblazer/cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,33 @@ def update_analysis(context, analysis_id: int):
context.obj["trailblazer"].update_run_status(analysis_id=analysis_id)


@base.command()
@base.command("add-user")
@click.option("--name", help="Name of new user to add")
@click.argument("email", default=environ_email())
@click.pass_context
def user(context, name: str, email: str) -> None:
"""Add a new or display information about an existing user."""
def add_user_to_db(context, email: str, name: str) -> None:
"""Add a new user to the database."""
trailblazer_db: Store = context.obj["trailblazer"]
existing_user = trailblazer_db.get_user(email=email, exclude_archived=False)
if existing_user:
LOG.info(f"Existing user found: {existing_user.to_dict()}")
elif name:
new_user = trailblazer_db.add_user(email=email, name=name)
LOG.info(f"New user added: {email} ({new_user.id})")
else:
LOG.error("User not found")
if is_existing_user(user=existing_user, email=email):
return
new_user = trailblazer_db.add_user(email=email, name=name)
LOG.info(f"New user added: {new_user.email} ({new_user.id})")


@base.command("users")
@base.command("get-user")
@click.argument("email", default=environ_email())
@click.pass_context
def get_user_from_db(context, email: str) -> None:
"""Display information about an existing user."""
trailblazer_db: Store = context.obj["trailblazer"]
existing_user = trailblazer_db.get_user(email=email, exclude_archived=False)
if not is_existing_user(user=existing_user, email=email):
return
LOG.info(f"Existing user found: {existing_user.to_dict()}")


@base.command("get-users")
@click.option("--name", type=click.types.STRING, help="Name of new users to list")
@click.option("--email", type=click.types.STRING, help="Name of new users to list")
@click.option("--exclude-archived", is_flag=True, help="Exclude archived users")
Expand All @@ -115,10 +124,9 @@ def get_users_from_db(context, name: str, email: str, exclude_archived: bool) ->
users: List[User] = trailblazer_db.get_users(
email=email, exclude_archived=exclude_archived, name=name
)

LOG.info("Listing users in database:")
for user in users:
LOG.info(f"{user}")
LOG.info(f"{user.to_dict()}")


@base.command("archive-user")
Expand All @@ -144,7 +152,7 @@ def archive_user(context, email: str) -> None:
def unarchive_user(context, email: str) -> None:
"""Unarchive an existing user identified by email."""
trailblazer_db: Store = context.obj["trailblazer"]
existing_user: user = trailblazer_db.get_user(email=email, exclude_archived=False)
existing_user: User = trailblazer_db.get_user(email=email, exclude_archived=False)

if not is_existing_user(user=existing_user, email=email):
return
Expand Down
1 change: 1 addition & 0 deletions trailblazer/cli/utils/user_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def is_existing_user(user: User, email: str) -> bool:
if not user:
LOG.error(f"User with email {email} not found")
return False
LOG.info(f"User with email {email} found")
return True


Expand Down

0 comments on commit cff05ee

Please sign in to comment.