def test_update_hotel_by_id(mock_connect): with allure.step('Update hotel'): with app.app_context(): db = AndrewDB() result = db.update_hotel_by_id(1, "Moscow", "Innopolis", "NoName", 4, "Not named hotel", "noname.png") assert result
def get_profile(): logger.info("Got a profile page request: %s" % request) db = AndrewDB() form = ProfileForm() user = current_user user_info = None form.csrf_enabled = False if user.is_customer(): res = db.get_customer_by_id(user.user_id) user_info = Customer(res['first_name'], res['last_name'], user.email, res['phone_number'], res['payment_info']) elif user.is_hotel_admin(): res = db.get_hotel_admin_by_id(user.user_id) if res: user_info = HotelAdmin(res['first_name'], res['last_name'], user.email, res['phone_number']) else: flash("Unable to find a hotel admin entry") logger.info( "Unable to find a hotel admin entry, Redirecting to index page" ) return redirect(url_for('index')) elif user.is_receptionist(): user_info = db.get_receptionist_by_id(user.user_id) user_info['email'] = user.email elif user.is_admin(): db.get_admin_by_id(user.user_id) user_info['email'] = user.email logger.info("Rendering the Profile page") return render_template('profile.html', form=form, user=user_info)
def addProperty(): logger.info("Got an Add property page request: %s" % request) g.role = 'hotel_admin' db = AndrewDB() form = RegisterForm() logger.info("Validating the register form") form.csrf_enabled = False if form.validate_on_submit(): if not form.password.data == form.password_confirmation.data: logger.info( "Password confirmation failed, Redirecting to add property page" ) return redirect(url_for('addProperty')) hash_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') res = db.insert_sys_user(form.email.data, hash_password, g.role) if res is None: flash('User with this email already registered') logger.info("User with this email (%s) alredy registered, " "redirecting to add property page" % form.email.data) return redirect(url_for('addProperty')) user_id = res[0].strip('()').split(',')[0] db.insert_hotel_admin(str(user_id), str(form.first_name.data), str(form.last_name.data), str(form.telephone.data)) print(user_id) user = User(user_id, form.email.data, hash_password, g.role) login_user(user) flash('User successfully registered') logger.info( "User (ID = %s) successfully registered, redirecting to My hotels page" % user.get_id()) return redirect(url_for('myHotels')) logger.info("Rendering the Add property page") return render_template('property.html', form=form)
def test_setup_room_by_id(mock_connect): """Update hotel room parameters""" with allure.step('Set up room by id'): with app.app_context(): db = AndrewDB() db.set_up_room_by_id(1, 1, 50, "room blue", "very blue room", 5000, 5)
def test_add_new_receptionist(mock_connect): """Add new receptionist user""" with allure.step('Add receptionist'): with app.app_context(): db = AndrewDB() db.add_new_receptionist(1, 1, "John", "Doe", "+0-000-00-00-00", 5000)
def myHotels(): logger.info("Got a My hotels page request: %s" % request) db = AndrewDB() form = UDHotelForm() form.csrf_enabled = False if current_user.is_hotel_admin(): logger.info("Validating the Update or Delete hotel form") if form.validate_on_submit(): if form.edit.data: logger.info("Redirecting to Edit hotel page") return redirect( url_for('editHotel', hotel_id=form.hotel_id.data)) if form.delete.data: img = db.remove_hotel_by_id(form.hotel_id.data) os.remove(os.path.abspath('app' + img)) flash('Hotel was removed') logger.info( "Hotel (ID=%s) was removed, Redirecting to My Hotels page" % form.hotel_id.data) return redirect(url_for('myHotels')) if form.manage.data: logger.info("Redirecting to manage hotel page") return redirect( url_for('manageHotel', hotel_id=form.hotel_id.data)) if form.add_hotel.data: logger.info("Redirecting to Add hotel page") return redirect(url_for('addHotel')) hotels = db.get_hotels_by_admin_id(current_user.get_id()) logger.info("Rendering the My hotels page") return render_template('my_hotel.html', form=form, hotels=hotels) else: flash("Access error") logger.info("Access error, Redirecting to login page") return redirect(url_for('login'))
def update_profile(): logger.info("Got a profile page request: %s" % request) db = AndrewDB() form = ProfileForm() user = current_user logger.info("Validating the profile form") form.csrf_enabled = False if form.validate_on_submit(): if user.is_customer(): db.update_customer(user.user_id, form.first_name.data, form.last_name.data, form.telephone.data, form.credit_card.data) logger.info("Redirecting to index page") return redirect(url_for('index')) elif user.is_hotel_admin(): db.update_hotel_admin(user.user_id, form.first_name.data, form.last_name.data, form.telephone.data) logger.info("Redirecting to index page") return redirect(url_for('index')) elif user.is_admin(): db.update_admin(user.user_id, form.first_name.data, form.last_name.data, form.telephone.data) logger.info("Redirecting to admin page") return redirect(url_for('admin')) flash("Invalid form") logger.info("Invalid profile form, Redirecting to add property page") return redirect(url_for('index'))
def addHotel(): logger.info("Got an Add hotel page request: %s" % request) db = AndrewDB() form = CUHotelForm() form.csrf_enabled = False if current_user.is_hotel_admin(): logger.info("Validating the Create and Update hotel form") if form.validate_on_submit(): img_name = imgName(form.img.data.filename) if img_name: img_path = '/static/img/hotels/' + img_name try: db.insert_location_if_not_exists(form.country.data, form.city.data) db.add_hotel(form.city.data, form.address.data, form.hotel_name.data, form.stars.data, form.description.data, current_user.user_id, img_path) except Exception as e: logger.exception("Unable to add Hotel") print(e) logger.info("Redirecting to My hotels page") return redirect(url_for('myHotels')) form.img.data.save( os.path.join(app.config['UPLOAD_FOLDER'], img_name)) flash('Hotel was added') logger.info("Hotel was added, Redirecting to My hotels page") return redirect(url_for('myHotels')) logger.info("Rendering the Edit hotel page") return render_template('edit_hotel.html', form=form, hotel=None) else: flash("Access error") logger.info("Access error, Redirecting to login page") return redirect(url_for('login'))
def editHotel(hotel_id): logger.info("Got an Edit hotel page request: %s" % request) db = AndrewDB() form = CUHotelForm() form.csrf_enabled = False if current_user.is_hotel_admin(): logger.info("Validating the Create and Update hotel form") if form.validate_on_submit(): img_name = imgName(form.img.data.filename) if img_name: db.insert_location_if_not_exists(form.country.data, form.city.data) old_img = db.get_image_name_by_hotel_id(hotel_id) img_path = '/static/img/hotels/' + img_name db.update_hotel_by_id(hotel_id, form.city.data, form.address.data, form.hotel_name.data, form.stars.data, form.description.data, img_path) form.img.data.save( os.path.join(app.config['UPLOAD_FOLDER'], img_name)) os.remove(os.path.abspath('app' + old_img)) return redirect(url_for('myHotels')) res = db.get_hotel_and_address_by_id(hotel_id) logger.info("Rendering the Edit hotel page") return render_template('edit_hotel.html', form=form, hotel=res) else: flash("Access error") logger.info("Access error, Redirecting to login page") return redirect(url_for('login'))
def admin(): logger.info("Got an admin panel page request: %s" % request) form = CAdmin() db = AndrewDB() g.role = 'admin' logger.info("Validating the Create admin form") form.csrf_enabled = False if request.method == 'POST' and form.validate_on_submit(): hash_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user_id = db.insert_sys_user_get_id(form.email.data, hash_password) if user_id is None: flash('User with this email already registered') logger.info( "User with this email already registered, Redirecting to admin page" ) return redirect(url_for('admin-panel')) db.insert_admin(str(user_id), form.first_name.data, form.last_name.data, form.telephone.data) flash("Admin was added") logger.info("Admin was added, Redirecting to admin page") return redirect(url_for('admin-panel')) hotels = db.get_all_hotels() users = db.get_all_system_users() db_stat = db.get_db_statistics() admins = db.get_all_admins() g.db.commit() logger.info("Rendering the admin_panel page") return render_template('admin_panel.html', hotels=hotels, users=users, db_stat=db_stat, form=form, admins=admins)
def moreInfo(hotel_id): logger.info("Got a more info page request: %s" % request) db = AndrewDB() form = ReserveRoomForm() form.csrf_enabled = False search = session['search'] search['hotel_id'] = hotel_id if request.method == 'POST': info = form.data info['customer_id'] = current_user.user_id checkin = datetime.datetime.strptime(search['checkin'], '%Y-%m-%d').date() checkout = datetime.datetime.strptime(search['checkout'], '%Y-%m-%d').date() nights = (checkout - checkin).days cost = db.get_cost_by_id(form.room_id.data) info['amount'] = int(form.quantity.data) * nights * int(cost) info['transaction_id'] = db.create_transaction_get_id(info) db.add_booking(info) flash('Room was reserved') logger.info("Room with ID=%s was reserved" % form.room_id.data) hotel = db.get_vw_hotel_by_id(hotel_id) rooms = db.search_get_rooms(search) cust_info = db.get_vw_customer_by_id(current_user.user_id) logger.info("Rendering the More info page") return render_template('booking.html', form=form, search=search, hotel=hotel, rooms=rooms, cust_info=cust_info)
def test_get_image_name_by_hotel_id(mock_connect): with allure.step('Get hotel image'): with app.app_context(): db = AndrewDB() expected = {'img': "noname.png"} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.get_image_name_by_hotel_id({}) assert result == expected['img']
def test_search_hotel_by_form(mock_connect): with allure.step('Get rooms by search query'): with app.app_context(): db = AndrewDB() expected = ['hotel1', 'hotel2'] mock_connect().cursor.return_value.fetchall.return_value = expected result = db.search_hotels_by_form(create_search_form()) assert result == expected
def load_user(user_id): logger.info("Loading a user: ID=%s" % user_id) db = AndrewDB() res = db.get_user_by_id(user_id) if not res: logger.warning("User not found: ID=%s" % user_id) return None return User(res['user_id'], res['email'], res['password'], res['role'])
def test_get_hotel_and_address_by_id(mock_connect): with allure.step('Get hotel and address'): with app.app_context(): db = AndrewDB() expected = {'hotel_id': "1", 'address': "Innopolis"} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.get_hotel_and_address_by_id(1) assert result == expected
def test_get_hotels_by_admin_id(mock_connect): with allure.step('Get user by id'): with app.app_context(): db = AndrewDB() expected = ['hotel1', 'hotel2'] mock_connect().cursor.return_value.fetchall.return_value = expected result = db.get_hotels_by_admin_id(1) assert result == expected
def test_get_user_by_id(mock_connect): with allure.step('Get user by id'): with app.app_context(): db = AndrewDB() expected = {1: 'user1'} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.get_user_by_id(1) assert result == expected
def newBooking(): logger.info("Got a New booking page request: %s" % request) db = AndrewDB() recep = session['recep'] hotel = session['hotel'] today = datetime.datetime.now().date().strftime("%Y-%m-%d") rooms = db.get_rooms_by_params(recep['hotel_id'], today, today) logger.info("Rendering the New booking page") return render_template('new_booking.html', rooms=rooms, hotel=hotel)
def test_get_some_info_by_user_id(mock_connect): """Get info on bookings and their details by customer id""" with allure.step('Get some info by user id'): with app.app_context(): db = AndrewDB() expected = {1: "info"} mock_connect().cursor.return_value.fetchall.return_value = expected result = db.get_some_info_by_user_id(1) assert result == expected
def test_get_options_by_params(mock_connect): """Get rooms options from database by parameters""" with allure.step('Get options by parameters'): with app.app_context(): db = AndrewDB() expected = ["option1", "option2"] mock_connect().cursor.return_value.fetchone.return_value = expected result = db.get_option_by_params(True, True, True, True, True) assert result == expected
def test_insert_option(mock_connect): """Insert room option and get its id""" with allure.step('Insert option'): with app.app_context(): db = AndrewDB() expected = {'option_id': 1} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.insert_option(True, True, True, True, True) assert result == expected['option_id']
def test_select_config_id(mock_connect): """Get bed configuration id from database""" with allure.step('Select config id'): with app.app_context(): db = AndrewDB() expected = 1 mock_connect().cursor.return_value.fetchone.return_value = expected result = db.select_config(False, False, True) assert result == expected
def test_get_receptionists_by_hotel_id(mock_connect): """Fetch receptionist user data by hotel id""" with allure.step('Get receptionists of hotel'): with app.app_context(): db = AndrewDB() expected = ["receptionist1", "receptionist2"] mock_connect().cursor.return_value.fetchall.return_value = expected result = db.get_receptionists_by_hotel_id(1) assert result == expected
def test_get_db_statistics(mock_connect): """Get IO database performance""" with allure.step('Get db statistics'): with app.app_context(): db = AndrewDB() expected = [{'cache_hit_ratio': 1.01, 'numbackends': 0}] mock_connect().cursor.return_value.fetchall.return_value = expected result = db.get_db_statistics() assert result == expected[0]
def test_insert_sys_user_get_id(mock_connect): """Insert new user into database and return its id""" with allure.step('Insert into sys_user and get id'): with app.app_context(): db = AndrewDB() expected = {'user_id': 5} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.insert_sys_user_get_id("*****@*****.**", "123456") assert result == expected['user_id']
def test_get_rooms_with_settings_by_id(mock_connect): """Get hotel rooms list from database with settings""" with allure.step('Get rooms_with_settings by id'): with app.app_context(): db = AndrewDB() expected = {1: "room1"} mock_connect().cursor.return_value.fetchall.return_value = expected result = db.get_rooms_with_settings_by_id(1) assert result == expected
def test_get_booked_rooms_by_hotel_id(mock_connect): """Get booked rooms list of hotel by hotel id""" with allure.step('Get booked rooms by hotel id'): with app.app_context(): db = AndrewDB() expected = {1: "room1"} mock_connect().cursor.return_value.fetchall.return_value = expected result = db.get_rooms_with_settings_by_id(1) assert result == expected
def test_get_cost_by_id(mock_connect): """FEtch room price by room id""" with allure.step('Get cost by room id'): with app.app_context(): db = AndrewDB() expected = {'cost': 1} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.get_cost_by_id(1) assert result == expected['cost']
def test_get_hotel_by_id(mock_connect): """Get hotel data from database by id""" with allure.step('Get hotel by id'): with app.app_context(): db = AndrewDB() expected = {1: 'hotel1'} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.get_hotel_by_id(1) assert result == expected
def test_create_transaction_get_id(mock_connect): """Create transaction and get its id""" with allure.step('Create transaction and get id'): with app.app_context(): db = AndrewDB() expected = {'transaction_id': 1} mock_connect().cursor.return_value.fetchone.return_value = expected result = db.create_transaction_get_id({}) assert result == expected['transaction_id']