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)
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_update_order_with_valid_order_id_endpoint(self): order1 = OrderFactory.create() order2 = OrderFactory.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 = { 'channel': 'slack', 'mealPeriod': 'lunch', 'dateBookedFor': order2.date_booked_for.strftime('%Y-%m-%d'), 'mealItems': meal_items } response = self.client().put(self.make_url('/orders/{}'.format( order1.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['order']['channel'], data['channel']) '''Test invalid update request''' # User arbitrary value of 100 as the meal item ID response = self.client().put(self.make_url('/orders/100'), data=self.encode_to_json_string(data), headers=self.headers()) self.assert400(response)
def test_list_orders_handles_exception(self, mock_get_user): mock_get_user.side_effect = Exception('exception occured') OrderFactory.create() 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) response = self.client().get(self.make_url('/orders/'), headers=self.headers()) self.assert200(response)
def test_list_order_endpoint(self): # Create Three Dummy Vendors 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) response = self.client().get(self.make_url('/orders/'), 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['orders']), 3) self.assertJSONKeysPresent(payload['orders'][0], 'userId', 'channel', 'dateBookedFor')
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_get_all_orders_by_user_id_endpoint(self, mock_andela_service): orders = OrderFactory.create_batch(3) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() mock_andela_service.return_value = { 'id': user_id, 'first_name': fake.first_name(), 'last_name': fake.last_name() } for order in orders: order.user_id = user_id PermissionFactory.create(keyword='view_orders', role_id=role.id) UserRoleFactory.create(user_id=user_id, role_id=role.id) mock_andela_service.return_value = { 'id': user_id, 'first_name': fake.first_name(), 'last_name': fake.last_name() } response = self.client().get(self.make_url( '/orders/user/{}'.format(user_id)), headers=self.headers()) self.assert200(response) self.assertEqual( len(loads(response.data, encoding='utf-8')['payload']['orders']), 3)
def test_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')
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)
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_collect_order_not_valid_endpoint(self): order = OrderFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() order.meal_period, order.user_id = 'lunch', user_id PermissionFactory.create(keyword='view_orders', role=role) UserRoleFactory.create(user_id=user_id, role=role) data={'userId': user_id, 'orderType': 'breakfast', 'orderDate': order.date_booked_for.strftime('%Y-%m-%d')} response = self.client().post(self.make_url('/orders/collect'), data=self.encode_to_json_string(data) , headers=self.headers()) self.assert400(response)
def test_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)
def test_get_all_orders_by_user_id_endpoint(self): orders = OrderFactory.create_batch(3) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() 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) response = self.client().get(self.make_url('/orders/user/{}'.format(user_id)), headers=self.headers()) self.assert200(response) self.assertEqual(len(loads(response.data, encoding='utf-8')['payload']['orders']), 3)
def test_check_order_valid_but_collected_endpoint(self): order = OrderFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() order.meal_period, order.user_id = 'lunch', user_id order.order_status = OrderStatus.collected PermissionFactory.create(keyword='view_orders', role=role) UserRoleFactory.create(user_id=user_id, role=role) data = {'userId': user_id, 'orderType': order.meal_period, 'orderDate': order.date_booked_for.strftime('%Y-%m-%d')} response = self.client().post(self.make_url('/orders/check'), data=self.encode_to_json_string(data) , headers=self.headers()) self.assert200(response) self.assertEqual(loads(response.data, encoding='utf-8')['payload']['order']['orderStatus'], 'collected')
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')
def test_get_specific_meal_item_endpoint(self): order = OrderFactory.create() print('/orders/{}/'.format(order.id)) 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_collect_order_already_collected(self): order = OrderFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() order.meal_period, order.user_id = 'lunch', user_id PermissionFactory.create(keyword='view_orders', role_id=role.id) UserRoleFactory.create(user_id=user_id, role_id=role.id) data = {'userId': user_id, 'orderType': order.meal_period, 'orderDate': order.date_booked_for.strftime('%Y-%m-%d')} response = self.client().post(self.make_url('/orders/collect'), data=self.encode_to_json_string(data) , headers=self.headers()) self.assert200(response) response1 = self.client().post(self.make_url('/orders/collect'), data=self.encode_to_json_string(data) , headers=self.headers()) self.assertEqual(response1.status_code, 200)
def test_list_user_orders_date_range_handles_exception( self, mock_get_user): mock_get_user.side_effect = Exception('exception occured') order = OrderFactory.create() 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) response = self.client().get(self.make_url( f'/orders/user/{order.user_id}/2019-04-01/2019-01-04'), headers=self.headers()) self.assert200(response)
def test_get_order_by_user_id_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 } order = OrderFactory.create() response = self.client().get(self.make_url(f'/orders/user/{order.user_id}/{order.date_booked}/{order.date_booked_for}'), 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']['orders'][0]['id'], order.id)
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_one_order(client, users): # create an order order = OrderFactory.create( ordered_by=users[0], contributions=( (users[0], Decimal("5.00")), (users[1], Decimal("6.00")), ) ) db.session.add(order) db.session.commit() url = ( "/api/organizations/{org_id}/orders/{date}/unallocated" "?nonparticipants=true".format( org_id=users[0].organization.id, date=order.for_date ) ) response = client.get(url) assert response.status_code == 200 assert response.headers["Access-Control-Allow-Origin"] == "*" obj = json.loads(response.get_data(as_text=True)) assert len(obj['data']) == len(users) assert obj['total_unallocated'] == "10.50"
def test_interactions_after_final_meal_selection(self, mock_json_loads, mock_user_info, mock_order_repo, mock_andela_service, mock_menu_repo, mock_meal_items, mock_post): location = LocationFactory.create() location.save() menu = MenuFactory.create() menu.save() final_selection['state'] = f'{menu.id}_breakfast_2019-02-19_order_{location.id}_1' mock_json_loads.return_value = final_selection mock_user_info.return_value = {'user': {'profile': {'email': '*****@*****.**'}}} mock_andela_service.return_value = {'id': 'victor_adukwu_andela_com', 'email': '*****@*****.**'} mock_menu_repo.return_value = menu order = OrderFactory.create(menu=menu, location=location) mock_order_repo.return_value = order mock_meal_items.return_values = MealItemFactory.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')) self.assert200(response) self.assertEqual(response_json['type'], 'dialog_submission') mock_post.assert_called_once()
def orders(app): o1 = OrderFactory.create() o2 = OrderFactory.create() db.session.commit() return [o1, o2]