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)
Exemple #3
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')
    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_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')
    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')
Exemple #7
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')
Exemple #8
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)
Exemple #9
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_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')
Exemple #11
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)
Exemple #12
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')
    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')
    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')
Exemple #15
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)