def test_user_method_raises_expection_when_not_auth_provided(self): with self.assertRaises(Exception) as e: Auth.user() self.assertEqual(e.exception.args[0], 'Authorization Header is Expected')
def test_user_method_raises_expection_with_an_invalid_token( self, mock_get_token): with self.assertRaises(Exception) as e: mock_get_token.return_val = 'invalid_token' Auth.user() self.assertEqual(e.exception.args[0], 'Error Decoding')
def create_vendor_rating(self): '''Adds a vendor rating during a specific engagement''' (comment, rating, service_date, channel, engagement_id) = self.request_params('comment', 'rating', 'serviceDate', 'channel', 'engagementId') main_meal_id = None user_id = Auth.user('id') vendor_id = self.vendor_engagement_repo.get(engagement_id).vendor_id if self.vendor_repo.get(vendor_id): service_date = datetime.strptime(service_date, '%Y-%m-%d') rating = self.vendor_rating_repo.new_rating( vendor_id, user_id, rating, service_date, RatingType.engagement, engagement_id, engagement_id, main_meal_id, channel, comment) self.vendor_repo.update_vendor_average_rating(vendor_id) rtng = rating.serialize() return self.handle_response('Rating created', payload={'rating': rtng}, status_code=201) return self.handle_response('Invalid vendor_id provided', status_code=400)
def create_order_rating(self): '''Adds a order rating during a specific engagement ''' (order_id, comment, rating, channel) = self.request_params('orderId', 'comment', 'rating', 'channel') user_id = Auth.user('id') order = self.order_repo.get(order_id) if order: menu = self.menu_repo.get(order.menu_id) if menu: vendor_id = menu.vendor_engagement.vendor_id engagement_id = menu.vendor_engagement.id rating = self.vendor_rating_repo.new_rating( vendor_id, user_id, rating, RatingType.order, order_id, engagement_id, channel, comment) rating_obj = rating.serialize() if rating: updates = {} updates['has_rated'] = True updated_order = self.order_repo.update( order, **updates).serialize() return self.handle_response('Rating created', payload={'rating': rating_obj}, status_code=201) return self.handle_response('Invalid vendor_id provided', status_code=400)
def create_order(self): """ creates an order :return: order object """ user_id = Auth.user('id') location_id = Auth.get_location() date_booked_for, channel, meal_period, meal_items, menu_id = self.request_params( 'dateBookedFor', 'channel', 'mealPeriod', 'mealItems', 'menuId' ) if self.order_repo.user_has_order(user_id, date_booked_for, meal_period): return self.handle_response('You have already booked for this meal period.', status_code=400) location = LocationRepo().get(location_id) current_time = current_time_by_zone(location.zone) if datetime.strptime(date_booked_for, "%Y-%m-%d") < datetime.now(): return self.handle_response('You are not allowed to book for a date in the past', status_code=400) if int(current_time_by_zone(location.zone).strftime('%H')) > 15: if check_date_current_vs_date_for(current_time, datetime.strptime(date_booked_for, "%Y-%m-%d")): return self.handle_response('It is too late to book a meal for the selected date ', status_code=400) meal_object_items = self.meal_item_repo.get_meal_items_by_ids(meal_items) new_order = self.order_repo.create_order( user_id, date_booked_for, meal_object_items, location_id, menu_id, channel, meal_period).serialize() new_order['mealItems'] = [ item.to_dict(only=OrderController.default_meal_item_return_fields) for item in meal_object_items ] return self.handle_response('OK', payload={'order': new_order}, status_code=201)
def create_order_rating(self): """Adds a order or meal rating during a specific engagement """ (order_id, main_meal_id, engagement_id, comment, rating, service_date, channel) = self.request_params('orderId', 'mainMealId', 'engagementId', 'comment', 'rating', 'serviceDate', 'channel') if not (1 <= rating <= 5): return self.handle_response( 'Rating must be between 1 and 5, inclusive.', status_code=400) user_id = Auth.user('id') if not self.meal_repo.get(main_meal_id): return self.handle_response('Meal item with this id not found', status_code=400) engagement = self.vendor_engagement_repo.get(engagement_id) if not engagement: return self.handle_response('Engagement with this id is not found', status_code=400) vendor_id = engagement.vendor_id if order_id: rating_type = RatingType.order type_id = order_id order = self.order_repo.get(order_id) if not order: return self.handle_response('Order with this id is not found', status_code=400) if order.has_rated: return self.handle_response('This order has been rated', status_code=400) else: if (datetime.now() - datetime.strptime(service_date, '%Y-%m-%d')).days < 1: return self.handle_response( 'You can only rate meals of past days.', status_code=400) rating_type = RatingType.meal type_id = main_meal_id user_meal_rating = self.vendor_rating_repo.get_unpaginated( user_id=user_id, type_id=type_id, rating_type='meal') if user_meal_rating: return self.handle_response('You have already rated this meal', status_code=400) rating = self.vendor_rating_repo.new_rating( vendor_id, user_id, rating, datetime.strptime(service_date, '%Y-%m-%d'), rating_type, type_id, engagement_id, main_meal_id, channel, comment) self.vendor_repo.update_vendor_average_rating(vendor_id) if rating.id and rating_type == RatingType.order: updates = {'has_rated': True} self.order_repo.update(order, **updates) rating_obj = rating.serialize() return self.handle_response('Rating successful', payload={'rating': rating_obj}, status_code=201)
def test_user_method_return_list_of_user_data_based_on_supplied_keys(self): with self.app.test_request_context(path='/api/v1/vendors', method='GET', headers=self.headers()) as request: decoded = Auth.decode_token(self.get_valid_token()) values = Auth.user('id', 'first_name', 'last_name', 'email') id, first_name, last_name, email = values self.assertIsInstance(values, list) self.assertEquals(decoded['UserInfo']['id'], id) self.assertEquals(decoded['UserInfo']['first_name'], first_name) self.assertEquals(decoded['UserInfo']['last_name'], last_name) self.assertEquals(decoded['UserInfo']['email'], email)
def delete_order(self, order_id): order = self.order_repo.get(order_id) if order: if order.is_deleted: return self.handle_response('Order has already been deleted', status_code=400) if Auth.user('id') != order.user_id: return self.handle_response('You cannot delete an order that is not yours', status_code=403) updates = {} updates['is_deleted'] = True self.order_repo.update(order, **updates) return self.handle_response('Order deleted', payload={"status": "success"}) return self.handle_response('Invalid or incorrect order_id provided', status_code=400)
def create_vendor_rating(self): '''Adds a vendor rating during a specific engagement''' (vendor_id, comment, rating, channel, engagement_id) = self.request_params('vendorId', 'comment', 'rating', 'channel', 'engagementId') user_id = Auth.user('id') if self.vendor_repo.get(vendor_id): rating = self.vendor_rating_repo.new_rating( vendor_id, user_id, rating, RatingType.engagement, 0, engagement_id, channel, comment) rtng = rating.serialize() return self.handle_response('Rating created', payload={'rating': rtng}, status_code=201) return self.handle_response('Invalid vendor_id provided', status_code=400)
def update_vendor_rating(self, rating_id): """edits an existing rating""" rtng = self.vendor_rating_repo.get(rating_id) comment = self.get_json()['comment'] if rtng: if Auth.user( 'id' ) == rtng.user_id: #You cannot update someone else's rating updates = {} if comment: updates['comment'] = comment self.vendor_rating_repo.update(rtng, **updates) return self.handle_response( 'OK', payload={'rating': rtng.serialize()}) return self.handle_response( 'You are not allowed to update a rating that is not yours', status_code=403) return self.handle_response('Invalid or incorrect rating_id provided', status_code=404)
def user(self, *keys): return Auth.user(*keys)