def init(user, password, pip_user, pip_pwd): """Init database and create an admin user""" from flask_migrate import stamp from extrapypi.extensions import db, migrate from extrapypi import models click.echo("Creating database...") db.create_all() click.echo("Database created") click.echo("Creating user %s" % user) pwd = custom_app_context.hash(password) user = models.User(username=user, email="*****@*****.**", password_hash=pwd, role='admin') pip_pwd = custom_app_context.hash(pip_pwd) pip_usr = models.User(username=pip_user, email="*****@*****.**", password_hash=pip_pwd, role="installer") db.session.add(pip_usr) db.session.add(user) db.session.commit() click.echo("User %s created" % user.username) click.echo("User %s created" % pip_usr.username) stamp(directory=migrate.directory)
def test_user(): """Test user features""" u = models.User(username='******', email='*****@*****.**', password_hash='badhash', role='maintainer') assert u.is_authenticated is True assert u.is_anonymous is False assert u.get_id() == 'None' assert str(u) == "<User myuser>" assert u.is_admin is False with pytest.raises(Exception): u.role = 'badrole'
def create_user(username, password, email, role): """Create a new user. Default role is admin """ from extrapypi.extensions import db from extrapypi import models pwd = custom_app_context.hash(password) user = models.User(username=username, password_hash=pwd, email=email, role=role) db.session.add(user) try: db.session.commit() click.echo("User %s created" % username) except Exception: click.echo("Cannot create user %s" % username, err=True) db.rollback()