Пример #1
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"])
Пример #2
0
    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"])
Пример #3
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"])
    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_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_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)
Пример #7
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')
Пример #8
0
    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)
Пример #9
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)
    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'])
Пример #11
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_get_single_rating(self):
        '''Test that users with the right permission can see details of a single rating vendor rating'''

        rating = VendorRatingFactory.create()
        rating_id = rating.id
        role = RoleFactory.create(name='Admin')
        permission = PermissionFactory.create(keyword='view_ratings',
                                              role_id=role.id)
        user_role = UserRoleFactory.create(user_id=rating.user_id,
                                           role_id=role.id)

        response = self.client().get(self.make_url(f'/ratings/{rating_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, 'rating')
        self.assertJSONKeysPresent(payload['rating'], 'vendorId', 'userId',
                                   'id', 'comment', 'rating', 'channel')

        self.assertEqual(payload['rating']['id'], rating_id)
        self.assertEqual(payload['rating']['userId'], rating.user_id)
        self.assertEqual(payload['rating']['vendorId'], rating.vendor_id)
        self.assertEqual(payload['rating']['comment'], rating.comment)
        self.assertEqual(payload['rating']['rating'], rating.rating)
        '''Search for a non-existing rating returns 400 error'''
        response = self.client().get(self.make_url('/ratings/100'),
                                     headers=self.headers())
        self.assert400(response)
    def test_list_vendor_ratings_endpoint(self):
        '''Test that users with the right permission can view list of vendor ratings'''

        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        vendor_id = VendorFactory.create().id
        permission = PermissionFactory.create(keyword='view_ratings',
                                              role_id=role.id)
        user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id)

        for i in range(5):
            vendor_rating = VendorRating(vendor_id=vendor_id,
                                         user_id=user_id,
                                         comment=f'comment-{i}',
                                         rating=f'{i}',
                                         channel='slack')
            vendor_rating.save()

        response = self.client().get(
            self.make_url(f'/ratings/vendor/{vendor_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(len(payload['ratings']), 5)
        self.assertJSONKeysPresent(payload['ratings'][0], 'vendorId', 'userId',
                                   'id', 'comment', 'rating', 'channel')
Пример #14
0
    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)
Пример #15
0
    def test_create_user_role_endpoint(self, mock_andela_get_user_by_email):
        location = LocationFactory.create()
        user = UserFactory()
        mock_andela_get_user_by_email.return_value = {'id': user.user_id}

        create_user_role('create_user_roles')

        new_role = RoleFactory.create()

        user_role_data = {
            'roleId': new_role.id,
            'emailAddress': self.user_email()
        }
        headers = self.headers()
        headers.update({'X-Location': location.id})
        response = self.client().post(
            self.make_url(f'/roles/user'),
            data=self.encode_to_json_string(user_role_data),
            headers=headers)

        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_json['msg'], 'OK')
        self.assertEqual(response_json['payload']['user_role']['roleId'],
                         new_role.id)
Пример #16
0
    def test_delete_role_endpoint_without_right_permission(self):
        role = RoleFactory.create()

        role1 = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='delete_roless', role_id=100)
        UserRoleFactory.create(user_id=user_id, role=role1)

        response = self.client().delete(self.make_url(f'/roles/{role.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_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)
Пример #18
0
    def test_delete_engagement_with_associated_menus(self):
        current_date = datetime.now().date()
        engagement = VendorEngagementFactory.create()
        menu_repo = MenuRepo()
        menu = menu_repo.new_menu(date='2018-10-15',
                                  meal_period='lunch',
                                  main_meal_id=1,
                                  allowed_side=1,
                                  allowed_protein=1,
                                  side_items=[2],
                                  protein_items=[3],
                                  vendor_engagement_id=engagement.id,
                                  location_id=1)

        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        permission = PermissionFactory.create(keyword='delete_engagement',
                                              role_id=role.id)
        user_role = 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.assert400(response)
        self.assertEqual(
            response_json['msg'],
            'This engagement cannot be deleted because it has a child object')
Пример #19
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_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)
Пример #21
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)
Пример #22
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)
Пример #23
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)
Пример #24
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])
Пример #25
0
    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)
Пример #26
0
    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_delete_role_endpoint_with_right_permission(self):
        role = RoleFactory.create()

        role1 = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        permission = PermissionFactory.create(keyword='delete_roles',
                                              role_id=role1.id)
        user_role = UserRoleFactory.create(user_id=user_id, role_id=role1.id)

        response = self.client().delete(self.make_url(f'/roles/{role.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'], 'role deleted')
Пример #28
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 '
        )
Пример #29
0
    def test_update_roles_endpoint(self):

        role = RoleFactory.create()
        role1 = RoleFactory.create(name="admin")
        user_id = BaseTestCase.user_id()

        PermissionFactory.create(keyword="create_roles", role=role1)
        UserRoleFactory.create(user_id=user_id, role=role1)
        data = {"name": "Super Admin"}
        response = self.client().put(
            self.make_url("/roles/{}".format(role.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["role"]["name"], data["name"])
    def test_update_roles_endpoint(self):

        role = RoleFactory.create()
        role1 = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        permission = PermissionFactory.create(keyword='create_roles',
                                              role_id=role1.id)
        user_role = UserRoleFactory.create(user_id=user_id, role_id=role1.id)
        data = {'name': 'Super Admin'}
        response = self.client().put(self.make_url('/roles/{}'.format(
            role.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['role']['name'], data['name'])