def test_list_vendor_ratings_endpoint(self): '''Test that users with the right permission can view list of vendor ratings''' role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() vendor_id = VendorFactory.create().id permission = PermissionFactory.create(keyword='view_ratings', role_id=role.id) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) for i in range(5): vendor_rating = VendorRating(vendor_id=vendor_id, user_id=user_id, comment=f'comment-{i}', rating=f'{i}', channel='slack') vendor_rating.save() response = self.client().get( self.make_url(f'/ratings/vendor/{vendor_id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(len(payload['ratings']), 5) self.assertJSONKeysPresent(payload['ratings'][0], 'vendorId', 'userId', 'id', 'comment', 'rating', 'channel')
def test_order_allocations(app): org = OrganizationFactory.create() u1 = UserFactory.create(organization=org) u2 = UserFactory.create(organization=org) u3 = UserFactory.create(organization=org) vendor = VendorFactory.create() order = Order( ordered_by=u1, vendor=vendor, for_date=date.today(), placed_at=datetime.now(), ) db.session.add(order) db.session.commit() assert order.total_amount == Decimal("0.00") oc1 = OrderContribution(order=order, user=u1, amount=Decimal("10.80")) db.session.add(oc1) db.session.commit() assert order.total_amount == Decimal("10.80") oc2 = OrderContribution(order=order, user=u2, amount=Decimal("2.50")) db.session.add(oc2) db.session.commit() assert order.total_amount == Decimal("13.30") oc3 = OrderContribution(order=order, user=u3, amount=Decimal("4.75")) db.session.add(oc3) db.session.commit() assert order.total_amount == Decimal("18.05")
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_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_update_vendors_endpoint(self): vendor = VendorFactory.create() data = { 'name': 'Jays Place', 'address': '123 Awesome Ave', 'tel': '10101010101', 'isActive': True, 'contactPerson': 'Joseph Cobhams' } response = self.client().put(self.make_url('/vendors/{}'.format( vendor.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['vendor']['name'], data['name']) self.assertEqual(payload['vendor']['address'], data['address']) self.assertEqual(payload['vendor']['tel'], data['tel']) self.assertEqual(payload['vendor']['contactPerson'], data['contactPerson']) '''Test invalid update request''' # User arbitrary value of 100 as the Vendor ID response = self.client().put(self.make_url('/vendors/100'), data=self.encode_to_json_string(data), headers=self.headers()) self.assert400(response)
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_order_allocations(app): org = OrganizationFactory.create() u1 = UserFactory.create(organization=org) u2 = UserFactory.create(organization=org) u3 = UserFactory.create(organization=org) vendor = VendorFactory.create() order = Order( ordered_by=u1, vendor=vendor, for_date=date.today(), placed_at=datetime.now(), ) db.session.add(order) db.session.commit() assert order.total_amount == Decimal("0.00") oc1 = OrderContribution(order=order, user=u1, amount=Decimal("10.80")) db.session.add(oc1) db.session.commit() assert order.total_amount == Decimal("10.80") oc2 = OrderContribution(order=order, user=u2, amount=Decimal("2.50")) db.session.add(oc2) db.session.commit() assert order.total_amount == Decimal("13.30") oc3 = OrderContribution(order=order, user=u3, amount=Decimal("4.75")) db.session.add(oc3) db.session.commit() assert order.total_amount == Decimal("18.05")
def test_un_suspend_vendor(self): vendor = VendorFactory.create(is_active=False) response = self.client().put(self.make_url(f'/vendors/un_suspend/{vendor.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['vendor']['isActive'], True)
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_vendor_endpoint_with_wrong_vendor_id(self): vendor = VendorFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() permission = PermissionFactory.create(keyword='delete_vendor', role_id=role.id) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete(self.make_url(f'/vendors/-576A'), headers=self.headers()) self.assert404(response)
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)
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_vendor_endpoint_without_right_permission(self): vendor = VendorFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() permission = PermissionFactory.create(keyword='delete_vendor', role_id=100) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete(self.make_url(f'/vendors/{vendor.id}'), 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 - No Permission Granted')
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_delete_vendor_with_associated_engagement(self): current_date = datetime.datetime.now().date() vendor = VendorFactory.create() vendor_engagement_repo = VendorEngagementRepo() vendor_engagement = vendor_engagement_repo.new_vendor_engagement(vendor_id=vendor.id, start_date=current_date, location_id=self.headers()['X-Location']) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() permission = PermissionFactory.create(keyword='delete_vendor', role_id=role.id) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete(self.make_url(f'/vendors/{vendor.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert400(response) self.assertEqual(response_json['msg'], 'Vendor cannot be deleted because it has a child object')
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_delete_vendor_endpoint_with_right_permission(self): vendor = VendorFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() permission = PermissionFactory.create(keyword='delete_vendor', role=role) user_role = UserRoleFactory.create(user_id=user_id, role=role) response = self.client().delete(self.make_url(f'/vendors/{vendor.id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['status'], 'success') self.assertEqual(response_json['msg'], 'Vendor deleted')
def test_get_specific_vendor_enpoint(self): vendor = VendorFactory.create() response = self.client().get(self.make_url('/vendors/{}'.format(vendor.id)), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertJSONKeyPresent(payload, 'vendor') self.assertJSONKeysPresent(payload['vendor'], 'name', 'tel', 'id', 'address', 'contactPerson', 'timestamps') self.assertEqual(payload['vendor']['id'], vendor.id) self.assertEqual(payload['vendor']['name'], vendor.name) self.assertEqual(payload['vendor']['address'], vendor.address) self.assertEqual(payload['vendor']['tel'], vendor.tel) '''Test invalid update request''' # User arbitrary value of 100 as the Vendor ID response = self.client().get(self.make_url('/vendors/100'), 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_create(client): user = UserFactory.create() vendor = VendorFactory.create() db.session.commit() response = client.post('/api/orders', data={ "contributed_by": user.id, "contributed_amount": "8.50", "ordered_by_id": user.id, "vendor_id": vendor.id, }) assert response.status_code == 201 assert response.headers["Access-Control-Allow-Origin"] == "*" assert "Location" in response.headers obj = json.loads(response.get_data(as_text=True)) assert "id" in obj url = response.headers["Location"] path = urlparse(url).path resp2 = client.get(path) assert resp2.status_code == 200 created = json.loads(resp2.get_data(as_text=True)) assert created["contributions"][0]["amount"] == "8.50"
def test_create(client): user = UserFactory.create() vendor = VendorFactory.create() db.session.commit() response = client.post('/api/orders', data={ "contributed_by": user.id, "contributed_amount": "8.50", "ordered_by_id": user.id, "vendor_id": vendor.id, }) assert response.status_code == 201 assert response.headers["Access-Control-Allow-Origin"] == "*" assert "Location" in response.headers obj = json.loads(response.get_data(as_text=True)) assert "id" in obj url = response.headers["Location"] path = urlparse(url).path resp2 = client.get(path) assert resp2.status_code == 200 created = json.loads(resp2.get_data(as_text=True)) assert created["contributions"][0]["amount"] == "8.50"
def vendors(app): v1 = VendorFactory.create() v2 = VendorFactory.create() db.session.commit() return [v1, v2]
def vendors(app): v1 = VendorFactory.create() v2 = VendorFactory.create() db.session.commit() return [v1, v2]