def test_create_order_with_valid_details_endpoint(self):
        LocationFactory.create(id=1, zone='+1')
        order = OrderFactory.create()
        menu = MenuFactory.create()
        meal_item1 = MealItemFactory.create()
        meal_item2 = MealItemFactory.create()
        meal_item3 = MealItemFactory.create()
        meal_item1.meal_type = MealTypes.protein
        meal_item2.meal_type = MealTypes.main

        meal_items = [meal_item1.id, meal_item2.id, meal_item3.id]
        data = {'userId': order.user_id, 'dateBookedFor': (date.today() + timedelta(days=2)).strftime('%Y-%m-%d'),
                'dateBooked': order.date_booked.strftime('%Y-%m-%d'), 'channel': 'web', 'menuId': menu.id,
                'mealPeriod': order.meal_period, 'mealItems': meal_items}

        response = self.client().post(
            self.make_url('/orders/'), 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['order']['userId'], BaseTestCase.user_id())
        self.assertEqual(payload['order']['channel'], 'web')
    def test_create_order_with_invalid_details_endpoint(self):
        items = [item.id for item in MealItemFactory.create_batch(4)]
        menu = MenuFactory.create()
        LocationFactory.create(id=1, zone='+1')
        data = {
            'dateBookedFor':
            (date.today() + timedelta(days=-3)).strftime('%Y-%m-%d'),
            'channel':
            'web',
            'mealPeriod':
            'lunch',
            'menuId':
            menu.id
        }

        # If we don't add meal items
        response = self.client().post(self.make_url('/orders/'),
                                      data=self.encode_to_json_string(data),
                                      headers=self.headers())
        self.assert400(response)

        # If we book in the past
        data.update({'mealItems': items})
        response1 = self.client().post(self.make_url('/orders/'),
                                       data=self.encode_to_json_string(data),
                                       headers=self.headers())
        self.assert400(response1)
        data.update({
            'dateBookedFor':
            (date.today() + timedelta(days=2)).strftime('%Y-%m-%d')
        })
        response2 = self.client().post(self.make_url('/orders/'),
                                       data=self.encode_to_json_string(data),
                                       headers=self.headers())
        self.assertEqual(response2.status_code, 201)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
    def test_create_user_endpoint_succeeds(self):
        create_user_role('create_user')
        user = UserFactory.create(user_id="user_id_2", is_deleted=True)
        role = RoleFactory(name='test_role')

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

        user_data = dict(
            firstName=user.first_name,
            lastName=user.last_name,
            roleId=role.id
        )

        response = self.client().post(
            self.make_url("/users/"),
            headers=headers,
            data=self.encode_to_json_string(user_data))

        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']['firstName'], user.first_name)
        self.assertEqual(response_json['payload']['user']['lastName'], user.last_name)
        self.assertEqual(response_json['payload']['user']['userRoles'][0]['name'], role.name)
        self.assertEqual(response_json['payload']['user']['userRoles'][0]['help'], role.help)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
    def test_list_menu_endpoint_with_invalid_date_fails(self):
        """Test that users with the wrong date fails"""
        meal_item_repo = MealItemRepo()
        location = LocationFactory.create()

        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)

        menu = MenuFactory.create(main_meal_id=main_meal_item.id,
                                  side_items=str(side_meal_item.id),
                                  protein_items=str(protein_meal_item.id))

        date = '201502-24'

        response = self.client().get(
            self.make_url(f'/menus/{menu.meal_period}/{date}/{date}'),
            headers=self.headers())
        self.assert400(response)
Ejemplo n.º 7
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])
Ejemplo n.º 8
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)
Ejemplo n.º 9
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')
Ejemplo n.º 10
0
 def test_create_menu_template_with_permission_succeeds(self):
     self.create_admin()
     LocationFactory.create(id=1).save()
     data = {
         "name": "Name of the template",
         "mealPeriod": "lunch",
         "description": "somehting"
     }
     response = self.client().post(self.make_url("/menu_template/"),
                                   headers=self.headers(),
                                   data=self.encode_to_json_string(data))
     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.assertJSONKeysPresent(response_json['payload'], 'name')
     self.assertJSONKeysPresent(response_json['payload'], 'locationId')
Ejemplo n.º 11
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 '
        )
Ejemplo n.º 12
0
    def test_interactions_after_selecting_location(self, mock_get_menu_start_end_on, mock_json_loads, mock_locationrepo_get):
        mock_json_loads.return_value = center_selected
        mock_get_menu_start_end_on.return_value = (datetime(2019, 2, 15), datetime(2019, 2, 15))
        mock_locationrepo_get.get.return_value = LocationFactory.create(id=1)

        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'))

        self.assert200(response)
        self.assertEqual(response_json['type'], 'interactive_message')
        self.assertEqual(type(response_json['actions']), list)
Ejemplo n.º 13
0
    def test_delete_existing_location_succeeds(self):
        new_location = LocationFactory.create()

        response = self.client().delete(
            self.make_url(f'/locations/{new_location.id}'),
            headers=self.headers())

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

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response_json['msg'], 'Location deleted successfully')
Ejemplo n.º 14
0
    def test_get_location_succeeds(self):

        new_location = LocationFactory.create()

        response = self.client().get(self.make_url('/locations/'),
                                     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']['locations'][0]['name'],
                         new_location.name)
Ejemplo n.º 15
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'
        )
 def test_new_meal_service_method_returns_new_meal_service_object(self):
     role = RoleFactory.create()
     location = LocationFactory.create()
     user_role = UserRoleFactory.build(role_id=role.id, location=location)
     user = UserFactory.create(user_type=user_role)
     meal_session = MealSessionFactory(location=location)
     meal_service = MealServiceFactory(user=user, session=meal_session)
     new_meal_service = self.repo.new_meal_service(
         user_id=meal_service.user.id,
         session_id=meal_service.session.id,
         date=meal_service.date)
     self.assertIsInstance(new_meal_service, MealService)
     self.assertEqual(new_meal_service.user_id, meal_service.user_id)
     self.assertEqual(new_meal_service.session_id, meal_service.session_id)
     self.assertEqual(new_meal_service.date, meal_service.date)
    def test_daily_taps_wrong_date_range(self):
        location = LocationFactory.create()
        session = MealSessionFactory(location_id=location.id)
        user = UserFactory()

        MealServiceFactory.create_batch(3,
                                        user_id=user.id,
                                        session_id=session.id,
                                        date=datetime.datetime(2019, 4, 8))

        response = self.client().get(self.make_url(
            '/reports/taps/daily/?date_range=2019-04-01:2019-04-10'),
                                     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'],
                         'Start date must not be less than end date')
Ejemplo n.º 18
0
    def test_create_user_endpoint_succeeds2(self):
        location = LocationFactory.create()
        headers = self.headers()
        headers.update({"X-Location": location.id})

        create_user_role("view_users", "admin")
        user = UserFactory()
        user.save()
        response = self.client().get(
            self.make_url(f"/users/user_profile/{user.id}"), headers=headers
        )

        response_json = self.decode_from_json_string(response.data.decode("utf-8"))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response_json["msg"], "OK")
        self.assertEqual(
            response_json["payload"]["user"]["first_name"], user.first_name
        )
        self.assertEqual(response_json["payload"]["user"]["last_name"], user.last_name)
    def test_delete_order_not_yours(self):
        location = LocationFactory.create()
        user_id = BaseTestCase.user_id()
        meal = MealItemFactory.create()
        menu = MenuFactory.create()
        order_data = {
            'user_id': '-UTG654RfggtdI',
            'date_booked_for': '2018-10-20',
            'channel': 'web', 'meal_period': 'lunch', 'menu_id': menu.id,
            'meal_items': [meal], 'location_id': location.id
        }
        order_repo = OrderRepo()
        order = order_repo.create_order(**order_data)

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

        self.assert403(response)
        self.assertEqual(response_json['msg'], 'You cannot delete an order that is not yours')
Ejemplo n.º 20
0
    def test_update_existing_location_succeeds(self):
        new_location = LocationFactory.create()

        update_location_data = {'name': new_location.name, 'zone': 'central'}

        response = self.client().patch(
            self.make_url(f'/locations/{new_location.id}'),
            data=self.encode_to_json_string(update_location_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'], 'OK')
        self.assertEqual(response_json['payload']['location']['name'],
                         update_location_data['name'])
        self.assertEqual(response_json['payload']['location']['zone'],
                         new_location.zone)
Ejemplo n.º 21
0
    def test_create_user_endpoint_succeeds2(self):
        location = LocationFactory.create()
        headers = self.headers()
        headers.update({'X-Location': location.id})

        create_user_role('view_users')
        user = UserFactory(slack_id="-LXTuXlk2W4Gskt8KTte")
        user.save()

        response = self.client().get(
            self.make_url(f'/users/{user.slack_id}/'),
            headers=headers
        )

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

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response_json['msg'], 'OK')
        self.assertEqual(response_json['payload']['user']['firstName'], user.first_name)
        self.assertEqual(response_json['payload']['user']['lastName'], user.last_name)
Ejemplo n.º 22
0
    def test_create_meal_item_endpoint_with_existing_name(self):
        location = LocationFactory.create()
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='create_meal_item', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)
        headers = self.headers()
        headers.update({'X-Location': location.id})

        MealItemFactory.create(name='sweet item', location=location)
        meal_item = MealItemFactory.build(name='sweet item')
        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'))

        self.assert400(response)
        self.assertEqual(response_json['msg'], 'Meal item with this name already exists')
    def test_get_specific_meal_item_endpoint(self, mock_andela_service):
        location = LocationFactory.create()
        order = OrderFactory.create(location=location)
        order.save()

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

        response = self.client().get(self.make_url('/orders/{}'.format(order.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, 'order')
        self.assertJSONKeysPresent(payload['order'], 'userId', 'channel')
        self.assertEqual(int(payload['order']['id']), order.id)
        self.assertEqual(payload['order']['userId'], order.user_id)
        self.assertEqual(payload['order']['channel'], order.channel)
    def test_daily_taps_custom_date_range(self):
        location = LocationFactory.create()
        session = MealSessionFactory(location_id=location.id)
        user = UserFactory()

        MealServiceFactory.create_batch(3,
                                        user_id=user.id,
                                        session_id=session.id,
                                        date=datetime.datetime(2019, 4, 8))

        response = self.client().get(self.make_url(
            '/reports/taps/daily/?date_range=2019-04-10:2019-04-01'),
                                     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)
Ejemplo n.º 25
0
    def test_create_user_endpoint_succeeds(self):
        create_user_role("create_user", "admin")
        user = UserFactoryFake.build(id=500, is_deleted=True)
        role = RoleFactory(name="test_role")

        location = LocationFactory.create()
        headers = self.headers()
        headers.update({"X-Location": location.id})

        user_data = dict(
            first_name=user.first_name,
            last_name=user.last_name,
            role_id=role.id,
            gender=user.gender,
            date_of_birth=str(user.date_of_birth),
            employment_date=str(user.employment_date),
            password=user.password,
            email=user.email,
            location_id=user.location_id,
        )

        response = self.client().post(
            self.make_url("/users/"),
            headers=headers,
            data=self.encode_to_json_string(user_data),
        )

        response_json = self.decode_from_json_string(response.data.decode("utf-8"))
        print(response_json)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_json["msg"], "OK")
        self.assertEqual(
            response_json["payload"]["user"]["first_name"], user.first_name
        )
        self.assertEqual(response_json["payload"]["user"]["last_name"], user.last_name)
        self.assertEqual(
            response_json["payload"]["user"]["user_roles"][0]["name"], role.name
        )
        self.assertEqual(
            response_json["payload"]["user"]["user_roles"][0]["help"], role.help
        )
Ejemplo n.º 26
0
    def test_list_meal_item_endpoint_right_permission(self):
        # Create Three Dummy Vendors
        location = LocationFactory.create()
        meals = MealItemFactory.create_batch(3, location=location)

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

        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)

        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']

        self.assert200(response)
        self.assertEqual(len(payload['mealItems']), 3)
        self.assertJSONKeysPresent(payload['mealItems'][0], 'name', 'mealType', 'image')
    def test_delete_order_endpoint_with_right_permission(self):
        location = LocationFactory.create()
        user_id = BaseTestCase.user_id()
        meal = MealItemFactory.create()
        menu = MenuFactory.create()
        order_data = {
            'user_id': user_id,
            'date_booked_for': '2018-10-20',
            'channel': 'web', 'meal_period': 'lunch', 'menu_id': menu.id,
            'meal_items': [meal], 'location_id': location.id
        }
        order_repo = OrderRepo()
        order = order_repo.create_order(**order_data)

        response = self.client().delete(self.make_url(f'/orders/{order.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'], 'Order deleted')
    def test_list_orders_date_range_endpoint(self, mock_andela_get_user_by_email):

        first_name, last_name = self.user_first_and_last_name()
        mock_andela_get_user_by_email.return_value = {
            'id': self.user_id(),
            'first_name': first_name,
            'last_name': last_name
        }

        create_user_role('view_orders')
        location = LocationFactory.create()
        order = OrderFactory.create(location=location)

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

        response = self.client().get(self.make_url(f'/orders/{order.date_booked}/{order.date_booked_for}'), headers=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']['orders'][0]['id'], order.id)
    def test_list_order_endpoint(self, mock_andela_service):
        # Create Three Dummy Vendors
        location = LocationFactory.create()
        OrderFactory.create_batch(3, location=location)
        role = RoleFactory.create(name='admin')
        user_id = BaseTestCase.user_id()
        PermissionFactory.create(keyword='view_orders', role=role)
        UserRoleFactory.create(user_id=user_id, role=role)

        mock_andela_service.return_value = {
            'id': user_id,
            'first_name': fake.first_name(),
            'last_name': fake.last_name()
        }
        headers = self.headers()
        headers.update({'X-Location': location.id})

        response = self.client().get(self.make_url('/orders/'), headers=headers)
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        payload = response_json['payload']
        self.assert200(response)
        self.assertEqual(len(payload['orders']), 3)
        self.assertJSONKeysPresent(payload['orders'][0], 'userId', 'channel', 'dateBookedFor')
Ejemplo n.º 30
0
    def test_list_menu_range_endpoint_succeeds(self):
        location = LocationFactory.create()
        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)
        headers = self.headers()
        headers.update({'X-Location': location.id})
        menu = MenuFactory.create(main_meal_id=main_meal_item.id,
                                  side_items=str(side_meal_item.id),
                                  protein_items=str(protein_meal_item.id),
                                  location=location)
        menu.save()
        start_date = menu.vendor_engagement.start_date - timedelta(days=1)

        response = self.client().get(self.make_url(
            f'/menus/{menu.meal_period}/{start_date}/{menu.vendor_engagement.end_date}'
        ),
                                     headers=headers)

        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))
        self.assertEqual(response_json['msg'], 'OK')
        self.assertEqual(
            response_json['payload']['menuList'][0]['menus'][0]['id'], menu.id)