Beispiel #1
0
def test_get_sys_user_by_email(mock_connect):
    """Get user from database contents by E-mail"""
    with allure.step('Get sys_user by email'):
        with app.app_context():
            db = AndrewDB()
            expected = {
                'user_id': 5,
                'email': "*****@*****.**",
                'password': "******",
                'role': "ROLE_ADMIN"
            }
            mock_connect().cursor.return_value.fetchone.return_value = expected
            result = db.get_sys_user_by_email("*****@*****.**")
        assert result == expected
def login():
    logger.info("Got a login page request: %s" % request)
    db = AndrewDB()
    form = LoginForm()
    logger.info("Validating the login form")
    form.csrf_enabled = False
    if form.validate_on_submit():
        sys_user = db.get_sys_user_by_email(form.email.data)
        if not sys_user or not check_password(sys_user['password'],
                                              form.password.data):
            flash('Email Address or Password is invalid')
            logger.info(
                "Email address or password is invalid, redirecting to the login page"
            )
            return redirect(url_for('login'))
        user = User(sys_user['user_id'], sys_user['email'],
                    sys_user['password'], sys_user['role'])
        if form.remember_me.data:
            login_user(user, remember=True)
        else:
            login_user(user)
        flash('Logged in successfully')
        logger.info("Logged in successfully: ID = %s" % user.get_id())
        if user.is_receptionist():
            logger.info("Redirecting to Manage booking page")
            return redirect(url_for('manageBooking'))
        if user.is_hotel_admin():
            logger.info("Redirecting to My hotels page")
            return redirect(url_for('myHotels'))
        if user.is_admin():
            logger.info("Redirecting to admin page")
            return redirect(url_for('admin'))
        logger.info("Redirecting to index page")
        return redirect(url_for('index'))
    logger.info("Rendering the login page")
    return render_template('login.html', form=form)