/** * Remaining uncovered procedures: address update/delete rules, staff-user * management, review responses, auth profile/password, upload-URL validation, * the delivery polygon and getCartSlots. */ import { apiClient, captureError, uniqueSuffix } from '../helpers/api' import { createFutureSlot, createStoreWithSku, registerThrowawayUser, registerUserWithAddress, } from '../helpers/fixtures' import { staffSession } from '../helpers/sessions' const addressBody = (over: Record = {}) => ({ regionId: 1, name: 'API Address', phone: '9999999999', addressLine1: '1 API Street', city: 'Mahabubnagar', state: 'Telangana', isDefault: false, ...over, }) describe('misc procedures', () => { let admin: ReturnType beforeAll(async () => { const staff = await staffSession() admin = apiClient(staff.token) }) describe('address rules', () => { it('rejects updating another user address', async () => { const owner = await registerUserWithAddress() const other = await registerUserWithAddress() const error = await captureError(() => apiClient(other.token).user.address.updateAddress.mutate({ id: owner.addressId, ...addressBody({ name: 'hijack' }), }) ) expect(error.message).toMatch(/address not found/i) }) it('refuses to delete the default address', async () => { const user = await registerUserWithAddress() const error = await captureError(() => apiClient(user.token).user.address.deleteAddress.mutate({ id: user.addressId }) ) expect(error.message).toMatch(/cannot delete default address/i) }) it('refuses to delete an address attached to an ongoing order', async () => { const store = await createStoreWithSku({ price: 100 }) const slot = await createFutureSlot([store.skuId]) const user = await registerUserWithAddress() const extra = await apiClient(user.token).user.address.createAddress.mutate( addressBody({ name: 'Order address' }) ) const orderAddressId = extra.data!.id await apiClient(user.token).user.order.placeOrder.mutate({ selectedItems: [{ skuId: store.skuId, quantity: 1, slotId: slot.slotId }], addressId: orderAddressId, regionId: 1, paymentMethod: 'cod', }) const error = await captureError(() => apiClient(user.token).user.address.deleteAddress.mutate({ id: orderAddressId }) ) expect(error.message).toMatch(/ongoing order/i) }) it('deletes an unused non-default address', async () => { const user = await registerUserWithAddress() const extra = await apiClient(user.token).user.address.createAddress.mutate( addressBody({ name: 'Unused' }) ) const res = await apiClient(user.token).user.address.deleteAddress.mutate({ id: extra.data!.id, }) expect(res.success).toBe(true) }) it('returns the default address', async () => { const user = await registerUserWithAddress() const res = await apiClient(user.token).user.address.getDefaultAddress.query() expect(res.data?.id).toBe(user.addressId) }) }) describe('staff user management', () => { it('lists roles', async () => { const res = await admin.admin.staffUser.getRoles.query() expect(Array.isArray(res.roles)).toBe(true) expect(res.roles.length).toBeGreaterThan(0) }) it('creates a staff user, lists it and rejects a duplicate name', async () => { const roles = await admin.admin.staffUser.getRoles.query() const name = `misc-staff-${uniqueSuffix()}` const created = await admin.admin.staffUser.createStaffUser.mutate({ name, password: 'Passw0rd!', roleId: roles.roles[0].id, }) expect(created).toBeTruthy() const staff = (await admin.admin.staffUser.getStaff.query()) as | { id: number; name: string }[] | { staff?: { id: number; name: string }[] } const list = Array.isArray(staff) ? staff : (staff.staff ?? []) expect(list.some((s) => s.name === name)).toBe(true) const error = await captureError(() => admin.admin.staffUser.createStaffUser.mutate({ name, password: 'Passw0rd!', roleId: roles.roles[0].id, }) ) expect(error.message).toMatch(/already exists/i) }) }) describe('product reviews', () => { it('responds to a review and returns the response', async () => { const store = await createStoreWithSku() const buyer = await registerUserWithAddress() const body = `Review ${uniqueSuffix()}` await apiClient(buyer.token).user.product.createReview.mutate({ productId: store.productId, reviewBody: body, ratings: 4, }) const listed = await admin.admin.product.getProductReviews.query({ productId: store.productId, limit: 50, offset: 0, }) const review = listed.reviews.find( (r: { reviewBody: string }) => r.reviewBody === body ) expect(review).toBeDefined() const res = await admin.admin.product.respondToReview.mutate({ reviewId: review!.id, adminResponse: 'Thanks for the feedback', }) expect(res.success).toBe(true) const after = await admin.admin.product.getProductReviews.query({ productId: store.productId, limit: 50, offset: 0, }) const updated = after.reviews.find((r: { id: number }) => r.id === review!.id) expect(updated!.adminResponse).toBe('Thanks for the feedback') }) }) describe('user auth', () => { it('updates the profile and returns the new name', async () => { const user = await registerUserWithAddress() const newName = `Renamed ${uniqueSuffix()}` const res = await apiClient(user.token).user.auth.updateProfile.mutate({ name: newName, bio: 'hello bio', }) expect(res.success).toBe(true) const self = await apiClient(user.token).user.user.getSelfData.query() expect(self.data.user.name).toBe(newName) }) it('rejects a too-short password', async () => { const user = await registerUserWithAddress() const error = await captureError(() => apiClient(user.token).user.auth.updatePassword.mutate({ password: '123' }) ) expect(error).toBeInstanceOf(Error) }) it('updatePassword: the new password works and the old one fails', async () => { const suffix = uniqueSuffix() const mobile = `9${String(Math.floor(Math.random() * 1e9)).padStart(9, '0')}` const reg = await apiClient().user.auth.register.mutate({ name: `PW ${suffix}`, email: `pw-${suffix}@example.com`, mobile, password: 'OldPass123', }) const res = await apiClient(reg.data.token).user.auth.updatePassword.mutate({ password: 'NewPass456', }) expect(res.success).toBe(true) // The new password authenticates... const newLogin = await apiClient().user.auth.login.mutate({ identifier: mobile, password: 'NewPass456', }) expect(newLogin.data.token).toBeTruthy() // ...and the old one no longer does. const error = await captureError(() => apiClient().user.auth.login.mutate({ identifier: mobile, password: 'OldPass123' }) ) expect(error.message).toMatch(/invalid credentials/i) }) }) describe('upload urls', () => { it('rejects an unknown context', async () => { const user = await registerThrowawayUser() const error = await captureError(() => apiClient(user.token).common.generateUploadUrls.mutate({ contextString: 'nope' as never, mimeTypes: ['image/jpeg'], }) ) expect(error).toBeInstanceOf(Error) }) it('still returns a URL for an unrecognised mime type', async () => { const user = await registerThrowawayUser() const res = await apiClient(user.token).common.generateUploadUrls.mutate({ contextString: 'complaint', mimeTypes: ['application/octet-stream'], }) expect(res.uploadUrls).toHaveLength(1) }) }) describe('delivery polygon', () => { it('accepts a point inside the delivery area', async () => { const res = await apiClient().common.checkLocationInPolygon.query({ lat: 16.747227, lng: 78.009737, }) expect(res).toEqual({ isInside: true }) }) it('rejects a point outside the delivery area', async () => { const res = await apiClient().common.checkLocationInPolygon.query({ lat: 17.747227, lng: 79.009737, }) expect(res).toEqual({ isInside: false }) }) it('rejects out-of-range coordinates', async () => { const error = await captureError(() => apiClient().common.checkLocationInPolygon.query({ lat: 999, lng: 0 }) ) expect(error).toBeInstanceOf(Error) }) }) describe('cart slots', () => { it('returns an empty object for no products', async () => { const res = await apiClient().user.cart.getCartSlots.query({ regionId: 1, productIds: [] }) expect(res).toEqual({}) }) it('returns a slot map keyed by product id', async () => { const store = await createStoreWithSku() await createFutureSlot([store.skuId]) const res = (await apiClient().user.cart.getCartSlots.query({ regionId: 1, productIds: [store.skuId], })) as Record expect(Object.keys(res)).toContain(String(store.skuId)) }) }) describe('constants', () => { it('surfaces internalAppVersion with a numeric default', async () => { const constants = await admin.admin.const.getConstants.query() const entry = constants.find((c: { key: string }) => c.key === 'internalAppVersion') expect(entry).toBeDefined() expect(typeof entry!.value).toBe('number') }) it('exposes internalAppVersion to the user app via essentialConsts', async () => { const consts = (await apiClient().common.essentialConsts.query({ boundId: 1 })) as { internalAppVersion?: number } expect(typeof consts.internalAppVersion).toBe('number') }) }) })