Skip to content

Commit

Permalink
Support api-key and password as parameter when using users.create_def…
Browse files Browse the repository at this point in the history
…ault task (#2516)

This PR includes the following changes:
* Adds `--api-key` and `--password` parameters to
`argilla.tasks.users.create_default`.
* Now `argilla.tasks.users.create_default` checks if there is a user
using default username before creating it and showing a message in such
case (but not raising an error).
  • Loading branch information
jfcalvo authored Mar 13, 2023
1 parent ebcd381 commit 084de8f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
9 changes: 3 additions & 6 deletions src/argilla/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

DEFAULT_MAX_KEYWORD_LENGTH = 128

API_KEY_HEADER_NAME = "X-Argilla-Api-Key"
WORKSPACE_HEADER_NAME = "X-Argilla-Workspace"

DEFAULT_USERNAME = "argilla"
DEFAULT_PASSWORD = "1234"
DEFAULT_API_KEY = "argilla.apikey" # Keep the same api key for now
DEFAULT_API_KEY = "argilla.apikey"
DEFAULT_MAX_KEYWORD_LENGTH = 128
DEFAULT_TELEMETRY_KEY = "C6FkcaoCbt78rACAgvyBxGBcMB3dM3nn"

# TODO: This constant will be drop out with issue
# https://github.com/argilla-io/argilla/issues/2251 fix
Expand All @@ -29,6 +29,3 @@
_OLD_WORKSPACE_HEADER_NAME = "X-Rubrix-Workspace"

ES_INDEX_REGEX_PATTERN = r"^(?!-|_)[a-z0-9-_]+$"


DEFAULT_TELEMETRY_KEY = "C6FkcaoCbt78rACAgvyBxGBcMB3dM3nn"
27 changes: 18 additions & 9 deletions src/argilla/tasks/users/create_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,35 @@


@click.command()
@click.option("--api-key", default=DEFAULT_API_KEY, help="API key for the user.")
@click.option("--password", default=DEFAULT_PASSWORD, help="Password for the user.")
@click.option("-q", "--quiet", is_flag=True, default=False, help="Run without output.")
def create_default(quiet: bool):
def create_default(api_key: str, password: str, quiet: bool):
"""Creates a user with default credentials on database suitable to start experimenting with argilla."""
with SessionLocal() as session, session.begin():
with SessionLocal() as session:
if accounts.get_user_by_username(session, DEFAULT_USERNAME):
if not quiet:
click.echo(f"User with default username already found on database, will not do anything.")

return

session.add(
User(
first_name="",
username=DEFAULT_USERNAME,
role=UserRole.admin,
api_key=DEFAULT_API_KEY,
password_hash=accounts.hash_password(DEFAULT_PASSWORD),
api_key=api_key,
password_hash=accounts.hash_password(password),
workspaces=[Workspace(name=DEFAULT_USERNAME)],
)
)
session.commit()

if not quiet:
click.echo("User with default credentials succesfully created:")
click.echo(f"• username: {DEFAULT_USERNAME!r}")
click.echo(f"• password: {DEFAULT_PASSWORD!r}")
click.echo(f"• api_key: {DEFAULT_API_KEY!r}")
if not quiet:
click.echo("User with default credentials succesfully created:")
click.echo(f"• username: {DEFAULT_USERNAME!r}")
click.echo(f"• password: {password!r}")
click.echo(f"• api_key: {api_key!r}")


if __name__ == "__main__":
Expand Down
44 changes: 44 additions & 0 deletions tests/tasks/users/test_create_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,52 @@ def test_create_default(db: Session):
assert [ws.name for ws in default_user.workspaces] == [DEFAULT_USERNAME]


def test_create_default_with_specific_api_key_and_password(db: Session):
result = CliRunner().invoke(create_default, "--api-key my-api-key --password my-password")

assert result.exit_code == 0
assert result.output != ""
assert db.query(User).count() == 1

default_user = db.query(User).filter_by(username=DEFAULT_USERNAME).first()
assert default_user
assert default_user.role == UserRole.admin
assert default_user.api_key == "my-api-key"
assert accounts.verify_password("my-password", default_user.password_hash)
assert [ws.name for ws in default_user.workspaces] == [DEFAULT_USERNAME]


def test_create_default_quiet(db: Session):
result = CliRunner().invoke(create_default, ["--quiet"])

assert result.exit_code == 0
assert result.output == ""
assert db.query(User).count() == 1


def test_create_default_with_existent_default_user(db: Session):
result = CliRunner().invoke(create_default)

assert result.exit_code == 0
assert result.output != ""
assert db.query(User).count() == 1

result = CliRunner().invoke(create_default)

assert result.exit_code == 0
assert result.output == "User with default username already found on database, will not do anything.\n"
assert db.query(User).count() == 1


def test_create_default_with_existent_default_user_and_quiet(db: Session):
result = CliRunner().invoke(create_default)

assert result.exit_code == 0
assert result.output != ""
assert db.query(User).count() == 1

result = CliRunner().invoke(create_default, ["--quiet"])

assert result.exit_code == 0
assert result.output == ""
assert db.query(User).count() == 1

0 comments on commit 084de8f

Please sign in to comment.