def test_cancel_api_with_success_and_then_rebooking(self): from app.tests.test_base import book_parking_slot from app.services.utils.common_utility import CommonUtility start_time = CommonUtility.get_time() end_time = CommonUtility.get_time(1) slot_id = 2 local_booking = book_parking_slot(self.test_app_client, start_time, end_time, slot_id, self.jwt_token) # Prepare test data for API request_data = { "booking_id": local_booking["data"]["booking_details"]["id"] } headers = {"Authorization": "JWT {}".format(self.jwt_token)} resp = None with self.test_app_client as c: resp = c.post("/parking-slot/cancel-booking", json=request_data, headers=headers) print(resp.data) resp_json = json.loads(resp.data) self.assertEqual("Your booking has been cancelled.", resp_json["message"]) self.assertEqual(0, resp_json["status_code"]) # Rebook the same slot again to verify cancellation local_booking = book_parking_slot(self.test_app_client, start_time, end_time, slot_id, self.jwt_token) print("Rebooking Response") print(local_booking)
def test_booking_api_with_already_booked_slot(self): # Prepare test data for API from app.services.utils.common_utility import CommonUtility request_data = { "start_time": int(CommonUtility.get_time() / 1000), # Convert to secs "end_time": int(CommonUtility.get_time(offset=1) / 1000), "parking_slot_id": 1 } headers = {"Authorization": "JWT {}".format(self.jwt_token)} resp = None with self.test_app_client as c: resp_dup = c.post("/parking-slot/book", json=request_data, headers=headers) # Another request trying to book the same slot for the same date time resp = c.post("/parking-slot/book", json=request_data, headers=headers) print(resp.data) resp_json = json.loads(resp.data) self.assertEqual("This slot is already booked for the given time.", resp_json.get("message")) self.assertEqual(103, resp_json["status_code"])
def test_booking_api_with_start_time_less_than_current_time(self): # Prepare test data for API from app.services.utils.common_utility import CommonUtility request_data = { "start_time": int(CommonUtility.get_time(offset=-1) / 1000), "end_time": int(CommonUtility.get_time() / 1000), "parking_slot_id": 1 } headers = {"Authorization": "JWT {}".format(self.jwt_token)} resp = None with self.test_app_client as c: resp = c.post("/parking-slot/book", json=request_data, headers=headers) print(resp.data) resp_json = json.loads(resp.data) self.assertEqual( "Parking start time cannot be less than current time!", resp_json.get("message")) self.assertEqual(105, resp_json["status_code"])
def format_booking_details(booking): """ Transform booking object into readable form :param booking: :return: """ parking_slot = ParkingSlot.query.filter_by(id=booking.parking_slot_id).first() parking_area = ParkingArea.query.filter_by(id=parking_slot.parking_area_id).first() parking_number = BookingUtility.get_parking_number(booking) user = User.query.filter_by(id=booking.user_id).one() start_time = CommonUtility.get_date_from_epoch(booking.start_time, ConstantsUtility.DATE_TIME_FORMAT) end_time = CommonUtility.get_date_from_epoch(booking.end_time, ConstantsUtility.DATE_TIME_FORMAT) return { "id": booking.id, ConstantsUtility.PARKING_AREA: parking_area.name, ConstantsUtility.PARKING_SLOT: parking_slot.id, ConstantsUtility.PARKING_NUMBER: parking_number, ConstantsUtility.START_TIME: start_time, ConstantsUtility.END_TIME: end_time, ConstantsUtility.USER_ID: user.id, ConstantsUtility.USER_EMAIL: user.email, ConstantsUtility.BOOKING_STATUS: booking.status }
def validate_parking_date_time(start_time, end_time): if int(CommonUtility.get_time()/1000) > start_time: raise APIException(ResponseInfo.CODE_INVALID_PARKING_DATE_TIME, ResponseInfo.MESSAGE_START_TIME_LESS_THAN_CURRENT_TIME) if int(CommonUtility.get_time()/1000) > end_time: raise APIException(ResponseInfo.CODE_INVALID_PARKING_DATE_TIME, ResponseInfo.MESSAGE_END_TIME_LESS_THAN_CURRENT_TIME)
def book_parking_slot(parking_slot_id, start_time, end_time): # Validate slot id, if not valid then an exception will be raised ParkingSlotUtility.validate_parking_slot_id(parking_slot_id) # Check if datetime provided is valid or not ParkingSlotUtility.validate_parking_date_time(start_time, end_time) # Check if parking slot is available for the requested date time range available, booking = ParkingSlotUtility.is_parking_slot_available( parking_slot_id, start_time, end_time) if not available: raise APIException( status_code=ResponseInfo.CODE_PARKING_SLOT_ALREADY_BOOKED, message=ResponseInfo.MESSAGE_PARKING_SLOT_ALREADY_BOOKED, data={"id": booking.id}) # Book the slot for the given date time period booking = ParkingSlotUtility.book_parking_slot(parking_slot_id, start_time, end_time) # Format booking object into readable dict booking_details = BookingUtility.format_booking_details(booking) # Send out an email to user user = CommonUtility.get_authenticated_user() BookingUtility.notify_user_via_email(user, booking_details) return booking_details
def create_parking_slot(area_id): slot = ParkingSlot(parking_area_id=area_id) db.session.add(slot) db.session.commit() return CommonUtility.convert_to_dict(slot)
def role_wrapper(*args, **kwargs): """Wraps the view function""" # Get authenticated user user = CommonUtility.get_authenticated_user() if user.role.name not in allowed_roles: return ResponseInfo.MESSAGE_UNAUTHORIZED_ACCESS, ResponseInfo.CODE_UNAUTHORIZED_ACCESS return view_function(*args, **kwargs)
def setUp(self): # Set up env variable APP_CONFIG_PATH # It contains the path of required external config file with patch.dict( 'os.environ', { "APP_CONFIG_PATH": "D:/HassanWorkSpace/flask-parking-app/parking_app_test_config.py" }): from app import app, db self.app = app self.db = db # Initialize test environment database db.create_all() # Setup test data from app.tests.test_base import setup_test_data setup_test_data() self.test_app_client = app.test_client() # Make booking, for which Cancel API test cases will be executed from app.tests.test_base import book_parking_slot from app.services.utils.common_utility import CommonUtility from app.tests.test_base import login_user from app.tests.test_base import register_user # Setup scenario for Cancelling API register_user(self.test_app_client, "*****@*****.**", "12345") token_resp = login_user(self.test_app_client, "*****@*****.**", "12345") self.jwt_token = token_resp["access_token"] self.booking = book_parking_slot(self.test_app_client, CommonUtility.get_time(), CommonUtility.get_time(1), 1, self.jwt_token) print(self.booking)
def test_booking_api_success(self): # Prepare test data for API from app.services.utils.common_utility import CommonUtility request_data = { "start_time": int(CommonUtility.get_time() / 1000), # Convert to secs "end_time": int(CommonUtility.get_time(offset=1) / 1000), "parking_slot_id": 1 } headers = {"Authorization": "JWT {}".format(self.jwt_token)} resp = None with self.test_app_client as c: resp = c.post("/parking-slot/book", json=request_data, headers=headers) print(resp.data) resp_json = json.loads(resp.data) self.assertEqual("Success", resp_json.get("message")) self.assertEqual(0, resp_json["status_code"])
def create_booking(slot_id, start_time, end_time): """ Create booking entry for given parking slot. :param slot_id: :param start_time: :param end_time: :return: """ user = CommonUtility.get_authenticated_user() booking = Booking(parking_slot_id=slot_id, start_time=start_time, end_time=end_time, status=ConstantsUtility.BOOKED, user_id=user.id) db.session.add(booking) db.session.commit() return booking
def cancel_parking_slot_booking_api(): """ Cancel a parking slot booking :return: """ data = CommonUtility.get_request_data() valid, schema = APIValidator.validate_cancel_parking_slot_booking_api(data) if not valid: return schema, ResponseInfo.CODE_BAD_REQUEST booking_id = data["booking_id"] cancelled = ParkingSlotController.cancel_parking_slot_booking(booking_id) response = Response(ResponseInfo.CODE_SUCCESS, ResponseInfo.MESSAGE_BOOKING_CANCELLED) return jsonify(response.to_dict())
def create_parking_slot_api(): """ Create a new parking area :return: """ data = CommonUtility.get_request_data() valid, schema = APIValidator.validate_create_parking_slot_api(data) if not valid: return schema, ResponseInfo.CODE_BAD_REQUEST parking_area_id = data["parking_area_id"] parking_slot = ParkingSlotController.create_parking_slot(parking_area_id) if parking_slot: data = {ConstantsUtility.PARKING_SLOT: parking_slot} response = Response(ResponseInfo.CODE_SUCCESS, ResponseInfo.MESSAGE_SUCCESS, data=data) return jsonify(response.to_dict())
def register_user(): """ Required parameters : email, password :return: """ data = CommonUtility.get_request_data() valid, schema = APIValidator.validate_register_user_api(data) if not valid: return schema, 400 email = data["email"] password = data["password"] user = UserController.register_user(email, password) if user: response = Response(ResponseInfo.CODE_SUCCESS, ResponseInfo.MESSAGE_SUCCESS) return jsonify(response.to_dict())
def create_parking_area_api(): """ Create a new parking area :return: """ data = CommonUtility.get_request_data() valid, schema = APIValidator.validate_create_parking_area_api(data) if not valid: return schema, 400 name = data["name"] image = data.get("image") parking_area = ParkingAreaController.create_parking_area(name, image) if parking_area: data = { ConstantsUtility.PARKING_AREA: parking_area } response = Response(ResponseInfo.CODE_SUCCESS, ResponseInfo.MESSAGE_SUCCESS, data=data) return jsonify(response.to_dict())
def book_parking_slot_api(): """ Book a parking slot :return: """ data = CommonUtility.get_request_data() valid, schema = APIValidator.validate_book_parking_slot_api(data) if not valid: return schema, ResponseInfo.CODE_BAD_REQUEST parking_slot_id = data[ConstantsUtility.PARKING_SLOT_ID] start_time = data[ConstantsUtility.START_TIME] end_time = data[ConstantsUtility.END_TIME] booking_details = ParkingSlotController.book_parking_slot( parking_slot_id, start_time, end_time) if booking_details: data = {ConstantsUtility.BOOKING_DETAILS: booking_details} response = Response(ResponseInfo.CODE_SUCCESS, ResponseInfo.MESSAGE_SUCCESS, data=data) return jsonify(response.to_dict())
def get_all_slots_by_area_id(area_id): parking_slots = ParkingSlot.query.filter_by(parking_area_id=area_id).all() return CommonUtility.convert_to_dict(parking_slots)
def get_parking_area_by_id(area_id): area = ParkingArea.query.filter_by(id=area_id).first() if area: return CommonUtility.convert_to_dict(area)
def get_all_parking_areas(): parking_areas = ParkingArea.query.all() return CommonUtility.convert_to_dict(parking_areas)
def get_slot_by_id(slot_id): slot = ParkingSlot.query.filter_by(id=slot_id).first() if slot: return CommonUtility.convert_to_dict(slot)