def test_list_engagements_by_vendor_endpoint(self):
        vendor = VendorFactory.create()
        VendorEngagementFactory.create(vendor=vendor)

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

        self.assert200(response)
        self.assertEqual(response_json['msg'], 'OK')
        self.assertEqual(response_json['payload']['engagements'][0]['vendor']['id'], vendor.id)
    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)
    def test_list_vendor_engagement_endpoint(self):
        location = LocationFactory(id=self.headers()['X-Location'])
        VendorEngagementFactory.create_batch(4, location=location)

        response = self.client().get(self.make_url('/engagements/'), 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['engagements']), 4)
        self.assertJSONKeysPresent(payload['engagements'][0], 'endDate', 'startDate', 'id', 'vendor',
                                   'terminationReason', 'timestamps')
Exemplo n.º 4
0
    def test_create_vendor_rating_endpoint_invalid_token(self):
        rating = VendorRatingFactory.build()
        order_id = OrderFactory.create().id
        vendor_id = VendorFactory.create().id
        engagement_id = VendorEngagementFactory.create().id
        main_meal_id = MealItemFactory.create().id
        data = {
            'mainMealId': main_meal_id,
            'vendorId': vendor_id,
            'engagementId': engagement_id,
            'serviceDate': datetime.strftime(rating.service_date, '%Y-%m-%d'),
            'ratingType': rating.rating_type,
            'orderId': order_id,
            'user_id': rating.user_id,
            'rating': rating.rating,
            'comment': rating.comment,
            'channel': rating.channel
        }
        response = self.client().post(
            self.make_url('/ratings/order/'),
            data=self.encode_to_json_string(data),
            headers={
                'Content-Type': 'application/json',
                'X-Location': '1',
                'Authorization': 'Bearer vnvhnv.hhbhjvjvcbcgff.cggnncbnnf'
            })

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

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response_json['msg'], 'Error Decoding')
Exemplo n.º 5
0
    def test_create_vendor_rating_endpoint_no_token(self):
        rating = VendorRatingFactory.build()
        order_id = OrderFactory.create().id
        vendor_id = VendorFactory.create().id
        engagement_id = VendorEngagementFactory.create().id
        main_meal_id = MealItemFactory.create().id
        data = {
            'mainMealId': main_meal_id,
            'vendorId': vendor_id,
            'engagementId': engagement_id,
            'serviceDate': datetime.strftime(rating.service_date, '%Y-%m-%d'),
            'ratingType': rating.rating_type,
            'orderId': order_id,
            'user_id': rating.user_id,
            'rating': rating.rating,
            'comment': rating.comment,
            'channel': rating.channel
        }
        response = self.client().post(self.make_url('/ratings/order/'),
                                      data=self.encode_to_json_string(data),
                                      headers=self.headers_without_token())

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

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response_json['msg'],
                         'Authorization Header is Expected')
    def test_create_vendor_rating_endpoint(self):
        rating = VendorRatingFactory.build()
        vendor_id = VendorFactory.create().id
        engagement_id = VendorEngagementFactory.create().id
        data = {
            'vendorId': vendor_id,
            'engagementId': engagement_id,
            'user_id': rating.user_id,
            'rating': rating.rating,
            'comment': rating.comment,
            'channel': rating.channel
        }
        response = self.client().post(self.make_url('/ratings/'),
                                      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['rating']['vendorId'], vendor_id)
        self.assertEqual(payload['rating']['userId'], rating.user_id)
        self.assertEqual(payload['rating']['rating'], rating.rating)
        self.assertEqual(payload['rating']['comment'], rating.comment)
        self.assertEqual(payload['rating']['channel'], rating.channel)
    def test_create_vendor_engagement_endpoint(self):
        vendor = VendorFactory()
        engagement = VendorEngagementFactory.build()

        start_date = str(engagement.start_date)
        end_date = str(engagement.end_date)

        data = {'vendorId': vendor.id, 'startDate': start_date, 'endDate': end_date, 'status': 1,
                'terminationReason': engagement.termination_reason, 'location_id': engagement.location_id}
        response = self.client().post(self.make_url('/engagements/'), 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.assertJSONKeysPresent(payload, 'engagement')
        self.assertJSONKeysPresent(payload['engagement'], 'endDate', 'startDate', 'vendor', 'terminationReason',
                                   'timestamps')

        self.assertEqual(payload['engagement']['vendor']['id'], vendor.id)
        self.assertEqual(payload['engagement']['vendor']['name'], vendor.name)
        self.assertEqual(payload['engagement']['vendor']['tel'], vendor.tel)

        # Assert The Year in engagement.start_date exists in the returned start_date response
        self.assertTrue(payload['engagement']['startDate'].find(start_date.split('-')[0]) > -1)
        self.assertTrue(payload['engagement']['startDate'].find(start_date.split('-')[2]) > -1)

        # Assert The Year in engagement.end_date exists in the returned end_date response
        self.assertTrue(payload['engagement']['endDate'].find(end_date.split('-')[0]) > -1)
        self.assertTrue(payload['engagement']['endDate'].find(end_date.split('-')[2]) > -1)
    def test_immediate_past_engagement_no_past_engagement(self):

        location = LocationFactory()
        location.save()
        vendor = VendorFactory.create(location=location)
        vendor.save()
        engagement1 = VendorEngagementFactory.create(vendor_id=vendor.id, end_date=datetime.now()+timedelta(10), location=location)
        engagement2 = VendorEngagementFactory.create(vendor_id=vendor.id, end_date=datetime.now()+timedelta(14), location=location)
        engagement1.save()
        engagement2.save()

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

        self.assert404(response)
        self.assertEqual(response_json['msg'], 'No past engagement for this location')
Exemplo n.º 9
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')
Exemplo n.º 10
0
    def test_create_order_rating_endpoint(self):
        rating = VendorRatingFactory.build()
        order_id = OrderFactory.create().id
        vendor_id = VendorFactory.create().id
        engagement_id = VendorEngagementFactory.create().id
        main_meal_id = MealItemFactory.create().id
        data = {
            'mainMealId': main_meal_id,
            'vendorId': vendor_id,
            'engagementId': engagement_id,
            'serviceDate': datetime.strftime(rating.service_date, '%Y-%m-%d'),
            'ratingType': rating.rating_type,
            'orderId': order_id,
            'user_id': rating.user_id,
            'rating': rating.rating,
            'comment': rating.comment,
            'channel': rating.channel
        }
        response = self.client().post(self.make_url('/ratings/order/'),
                                      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['rating']['userId'], rating.user_id)
        self.assertEqual(payload['rating']['rating'], rating.rating)
        self.assertEqual(payload['rating']['comment'], rating.comment)
        self.assertEqual(payload['rating']['channel'], rating.channel)
Exemplo n.º 11
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)
Exemplo n.º 12
0
    def test_interactions_after_final_meal_selection_valid(
        self,
        mock_json_loads,
        mock_user_info,
        mock_andela_service,
        mock_menu_repo,
        mock_engagement_repo,
        mock_post
    ):
        location = LocationFactory.create()
        location.save()
        meal = MealItemFactory.create()
        meal.save()
        submit_rating['state'] = f'{meal.id}_breakfast_2019-02-20_rate_{location.id}_1'

        mock_json_loads.return_value = submit_rating
        mock_user_info.return_value = {'user': {'profile': {'email': '*****@*****.**'}}}
        mock_andela_service.return_value = {'id': 'victor_adukwu_andela_com'}
        engagement = VendorEngagementFactory.create()
        engagement.save()
        mock_engagement_repo.return_value = engagement
        mock_menu_repo.return_value = self	.menu_factory.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'))
        mock_post.assert_called_once()
        self.assert200(response)
        self.assertEqual(response_json['type'], 'dialog_submission')
    def test_get_specific_vendor_engagement_endpoint(self):
        engagement = VendorEngagementFactory()

        start_date = str(engagement.start_date)
        end_date = str(engagement.end_date)

        response = self.client().get(self.make_url('/engagements/{}'.format(engagement.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, 'engagement')
        self.assertJSONKeysPresent(payload['engagement'], 'endDate', 'startDate', 'vendor', 'terminationReason',
                                   'timestamps')
        self.assertEqual(payload['engagement']['id'], engagement.id)
        self.assertEqual(payload['engagement']['vendor']['id'], engagement.vendor.id)
        self.assertEqual(payload['engagement']['vendor']['name'], engagement.vendor.name)
        self.assertEqual(payload['engagement']['vendor']['tel'], engagement.vendor.tel)

        # Assert The Year in engagement.start_date exists in the returned start_date response
        self.assertTrue(payload['engagement']['startDate'].find(start_date.split('-')[0]) > -1)
        self.assertTrue(payload['engagement']['startDate'].find(start_date.split('-')[2]) > -1)

        # Assert The Year in engagement.end_date exists in the returned end_date response
        self.assertTrue(payload['engagement']['endDate'].find(end_date.split('-')[0]) > -1)
        self.assertTrue(payload['engagement']['endDate'].find(end_date.split('-')[2]) > -1)

        '''Test invalid update request'''
        # User arbitrary value of 100 as the Vendor ID
        response = self.client().get(self.make_url('/engagements/100'), headers=self.headers())
        self.assert400(response)
Exemplo n.º 14
0
    def test_update_vendor_engagement_endpoint(self):
        engagement = VendorEngagementFactory()

        data = {
            'vendorId': engagement.vendor_id,
            'startDate': '2018-01-01',
            'endDate': '2018-01-07',
            'terminationReason': 'Food Poisoning',
            'status': 1
        }
        response = self.client().put(self.make_url('/engagements/{}'.format(
            engagement.id)),
                                     data=self.encode_to_json_string(data),
                                     headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))

        # assert False
        payload = response_json['payload']

        self.assert200(response)
        self.assertEqual(payload['engagement']['id'], engagement.id)
        self.assertEqual(payload['engagement']['status'], data['status'])
        self.assertEqual(payload['engagement']['vendorId'], data['vendorId'])
        self.assertEqual(payload['engagement']['terminationReason'],
                         data['terminationReason'])

        # Assert The Year in engagement.start_date exists in the returned start_date response
        self.assertTrue(payload['engagement']['startDate'].find(
            data['startDate'].split('-')[0]) > -1)
        self.assertTrue(payload['engagement']['startDate'].find(
            data['startDate'].split('-')[2]) > -1)

        # Assert The Year in engagement.end_date exists in the returned end_date response
        self.assertTrue(payload['engagement']['endDate'].find(
            data['endDate'].split('-')[0]) > -1)
        self.assertTrue(payload['engagement']['endDate'].find(
            data['endDate'].split('-')[2]) > -1)

        self.assertEqual(payload['engagement']['vendor']['name'],
                         engagement.vendor.name)
        self.assertEqual(payload['engagement']['vendor']['address'],
                         engagement.vendor.address)
        self.assertEqual(payload['engagement']['vendor']['contactPerson'],
                         engagement.vendor.contact_person)
        self.assertEqual(payload['engagement']['vendor']['tel'],
                         engagement.vendor.tel)
        self.assertEqual(payload['engagement']['vendor']['id'],
                         engagement.vendor.id)

        # '''Test invalid update request'''
        # User arbitrary value of 100 as the Vendor ID
        response = self.client().put(self.make_url('/engagements/100'),
                                     data=self.encode_to_json_string(data),
                                     headers=self.headers())
        self.assert400(response)
Exemplo n.º 15
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 '
        )
Exemplo n.º 16
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_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_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')
Exemplo n.º 19
0
    def test_list_rating_endpoint(self):
        vendor = VendorFactory.create()
        engagement = VendorEngagementFactory.create(vendor=vendor)
        rating = VendorRatingFactory.create(engagement=engagement, vendor=vendor)
        create_user_role('view_ratings')
        now = datetime.now()

        response = self.client().get(self.make_url(f'/ratings/{now.strftime("%Y-%m-%d")}'),
                                     headers=self.headers())

        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        self.assert404(response)
        self.assertEqual(response_json['msg'], 'No ratings for this date')
Exemplo n.º 20
0
    def test_rating_today_meal_fails(self):
        vendor = VendorFactory.create()

        engagement = VendorEngagementFactory.create(vendor=vendor)
        engagement_rating = VendorRatingFactory.build(engagement_id=engagement.id)

        rating_data = {'rating': engagement_rating.rating, 'engagementId': engagement_rating.engagement_id,
                       'serviceDate': datetime.strftime(datetime.now().date(), '%Y-%m-%d'), 'mainMealId': MealItemFactory.create().id}

        response = self.client().post(self.make_url(f'/ratings/order/'), data=self.encode_to_json_string(rating_data),
                                      headers=self.headers())

        self.assertEqual(response.status_code, 400)
Exemplo n.º 21
0
    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)
Exemplo n.º 22
0
    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'
        )
Exemplo n.º 23
0
    def test_interactions_after_final_meal_selection_invalid(self, mock_json_loads, mock_user_info,
                                                     mock_andela_service, mock_menu_repo, mock_engagement_repo,
                                                     rating_repo, mock_post):
        mock_json_loads.return_value = submit_rating
        mock_user_info.return_value = {'user': {'profile': {'email': '*****@*****.**'}}}
        mock_andela_service.return_value = {'id': 'victor_adukwu_andela_com'}
        mock_engagement_repo.return_value = VendorEngagementFactory.create()
        mock_menu_repo.return_value = self.menu_factory.create()
        rating_repo.return_value = None

        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'))
        mock_post.assert_called_once()
        self.assert200(response)
        self.assertEqual(response_json['type'], 'dialog_submission')
    def test_delete_engagement_endpoint_with_right_permission(self):
        engagement = 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=role)

        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'))
        payload = response_json['payload']

        self.assert200(response)
        self.assertEqual(payload['status'], 'success')
        self.assertEqual(response_json['msg'], 'Engagement deleted')
Exemplo n.º 25
0
    def test_create_vendor_rating_endpoint(self):
        vendor = VendorFactory.create()

        engagement = VendorEngagementFactory.create(vendor=vendor)
        engagement_rating = VendorRatingFactory.build(engagement_id=engagement.id)

        rating_data = {'rating': engagement_rating.rating, 'engagementId': engagement_rating.engagement_id,
                       'serviceDate': engagement_rating.service_date.strftime('%Y-%m-%d')}

        response = self.client().post(self.make_url(f'/ratings/'), data=self.encode_to_json_string(rating_data),
                                      headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_json['msg'], 'Rating created')
        self.assertEqual(response_json['payload']['rating']['id'], 1)
    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)
Exemplo n.º 27
0
    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 '
        )