def test_delete_order_endpoint_with_right_permission(self): user_id = BaseTestCase.user_id() meal = MealItemFactory.create() menu = MenuFactory.create() order_data = { 'user_id': user_id, 'date_booked_for': '2018-10-20', 'channel': 'web', 'meal_period': 'lunch', 'menu_id': menu.id, 'meal_items': [meal], 'location_id': 1 } order_repo = OrderRepo() order = order_repo.create_order(**order_data) response = self.client().delete(self.make_url(f'/orders/{order.id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['status'], 'success') self.assertEqual(response_json['msg'], 'Order deleted')
def test_already_deleted_order(self): user_id = BaseTestCase.user_id() meal = MealItemFactory.create() menu = MenuFactory.create() order_data = { 'user_id': user_id, 'date_booked_for': '2018-10-20', 'channel': 'web', 'meal_period': 'lunch', 'menu_id': menu.id, 'meal_items': [meal], 'location_id': 1 } order_repo = OrderRepo() order = order_repo.create_order(**order_data) self.client().delete(self.make_url(f'/orders/{order.id}'), headers=self.headers()) response = self.client().delete(self.make_url(f'/orders/{order.id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assert400(response) self.assertEqual(response_json['msg'], 'Order has already been deleted')
def test_list_menu_endpoint_with_invalid_date_fails(self): """Test that users with the wrong date fails""" meal_item_repo = MealItemRepo() location = LocationFactory.create() main_meal_item = meal_item_repo.new_meal_item(name="main1", image="image1", meal_type="main", location_id=location.id) side_meal_item = meal_item_repo.new_meal_item(name="side1", image="image11", meal_type="side", location_id=location.id) protein_meal_item = meal_item_repo.new_meal_item( name="protein1", image="image12", meal_type="protein", location_id=location.id) menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=str(side_meal_item.id), protein_items=str(protein_meal_item.id)) date = '201502-24' response = self.client().get( self.make_url(f'/menus/{menu.meal_period}/{date}/{date}'), headers=self.headers()) self.assert400(response)
def test_create_order_with_invalid_details_endpoint(self): items = [item.id for item in MealItemFactory.create_batch(4)] menu = MenuFactory.create() LocationFactory.create(id=1, zone='+1') data = { 'dateBookedFor': (date.today() + timedelta(days=-3)).strftime('%Y-%m-%d'), 'channel': 'web', 'mealPeriod': 'lunch', 'menuId': menu.id } # If we don't add meal items response = self.client().post(self.make_url('/orders/'), data=self.encode_to_json_string(data), headers=self.headers()) self.assert400(response) # If we book in the past data.update({'mealItems': items}) response1 = self.client().post(self.make_url('/orders/'), data=self.encode_to_json_string(data), headers=self.headers()) self.assert400(response1) data.update({ 'dateBookedFor': (date.today() + timedelta(days=2)).strftime('%Y-%m-%d') }) response2 = self.client().post(self.make_url('/orders/'), data=self.encode_to_json_string(data), headers=self.headers()) self.assertEqual(response2.status_code, 201)
def test_create_order_with_valid_details_endpoint(self): LocationFactory.create(id=1, zone='+1') order = OrderFactory.create() menu = MenuFactory.create() meal_item1 = MealItemFactory.create() meal_item2 = MealItemFactory.create() meal_item3 = MealItemFactory.create() meal_item1.meal_type = MealTypes.protein meal_item2.meal_type = MealTypes.main meal_items = [meal_item1.id, meal_item2.id, meal_item3.id] data = {'userId': order.user_id, 'dateBookedFor': (date.today() + timedelta(days=2)).strftime('%Y-%m-%d'), 'dateBooked': order.date_booked.strftime('%Y-%m-%d'), 'channel': 'web', 'menuId': menu.id, 'mealPeriod': order.meal_period, 'mealItems': meal_items} response = self.client().post( self.make_url('/orders/'), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 201) self.assertJSONKeyPresent(response_json, 'payload') self.assertEqual(payload['order']['userId'], BaseTestCase.user_id()) self.assertEqual(payload['order']['channel'], 'web')
def test_list_menu_range_endpoint_succeeds(self): meal_item_repo = MealItemRepo() main_meal_item = meal_item_repo.new_meal_item(name="main1", image="image1", meal_type="main", location_id=1) side_meal_item = meal_item_repo.new_meal_item(name="side1", image="image11", meal_type="side", location_id=1) protein_meal_item = meal_item_repo.new_meal_item(name="protein1", image="image12", meal_type="protein", location_id=1) menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=str(side_meal_item.id), protein_items=str(protein_meal_item.id)) start_date = menu.vendor_engagement.start_date - timedelta(days=1) response = self.client().get(self.make_url( f'/menus/{menu.meal_period}/{start_date}/{menu.vendor_engagement.end_date}' ), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response_json['msg'], 'OK') self.assertEqual( response_json['payload']['menuList'][0]['menus'][0]['id'], menu.id)
def test_update_menu_endpoint_with_wrong_protein_item_values(self): location = LocationFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='update_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) meal_item_repo = MealItemRepo() main_meal_item = meal_item_repo.new_meal_item(name="main1", image="image1", meal_type="main", location_id=location.id) side_meal_item = meal_item_repo.new_meal_item(name="side1", image="image11", meal_type="side", location_id=location.id) protein_meal_item = meal_item_repo.new_meal_item( name="protein1", image="image12", meal_type="protein", location_id=location.id) vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) vendor_engagement.save() menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=side_meal_item.id, protein_items=protein_meal_item.id) menu.vendor_engagement_id = vendor_engagement.id menu.save() data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': 2, 'allowedProtein': 2, 'sideItems': [side_meal_item.id], 'proteinItems': [1000], 'vendorEngagementId': vendor_engagement.id } response = self.client().put(self.make_url('/admin/menus/{}'.format( menu.id)), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assert400(response) self.assertEqual( response_json['msg'], 'Bad Request - proteinItems contains invalid id(s) for meal_item table ' )
def test_update_menu_endpoint(self): """Test update of a menu""" location = LocationFactory() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='update_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) meal_item_repo = MealItemRepo() main_meal_item = meal_item_repo.new_meal_item(name="main1", image="image1", meal_type="main", location_id=location.id) side_meal_item = meal_item_repo.new_meal_item(name="side1", image="image11", meal_type="side", location_id=location.id) protein_meal_item = meal_item_repo.new_meal_item( name="protein1", image="image12", meal_type="protein", location_id=location.id) vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) vendor_engagement.save() menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=side_meal_item.id, protein_items=protein_meal_item.id, location=location) menu.save() data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': 2, 'allowedProtein': 2, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } response = self.client().put(self.make_url('/admin/menus/{}'.format( menu.id)), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['menu']['allowedProtein'], data['allowedProtein']) self.assertEqual(payload['menu']['allowedSide'], data['allowedSide'])
def test_update_menu_endpoint_with_wrong_permission(self): role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='wrong_permission', role_id=role.id) UserRoleFactory.create(user_id=user_id, role_id=role.id) meal_item_repo = MealItemRepo() main_meal_item = meal_item_repo.new_meal_item(name="main1", description="descr1", image="image1", meal_type="main", location_id=1) side_meal_item = meal_item_repo.new_meal_item(name="side1", description="descr11", image="image11", meal_type="side", location_id=1) protein_meal_item = meal_item_repo.new_meal_item(name="protein1", description="descr11", image="image12", meal_type="protein", location_id=1) vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=side_meal_item.id, protein_items=protein_meal_item.id) menu.vendor_engagement_id = vendor_engagement.id data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': 10000, 'allowedSide': 2, 'allowedProtein': 2, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } response = self.client().put(self.make_url('/admin/menus/{}'.format( menu.id)), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assert400(response) self.assertEqual(response_json['msg'], 'Access Error - Permission Denied')
def test_delete_menu_endpoint_without_right_permission(self): """Test that a user without permission to delete menu cannot successfully do so""" menu = MenuFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='delete_menu', role_id=100) UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete( self.make_url(f'/admin/menus/{menu.id}'), headers=self.headers()) self.assert400(response)
def test_delete_menu_endpoint_with_right_permission(self): """Test that a user with permission to delete menu can successfully do so""" menu = MenuFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='delete_menu', role_id=role.id) UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete( self.make_url(f'/admin/menus/{menu.id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['status'], "success")
def test_interactions_after_selecting_action_menu_list_with_menus(self, mock_json_loads, mock_menu_repo): mock_json_loads.return_value = action_selected_menu location = LocationFactory() side_meal_item = MealItemFactory(location=location, meal_type="side") side_meal_item.save() protein_meal_item = MealItemFactory(location=location, meal_type="protein") menu = MenuFactory.create( side_items=str(side_meal_item.id), protein_items=str(protein_meal_item.id) ) mock_menu_repo.return_value = [menu] response = self.client().post(self.make_url(f'/bot/interactions/'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert200(response) self.assertEqual(response_json['type'], 'interactive_message') self.assertEqual(type(response_json['actions']), list)
def test_delete_order_not_yours(self): location = LocationFactory.create() user_id = BaseTestCase.user_id() meal = MealItemFactory.create() menu = MenuFactory.create() order_data = { 'user_id': '-UTG654RfggtdI', 'date_booked_for': '2018-10-20', 'channel': 'web', 'meal_period': 'lunch', 'menu_id': menu.id, 'meal_items': [meal], 'location_id': location.id } order_repo = OrderRepo() order = order_repo.create_order(**order_data) response = self.client().delete(self.make_url(f'/orders/{order.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert403(response) self.assertEqual(response_json['msg'], 'You cannot delete an order that is not yours')
def test_all_vendor_comparison(self): vendor = VendorFactory.create() engagement = VendorEngagementFactory.create(vendor_id=vendor.id) menu = MenuFactory.create(vendor_engagement_id=engagement.id) OrderFactory.create(menu_id=menu.id) OrderFactory.create(menu_id=menu.id - 1) recent_date = datetime.datetime.now().date() + datetime.timedelta(7) VendorRatingFactory.create_batch(3, service_date=recent_date) response = self.client().get( self.make_url('/reports/?all_vendor_comparison=true'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 200) self.assertJSONKeyPresent(response_json, 'payload') self.assertEqual(type(payload), list)
def test_interactions_after_final_meal_selection(self, mock_json_loads, mock_user_info, mock_order_repo, mock_andela_service, mock_menu_repo, mock_meal_items, mock_post): location = LocationFactory.create() location.save() menu = MenuFactory.create() menu.save() final_selection['state'] = f'{menu.id}_breakfast_2019-02-19_order_{location.id}_1' mock_json_loads.return_value = final_selection mock_user_info.return_value = {'user': {'profile': {'email': '*****@*****.**'}}} mock_andela_service.return_value = {'id': 'victor_adukwu_andela_com', 'email': '*****@*****.**'} mock_menu_repo.return_value = menu order = OrderFactory.create(menu=menu, location=location) mock_order_repo.return_value = order mock_meal_items.return_values = MealItemFactory.create() response = self.client().post(self.make_url(f'/bot/interactions/'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert200(response) self.assertEqual(response_json['type'], 'dialog_submission') mock_post.assert_called_once()