def test_retrieve_user_invalid_credentials(app): from aura.models import User db = get_db() db.session.add(User(username='******', password='******')) db.session.commit() from aura.services import retrieve_user #Non existing user with pytest.raises(ServiceError): retrieve_user({'username':'******', 'password': '******'}) #Wrong password still raises Service error with pytest.raises(ServiceError): retrieve_user({'username':'******', 'password': '******'})
def test_retrieve_user(app): from aura.models import User from aura.services import retrieve_user with app.app_context(): db = get_db() db.session.add(User(username='******', password='******')) db.session.commit() user = User.query.filter_by(username='******').first() print("==") print(user) print("==") user = retrieve_user({'username':'******', 'password': '******'}) assert isinstance(user, User) assert user.username == 'admin' assert user.password_is('admin')
def register_cli(): with current_app.app_context(): migrate = Migrate(current_app, get_db()) @current_app.cli.command() def sql(): base_dir = os.path.abspath("aura/database") uri = current_app.config['SQLALCHEMY_DATABASE_URI'] if uri.startswith('postgresql'): out = os.path.join(base_dir, "pg.sql") elif uri.startswith('sqlite'): out = os.path.join(base_dir, "sqlite.sql") else: raise ValueError("Unknown database URI: {}".format(uri)) # https://stackoverflow.com/a/40984270/8188975 data = io.StringIO() with redirect_stdout(data): upgrade(sql=True) with open(out, 'w') as f: f.write(data.getvalue()) print("Written to: {}".format(out))
from sqlalchemy.ext.hybrid import hybrid_property from base64 import b64encode from hashlib import sha256 from datetime import datetime import bcrypt from aura.strings import USERNAME_EMPTY, USERNAME_LENGTH_FAIL, PASSWORD_TOO_SHORT from aura.constants import PASSWORD_MIN_LENGTH, USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH from aura.exceptions import UserDomainError from aura.database import get_db db = get_db() roles_users = db.Table( 'roles_users', db.metadata, db.Column('user_id', db.Integer(), db.ForeignKey('users.id'), primary_key=True), db.Column('role_id', db.Integer(), db.ForeignKey('roles.id'), primary_key=True)) class Role(db.Model): __tablename__ = 'roles' id = db.Column('id', db.Integer(), db.Sequence('role_seq_id'), primary_key=True) name = db.Column('name', db.String(), unique=True, nullable=False)