def test_it_should_return_error_if_date_is_invalid(client): user = User(full_name='John Ade', phone_number='(324) 324 4234', address='1, afsfad, asdfasf.', email='*****@*****.**', password='******') user.save() token = user.generate_token() flight = Flight( **{ 'estimated_arrival_time': '2014-12-22T03:12:58.019077+00:00', 'airline': 'Arik', 'departure_time': '2014-12-22T03:12:58.019077+00:00', 'fare': 50000, 'max_capacity': 1, 'destination': 'Enugu', 'origin': 'Calabar' }) flight.save() response = client.get('/v1/flights/{}/bookings?date=2012'.format( str(flight.id)), headers={'Authorization': 'Bearer ' + token}) response_body = json.loads(response.data) assert response.status_code == 400 assert response_body == { 'status': 'error', 'message': 'Date should be in YYYY-MM-DD format' }
def test_it_should_return_flights_if_params_are_invalid(client): user = User(full_name='John Ade', phone_number='(324) 324 4234', address='1, afsfad, asdfasf.', email='*****@*****.**', password='******') user.save() token = user.generate_token() flight = Flight( **{ 'estimated_arrival_time': '2014-12-22T03:12:58.019077+00:00', 'airline': 'Arik', 'departure_time': '2014-12-22T03:12:58.019077+00:00', 'fare': 50000, 'max_capacity': 1, 'destination': 'Enugu', 'origin': 'Calabar' }) flight.save() response = client.get('/v1/flights?limit=', headers={'Authorization': 'Bearer ' + token}) response_body = json.loads(response.data) assert response.status_code == 200 assert response_body['status'] == 'success' assert any(response_flight['id'] == str(flight.id) for response_flight in response_body['data']['flights']) assert 'current_page' in response_body['data']['meta'] assert 'limit' in response_body['data']['meta'] assert 'total_items' in response_body['data']['meta'] assert 'no_of_pages' in response_body['data']['meta']
def test_it_should_return_422_if_flight_is_full(client): user = User(full_name='John Ade', phone_number='(324) 324 4234', address='1, afsfad, asdfasf.', email='*****@*****.**', password='******') user.save() token = user.generate_token() flight = Flight( **{ 'estimated_arrival_time': '2014-12-22T03:12:58.019077+00:00', 'airline': 'Arik', 'departure_time': '2014-12-22T03:12:58.019077+00:00', 'fare': 50000, 'max_capacity': 1, 'destination': 'Enugu', 'origin': 'Calabar' }) flight.save() flight_ticket = FlightTicket(user=user, flight=flight) flight_ticket.save() response = client.post('/v1/flights/{}/book'.format(str(flight.id)), data=json.dumps({}), headers={'Authorization': 'Bearer ' + token}) response_body = json.loads(response.data) assert response.status_code == 422 assert response_body == { 'status': 'error', 'message': 'Flight is full and cannot be booked' }
def test_it_should_create_flight_ticket_if_booking_is_successful(client): user = User(full_name='John Ade', phone_number='(324) 324 4234', address='1, afsfad, asdfasf.', email='*****@*****.**', password='******') user.save() token = user.generate_token() flight = Flight( **{ 'estimated_arrival_time': '2014-12-22T03:12:58.019077+00:00', 'airline': 'Arik', 'departure_time': '2014-12-22T03:12:58.019077+00:00', 'fare': 50000, 'max_capacity': 40, 'destination': 'Enugu', 'origin': 'Calabar' }) flight.save() response = client.post('/v1/flights/{}/book'.format(str(flight.id)), data=json.dumps({}), headers={'Authorization': 'Bearer ' + token}) response_body = json.loads(response.data) assert response.status_code == 201 assert response_body['status'] == 'success' assert response_body['data']['flight_ticket']['flight']['id'] == str( flight.id) assert 'ticket_number' in response_body['data']['flight_ticket'] assert 'password' not in response_body['data']['flight_ticket']['user']
def post(self): request_data = request.get_json(force=True) data = self.flight_schema.load(request_data) flight = Flight(**data) flight.save() return { 'status': 'success', 'data': { 'flight': self.flight_schema.dump(flight) } }, 201
def remind_users_of_upcoming_flights(): sg = SendGridAPIClient(env_config('SENDGRID_KEY')) tomorrow = datetime.now() + timedelta(days=1) pending_flights = Flight.objects( departure_time__gte=tomorrow.replace(hour=0, minute=0), departure_time__lte=tomorrow.replace(hour=23, minute=59), status=flight_statuses.PENDING ) booked_tickets = FlightTicket.objects( flight__in=pending_flights ) for ticket in booked_tickets: message = { 'personalizations': [ { 'to': [ { 'email': ticket.user.email } ], 'subject': 'Flight reminder' } ], 'from': { 'email': '*****@*****.**', 'name': 'Flight Booker' }, 'content': [ { 'type': 'text/plain', 'value': ''' Hi {0}, \n\nThis is a reminder of your flight scheduled to depart tomorrow by {1}. '''.format( ticket.user.full_name, ticket.flight.departure_time.strftime('%-I:%M %p') ) } ] } sg.send(message)