def test_update_meal_item_endpoint_right_permission(self):
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='update_meal_item', role_id=role.id)
        UserRoleFactory.create(user_id=user_id, role_id=role.id)

        meal_item = MealItemFactory.create()
        data = {
            'mealName': 'Jollof Rice',
            'description': 'tomato sautee rice',
            'mealType': 'protein',
            'image': 'a new image link'
        }
        response = self.client().put(self.make_url('/meal-items/{}'.format(
            meal_item.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['mealItem']['name'], data['mealName'])
        self.assertEqual(payload['mealItem']['description'],
                         data['description'])
        self.assertEqual(payload['mealItem']['mealType'], data['mealType'])
        self.assertEqual(payload['mealItem']['image'], data['image'])
    def test_invalid_update(self):
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="update_user_employment_history",
                                 role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)

        user_emp_data = UserEmploymentFactory.create()
        user_emp_data.save()

        data = {
            "user_id": user_id,
            "user_employment_id": user_emp_data.id,
            "institution_name": user_emp_data.institution_name,
            "job_title": user_emp_data.job_title,
            "employment_type": user_emp_data.employment_type,
            "institution_url": user_emp_data.institution_url,
            "institution_city": user_emp_data.institution_city,
            "institution_country": user_emp_data.institution_country,
            "institution_size": user_emp_data.institution_size,
            "work_summary": user_emp_data.work_summary,
            "accomplishments": user_emp_data.accomplishments,
            "start_date": str(user_emp_data.start_date),
            "end_date": str(user_emp_data.end_date),
            "is_current": user_emp_data.is_current,
        }
        response = self.client().put(
            self.make_url("/user_employment_history/1000"),
            data=self.encode_to_json_string(data),
            headers=self.headers(),
        )
        self.assert400(response)
    def test_get_specific_user_skill_endpoint(self):
        role = RoleFactory.create()
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="view_user_skill", role=role)
        UserRoleFactory.create(user_id=user_id, role=role)

        user_skill = UserSkillFactory.create(skill=self.skill_four)
        user_skill.save()
        response = self.client().get(
            self.make_url("/user_skill/user-single/{}".format(user_skill.id)),
            headers=self.headers(),
        )
        response_json = self.decode_from_json_string(response.data.decode("utf-8"))

        payload = response_json["payload"]

        self.assert200(response)
        self.assertJSONKeyPresent(payload, "user_skill")
        self.assertJSONKeysPresent(payload["user_skill"], "skill_level", "years")
        self.assertEqual(payload["user_skill"]["id"], user_skill.id)
        self.assertEqual(
            payload["user_skill"]["skill_level"],
            user_skill.skill_level,
        )
        self.assertEqual(payload["user_skill"]["years"], user_skill.years)
Example #4
0
    def test_get_specific_skill_category_endpoint(self):
        role = RoleFactory.create()
        user_id = BaseTestCase.user_id()
        skills_category = SkillCategoryFactory.create()
        skills_category.save()
        PermissionFactory.create(keyword="view_skills_categories", role=role)
        UserRoleFactory.create(user_id=user_id, role=role)

        response = self.client().get(
            self.make_url("/skills_categories/{}".format(skills_category.id)),
            headers=self.headers(),
        )

        response_json = self.decode_from_json_string(
            response.data.decode("utf-8"))
        payload = response_json["payload"]

        self.assert200(response)
        self.assertJSONKeyPresent(payload, "skills_category")
        self.assertJSONKeysPresent(payload["skills_category"], "name", "help")
        self.assertEqual(payload["skills_category"]["id"], skills_category.id)
        self.assertEqual(payload["skills_category"]["name"],
                         skills_category.name)
        self.assertEqual(payload["skills_category"]["help"],
                         skills_category.help)
    def test_create_user_skill_endpoint(self):
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword="create_user_skill", role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)
        user_proj_data = UserSkillFactoryFake.build(skill_id=self.skill_two.id)
        data = {
            "user_id": user_id,
            "skill_id": user_proj_data.skill_id,
            "skill_level": user_proj_data.skill_level,
            "years": user_proj_data.years,
        }
        response = self.client().post(
            self.make_url("/user_skill/"),
            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["user_skill"]["skill_id"],
            user_proj_data.skill_id,
        )
        self.assertEqual(payload["user_skill"]["years"], user_proj_data.years)
Example #6
0
    def test_list_menu_range_endpoint_with_right_permission_wrong_period(self):
        location = LocationFactory()
        meal_item_repo = MealItemRepo()
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_menu', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)
        current_date = datetime.now().date()
        start_date = current_date.strftime('%Y-%m-%d')
        end_date = (datetime.now().date() +
                    timedelta(days=7)).strftime('%Y-%m-%d')

        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)
        MenuFactory.create_batch(5,
                                 side_items=side_meal_item.id,
                                 protein_items=protein_meal_item.id)

        response = self.client() \
            .get(self.make_url(f'/admin/menus/wrong_period/{start_date}/{end_date}'), headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))

        self.assert400(response)
        self.assertEqual(response_json['msg'],
                         'Provide valid meal period and date range')
    def test_get_specific_user_project_endpoint(self):
        role = RoleFactory.create()
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="view_user_project", role=role)
        UserRoleFactory.create(user_id=user_id, role=role)

        user_project = UserProjectFactory.create()
        user_project.save()
        response = self.client().get(
            self.make_url("/user_project/user-single/{}".format(
                user_project.id)),
            headers=self.headers(),
        )
        response_json = self.decode_from_json_string(
            response.data.decode("utf-8"))

        payload = response_json["payload"]

        self.assert200(response)
        self.assertJSONKeyPresent(payload, "user_project")
        self.assertJSONKeysPresent(payload["user_project"], "project_name",
                                   "project_url")
        self.assertEqual(payload["user_project"]["id"], user_project.id)
        self.assertEqual(
            payload["user_project"]["project_name"],
            user_project.project_name,
        )
        self.assertEqual(payload["user_project"]["project_url"],
                         user_project.project_url)
Example #8
0
    def test_update_user_education_endpoint(self):
        RoleFactory.create()
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="update_user_education", role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)
        user_emp_data = UserEducationFactory.create()
        user_emp_data.save()
        data = {
            "user_id": user_id,
            "user_education_id": user_emp_data.id,
            "institution_name": user_emp_data.institution_name,
            "course_name": user_emp_data.course_name,
            "degree_earned": user_emp_data.degree_earned,
            "accomplishments": user_emp_data.accomplishments,
            "institution_city": user_emp_data.institution_city,
            "institution_country": user_emp_data.institution_country,
            "start_date": str(user_emp_data.start_date),
            "end_date": str(user_emp_data.end_date),
        }
        response = self.client().put(
            self.make_url("/user_education/{}".format(user_emp_data.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["user_education"]["institution_name"],
                         data["institution_name"])
Example #9
0
    def test_invalid_update(self):
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="update_user_education", role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)

        user_emp_data = UserEducationFactory.create()
        user_emp_data.save()

        data = {
            "user_id": user_id,
            "user_education_id": user_emp_data.id,
            "institution_name": user_emp_data.institution_name,
            "course_name": user_emp_data.course_name,
            "degree_earned": user_emp_data.degree_earned,
            "accomplishments": user_emp_data.accomplishments,
            "institution_city": user_emp_data.institution_city,
            "institution_country": user_emp_data.institution_country,
            "start_date": str(user_emp_data.start_date),
            "end_date": str(user_emp_data.end_date),
        }
        response = self.client().put(
            self.make_url("/user_education/1000"),
            data=self.encode_to_json_string(data),
            headers=self.headers(),
        )
        self.assert400(response)
    def test_list_order_by_date_endpoint(self, mock_andela_service):
        OrderFactory.create_batch(3)
        book_date = (date.today() + timedelta(days=1)).strftime('%Y-%m-%d')
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_orders', role_id=role.id)
        UserRoleFactory.create(user_id=user_id, role_id=role.id)

        mock_andela_service.return_value = {
            'id': user_id,
            'first_name': fake.first_name(),
            'last_name': fake.last_name()
        }

        response = self.client().get(self.make_url('/orders/2008-11-20'),
                                     headers=self.headers())
        self.assert200(response)
        self.assertEqual(
            len(loads(response.data, encoding='utf-8')['payload']['orders']),
            0)

        response1 = self.client().get(self.make_url(
            '/orders/{}'.format(book_date)),
                                      headers=self.headers())
        self.assert200(response1)
        self.assertEqual(
            len(loads(response1.data, encoding='utf-8')['payload']['orders']),
            3)
    def test_get_all_orders_by_user_id_endpoint(self, mock_andela_service):
        orders = OrderFactory.create_batch(3)
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()

        mock_andela_service.return_value = {
            'id': user_id,
            'first_name': fake.first_name(),
            'last_name': fake.last_name()
        }

        for order in orders:
            order.user_id = user_id
        PermissionFactory.create(keyword='view_orders', role_id=role.id)
        UserRoleFactory.create(user_id=user_id, role_id=role.id)

        mock_andela_service.return_value = {
            'id': user_id,
            'first_name': fake.first_name(),
            'last_name': fake.last_name()
        }

        response = self.client().get(self.make_url(
            '/orders/user/{}'.format(user_id)),
                                     headers=self.headers())
        self.assert200(response)
        self.assertEqual(
            len(loads(response.data, encoding='utf-8')['payload']['orders']),
            3)
    def test_update_user_skill_endpoint(self):
        RoleFactory.create()
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="update_user_skill", role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)
        user_proj_data = UserSkillFactory.create(skill=self.skill_four)
        user_proj_data.save()
        data = {
            "user_id": user_id,
            "user_skill_id": user_proj_data.id,
            "skill_id": user_proj_data.skill_id,
            "skill_level": user_proj_data.skill_level,
            "years": user_proj_data.years,
        }
        response = self.client().put(
            self.make_url("/user_skill/{}".format(user_proj_data.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["user_skill"]["skill_id"], data["skill_id"])
Example #13
0
    def test_create_meal_item_endpoint_with_right_permission(self):
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        location = LocationFactory.create()
        PermissionFactory.create(keyword='create_meal_item', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)
        meal_item = MealItemFactory.build(location=location)

        headers = self.headers()
        headers.update({'X-Location': location.id})

        data = {'mealName': meal_item.name, 'image': meal_item.image,
                'mealType': meal_item.meal_type}
        response = self.client().post(
            self.make_url('/meal-items/'), 
            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.assertJSONKeyPresent(response_json, 'payload')
        self.assertEqual(payload['mealItem']['name'], meal_item.name)
        self.assertEqual(payload['mealItem']['image'], meal_item.image)
        self.assertEqual(payload['mealItem']['mealType'], meal_item.meal_type)
Example #14
0
    def test_list_meal_item_endpoint_correct_sort_order(self):
        # Create Three Dummy Vendors
        location = LocationFactory.create()
        meals = MealItemFactory.create_batch(3, location=location)

        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_meal_item', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)

        headers = self.headers()
        headers.update({'X-Location': location.id})

        response = self.client().get(self.make_url('/meal-items/'), headers=headers)
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        payload = response_json['payload']

        meals_sorted_by_name = sorted(
            [meal.name for meal in meals]
        )

        meals_returned = [meal.get("name") for meal in payload['mealItems']]

        self.assert200(response)
        self.assertEqual(len(payload['mealItems']), 3)
        self.assertJSONKeysPresent(payload['mealItems'][0], 'name', 'mealType', 'image')
        self.assertEqual(meals_returned[0], meals_sorted_by_name[0])
        self.assertEqual(meals_returned[1], meals_sorted_by_name[1])
        self.assertEqual(meals_returned[2], meals_sorted_by_name[2])
Example #15
0
    def test_update_skill_categories_endpoint(self):

        RoleFactory.create()
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()
        skills_category = SkillCategoryFactory.create()
        skills_category.save()

        PermissionFactory.create(keyword="update_skills_categories",
                                 role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)
        data = {
            "name": "Super Admin",
            "help": "help",
            "skill_category_id": skills_category.id,
        }
        response = self.client().put(
            self.make_url("/skills_categories/{}".format(skills_category.id)),
            data=self.encode_to_json_string(data),
            headers=self.headers(),
        )
        print(response)
        response_json = self.decode_from_json_string(
            response.data.decode("utf-8"))
        print(response_json)
        payload = response_json["payload"]

        self.assert200(response)
        self.assertEqual(payload["skills_category"]["name"], data["name"])
Example #16
0
    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)
Example #17
0
    def test_list_menu_range_endpoint_with_right_permission(self):
        """ Test that users with right permission can view list of menu with date range """
        location = LocationFactory.create()
        meal_item_repo = MealItemRepo()
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_menu', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)
        current_date = datetime.now().date()
        start_date = current_date.strftime('%Y-%m-%d')
        end_date = (datetime.now().date() +
                    timedelta(days=7)).strftime('%Y-%m-%d')

        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)
        MenuFactory.create_batch(5,
                                 side_items=side_meal_item.id,
                                 protein_items=protein_meal_item.id,
                                 location=location)

        response = self.client() \
            .get(self.make_url(f'/admin/menus/{MealPeriods.lunch}/{start_date}/{end_date}'), headers=self.headers())

        self.assert200(response)
Example #18
0
    def test_list_menu_endpoint_with_right_permission(self):
        """Test that users with the right permission can view list of menus"""

        meal_item_repo = MealItemRepo()
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_menu', role_id=role.id)
        UserRoleFactory.create(user_id=user_id, role_id=role.id)
        the_date = datetime.now().date()
        current_date = the_date.strftime('%Y-%m-%d')

        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)
        MenuFactory.create_batch(5,
                                 side_items=side_meal_item.id,
                                 protein_items=protein_meal_item.id,
                                 date=the_date)

        response = self.client().get(
            self.make_url(f'/admin/menus/{MealPeriods.lunch}/{current_date}'),
            headers=self.headers())

        self.assert200(response)
    def test_delete_engagement_with_associated_menus(self):
        datetime.now().date()
        location = LocationFactory()
        location.save()
        meal_item = MealItemFactory(location=location)
        vendor = VendorFactory(location=location)
        engagement = VendorEngagementFactory.create(location=location, vendor=vendor)
        menu_repo = MenuRepo()
        menu_repo.new_menu(
            date='2018-10-15',
            meal_period='lunch',
            main_meal_id=meal_item.id,
            allowed_side=1,
            allowed_protein=1,
            side_items=[2],
            protein_items=[3],
            vendor_engagement_id=engagement.id,
            location_id=location.id
        )

        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='delete_engagement', role=role)
        UserRoleFactory.create(user_id=user_id, role=role, location_id=location.id)

        response = self.client().delete(self.make_url(f'/engagements/{engagement.id}'), headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))

        self.assert400(response)

        self.assertEqual(response_json['msg'], 'This engagement cannot be deleted because it has a child object')
    def test_invalid_update(self):
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="update_user_project", role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)

        user_proj_data = UserProjectFactory.create()
        user_proj_data.save()

        data = {
            "user_id": user_id,
            "user_project_id": user_proj_data.id,
            "project_name": user_proj_data.project_name,
            "project_url": user_proj_data.project_url,
            "start_date": str(user_proj_data.start_date),
            "end_date": str(user_proj_data.end_date),
            "is_current": user_proj_data.is_current,
        }
        response = self.client().put(
            self.make_url("/user_project/1000"),
            data=self.encode_to_json_string(data),
            headers=self.headers(),
        )
        self.assert400(response)
    def test_update_user_project_endpoint(self):
        RoleFactory.create()
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="update_user_project", role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)
        user_proj_data = UserProjectFactory.create()
        user_proj_data.save()
        data = {
            "user_id": user_id,
            "user_project_id": user_proj_data.id,
            "project_name": user_proj_data.project_name,
            "project_url": user_proj_data.project_url,
            "project_description": user_proj_data.project_description,
            "start_date": str(user_proj_data.start_date),
            "end_date": str(user_proj_data.end_date),
            "is_current": user_proj_data.is_current,
        }
        response = self.client().put(
            self.make_url("/user_project/{}".format(user_proj_data.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["user_project"]["project_name"],
                         data["project_name"])
    def test_list_order_by_page_endpoint(self, mock_andela_service):
        OrderFactory.create_batch(3)
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_orders', role_id=role.id)
        UserRoleFactory.create(user_id=user_id, role_id=role.id)

        mock_andela_service.return_value = {
            'id': user_id,
            'first_name': fake.first_name(),
            'last_name': fake.last_name()
        }

        response = self.client().get(self.make_url('/orders/'),
                                     query_string={
                                         'per_page': 2,
                                         'page': 1
                                     },
                                     headers=self.headers())
        decoded = loads(response.data, encoding='utf-8')
        self.assert200(response)
        self.assertEqual(decoded['payload']['meta']['current_page'], 1)
        self.assertEqual(len(decoded['payload']['orders']), 2)

        response1 = self.client().get(self.make_url('/orders/'),
                                      query_string={
                                          'per_page': 2,
                                          'page': 2
                                      },
                                      headers=self.headers())
        self.assert200(response1)
        decoded1 = loads(response1.data, encoding='utf-8')
        self.assertEqual(decoded1['payload']['meta']['current_page'], 2)
        self.assertEqual(len(decoded1['payload']['orders']), 1)
Example #23
0
    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_list_users_endpoint_without_users(self):
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_users', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)

        response = self.client().get(self.make_url('/users/'), headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 404)
    def test_delete_engagement_endpoint_with_wrong_vendor_id(self):
        VendorEngagementFactory.create()

        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='delete_engagement', role=role)
        UserRoleFactory.create(user_id=user_id, role_id=role.id)

        response = self.client().delete(self.make_url(f'/engagement/-576A'), headers=self.headers())

        self.assert404(response)
Example #26
0
    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_collect_order_not_valid_endpoint(self):
        order = OrderFactory.create()
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        order.meal_period, order.user_id = 'lunch', user_id
        PermissionFactory.create(keyword='view_orders', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)

        data={'userId': user_id, 'orderType': 'breakfast', 'orderDate': order.date_booked_for.strftime('%Y-%m-%d')}
        response = self.client().post(self.make_url('/orders/collect'), data=self.encode_to_json_string(data) , headers=self.headers())
        self.assert400(response)
    def test_delete_engagement_endpoint_without_right_permission(self):
        engagement = VendorEngagementFactory.create()

        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='delete_engagement', role_id=1000)
        UserRoleFactory.create(user_id=user_id, role_id=role.id)

        response = self.client().delete(self.make_url(f'/engagements/{engagement.id}'), headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        self.assert401(response)
        self.assertEqual(response_json['msg'], 'Access Error - No Permission Granted')
    def test_delete_user_endpoint_with_wrong_user_id(self):
        user = UserFactory.create()
        user.save()

        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='delete_user', role=role)
        UserRoleFactory.create(user_id=user_id, role_id=user.id)

        response = self.client().delete(self.make_url(f'/userrs/-576A/'), headers=self.headers())

        self.assert404(response)
	def test_delete_permission_endpoint_with_wrong_permission_id(self):
		permission_repo = PermissionRepo()
		role1 = RoleFactory.create(name='admin')
		permission_repo.new_permission(role1.id, 'name-1', 'keyword-1')
		user_id = BaseTestCase.user_id()
		PermissionFactory.create(keyword='delete_permissions', role_id=role1.id)
		UserRoleFactory.create(user_id=user_id, role_id=role1.id)
		response = self.client().delete(self.make_url(f'/roles/permissions/576'), headers=self.headers())
		response_json = self.decode_from_json_string(response.data.decode('utf-8'))

		self.assert404(response)
		self.assertEqual(response_json['msg'], 'Invalid or incorrect permission id provided')