def test_create_menu_endpoint_with_wrong_permission(self): """ Test for creation of a new menu without permmission """ role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='delete_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) menu = MenuFactory.build() main_meal_item = MealItemFactory.build() side_meal_item = MealItemFactory.build() protein_meal_item = MealItemFactory.build() vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': menu.allowed_side, 'allowedProtein': menu.allowed_protein, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } response = self.client().post(self.make_url('/admin/menus/'), \ data=self.encode_to_json_string(data), headers=self.headers()) self.assert400(response)
def test_create_menu_endpoint_with_right_permission_and_input(self): """Test for creation of new menu""" location = LocationFactory() menu = MenuFactory.build() main_meal_item = MealItemFactory.create() side_meal_item = MealItemFactory.create() protein_meal_item = MealItemFactory.create() vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor=vendor) vendor_engagement.save() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='create_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': menu.allowed_side, 'allowedProtein': menu.allowed_protein, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } headers = self.headers() headers.update({'X-Location': location.id}) response = self.client().post(self.make_url('/admin/menus/'), \ data=self.encode_to_json_string(data), headers=headers) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 201) self.assertJSONKeysPresent(payload, 'menu') self.assertJSONKeysPresent(payload['menu'], 'mainMeal', 'proteinItems', 'sideItems', 'allowedProtein', 'allowedSide', 'date', 'id', 'mealPeriod', 'timestamps', 'vendorEngagementId') self.assertEqual(payload['menu']['vendorEngagementId'], vendor_engagement.id) self.assertEqual(payload['menu']['mealPeriod'], menu.meal_period) self.assertEqual(payload['menu']['mainMealId'], main_meal_item.id) self.assertEqual(payload['menu']['allowedSide'], menu.allowed_side) self.assertEqual(payload['menu']['allowedProtein'], menu.allowed_protein)
def test_create_menu_endpoint_with_existing_main_meal_item(self): """Multiple menus with same same main meal id should not exist on the same day""" location = LocationFactory.create() main_meal_item = MealItemFactory.create(location=location) side_meal_item = MealItemFactory.create(location=location) protein_meal_item = MealItemFactory.create(location=location) headers = self.headers() headers.update({'X-Location': location.id}) menu = MenuFactory.build(location=location) menu.save() vendor = VendorFactory.build(location=location) vendor_engagement = VendorEngagementFactory.build(vendor=vendor, location=location) vendor_engagement.save() data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': menu.allowed_side, 'allowedProtein': menu.allowed_protein, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id, 'location_id': location.id } existing_menu = MenuRepo().new_menu( menu.date.strftime('%Y-%m-%d'), menu.meal_period, main_meal_item.id, menu.allowed_side, menu.allowed_protein, [side_meal_item.id], [protein_meal_item.id], vendor_engagement.id, location.id) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='create_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) response = self.client().post(self.make_url('/admin/menus/'), \ data=self.encode_to_json_string(data), headers=headers) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 400) self.assertEqual( response_json['msg'], 'You can\'t create multiple menus with same main item on the same day' )
def test_create_menu_endpoint_with_wrong_protein_items(self): """Test for creation of new menu with wrong protein items""" menu = MenuFactory.build() main_meal_item = MealItemFactory.create() side_meal_item = MealItemFactory.create() protein_meal_item = MealItemFactory.create() vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='create_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': menu.allowed_side, 'allowedProtein': menu.allowed_protein, 'sideItems': [side_meal_item.id], 'proteinItems': [100], 'vendorEngagementId': vendor_engagement.id } response = self.client().post(self.make_url('/admin/menus/'), \ data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 400) self.assertEqual( response_json['msg'], 'Bad Request - proteinItems contains invalid id(s) for meal_item table ' )