500 lines
18 KiB
TypeScript
500 lines
18 KiB
TypeScript
/**
|
|
* UADDR-01..30 — `user.address.*` (catalogue §6.7).
|
|
*
|
|
* Live HTTP suite: every call goes over the wire against the running backend.
|
|
* Addresses are created for throwaway users only and are never the seeded rows.
|
|
*/
|
|
|
|
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
|
|
import {
|
|
COORDS,
|
|
createAddress,
|
|
createCatalogOrderFixture,
|
|
placeOrder,
|
|
registerThrowawayUser,
|
|
} from '../helpers/fixtures'
|
|
|
|
jest.setTimeout(180_000)
|
|
|
|
const REGION_2 = { regionId: 2, boundId: 3 }
|
|
|
|
/** A complete createAddress payload with sane defaults (override per row). */
|
|
function addrInput(overrides: Record<string, unknown> = {}): any {
|
|
return {
|
|
regionId: 1,
|
|
name: 'E2E Home',
|
|
phone: '9876543210',
|
|
addressLine1: `12 E2E Road ${uniqueSuffix()}`,
|
|
addressLine2: 'Near test landmark',
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
latitude: COORDS.region1Bound1.lat,
|
|
longitude: COORDS.region1Bound1.lng,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
async function listAddresses(token: string): Promise<any[]> {
|
|
const res: any = await apiClient(token).user.address.getUserAddresses.query()
|
|
return res.data
|
|
}
|
|
|
|
async function defaultAddress(token: string): Promise<any> {
|
|
const res: any = await apiClient(token).user.address.getDefaultAddress.query()
|
|
return res.data
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* createAddress */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('user.address.createAddress (UADDR-01..13)', () => {
|
|
it('UADDR-01: the first address is created and becomes the default', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(addrInput())
|
|
|
|
expect(res.success).toBe(true)
|
|
expect(res.data.id).toBeGreaterThan(0)
|
|
expect(res.data.isDefault).toBe(true)
|
|
expect(res.data.regionId).toBe(1)
|
|
})
|
|
|
|
it('UADDR-02: a second address stays non-default', async () => {
|
|
const user = await registerThrowawayUser()
|
|
await createAddress(user.token)
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({ isDefault: false, addressLine1: '2 Second Road' })
|
|
)
|
|
expect(res.success).toBe(true)
|
|
expect(res.data.isDefault).toBe(false)
|
|
})
|
|
|
|
it('UADDR-03: an explicit default unsets the previous default', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const a1: any = await createAddress(user.token)
|
|
await createAddress(user.token, { isDefault: false })
|
|
const a3: any = await createAddress(user.token, { isDefault: true, addressLine1: '3 Third Road' })
|
|
|
|
const rows = await listAddresses(user.token)
|
|
const byId = Object.fromEntries(rows.map((r) => [r.id, r.isDefault]))
|
|
expect(byId[a3.id]).toBe(true)
|
|
expect(byId[a1.id]).toBe(false)
|
|
expect(rows.filter((r) => r.isDefault)).toHaveLength(1)
|
|
})
|
|
|
|
it('UADDR-04: a phone starting 6-9 is accepted', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({ phone: '6123456789' })
|
|
)
|
|
expect(res.success).toBe(true)
|
|
expect(res.data.phone).toBe('6123456789')
|
|
})
|
|
|
|
it('UADDR-05: an invalid phone is BAD_REQUEST', async () => {
|
|
const user = await registerThrowawayUser()
|
|
await expectTrpcError(
|
|
() => apiClient(user.token).user.address.createAddress.mutate(addrInput({ phone: '1234567890' })),
|
|
'BAD_REQUEST'
|
|
)
|
|
await expectTrpcError(
|
|
() => apiClient(user.token).user.address.createAddress.mutate(addrInput({ phone: '5876543210' })),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('UADDR-06: coordinates inside a bound derive that boundId', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({ latitude: COORDS.region1Bound1.lat, longitude: COORDS.region1Bound1.lng })
|
|
)
|
|
expect(res.data.boundId).toBe(1)
|
|
expect(res.data.latitude).toBeCloseTo(COORDS.region1Bound1.lat, 6)
|
|
})
|
|
|
|
it('UADDR-07: no coordinates falls back to the region effective bound', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({ latitude: undefined, longitude: undefined })
|
|
)
|
|
expect(res.success).toBe(true)
|
|
expect(res.data.latitude).toBeNull()
|
|
expect(res.data.boundId).toBe(1)
|
|
})
|
|
|
|
it('UADDR-08: coordinates outside the region are 422 UNPROCESSABLE_CONTENT', async () => {
|
|
const user = await registerThrowawayUser()
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({ latitude: 0, longitude: 0 })
|
|
),
|
|
'UNPROCESSABLE_CONTENT',
|
|
'outside this region'
|
|
)
|
|
})
|
|
|
|
it('UADDR-09: a redirecting googleMapsUrl supplies the coordinates', async () => {
|
|
// The handler follow-fetches the short link and reads `!3d<lat>!4d<lng>` from
|
|
// the redirect Location. httpbin stands in for the Google short-link server.
|
|
const target = `https://example.com/maps/!3d${COORDS.region1Bound1.lat}!4d${COORDS.region1Bound1.lng}`
|
|
const googleMapsUrl = `https://httpbin.org/redirect-to?url=${encodeURIComponent(target)}`
|
|
|
|
const user = await registerThrowawayUser()
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({ latitude: undefined, longitude: undefined, googleMapsUrl })
|
|
)
|
|
expect(res.success).toBe(true)
|
|
expect(res.data.latitude).toBeCloseTo(COORDS.region1Bound1.lat, 6)
|
|
expect(res.data.longitude).toBeCloseTo(COORDS.region1Bound1.lng, 6)
|
|
expect(res.data.boundId).toBe(1)
|
|
})
|
|
|
|
it('UADDR-10: a malformed googleMapsUrl yields no coords and the effective bound', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({
|
|
latitude: undefined,
|
|
longitude: undefined,
|
|
googleMapsUrl: 'https://maps.app.goo.gl/not-a-real-link-xyz',
|
|
})
|
|
)
|
|
expect(res.success).toBe(true)
|
|
expect(res.data.latitude).toBeNull()
|
|
expect(res.data.longitude).toBeNull()
|
|
expect(res.data.boundId).toBe(1)
|
|
})
|
|
|
|
it('UADDR-11: regionId is required (BAD_REQUEST)', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const input: any = addrInput()
|
|
delete input.regionId
|
|
await expectTrpcError(
|
|
() => apiClient(user.token).user.address.createAddress.mutate(input),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('UADDR-12: an optional addressLine2 round-trips', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const line2 = `Ground floor ${uniqueSuffix()}`
|
|
const res: any = await apiClient(user.token).user.address.createAddress.mutate(
|
|
addrInput({ addressLine2: line2 })
|
|
)
|
|
expect(res.data.addressLine2).toBe(line2)
|
|
const stored = (await listAddresses(user.token)).find((r) => r.id === res.data.id)
|
|
expect(stored.addressLine2).toBe(line2)
|
|
})
|
|
|
|
it('UADDR-13: an address is private to its owner (read/update/delete)', async () => {
|
|
const owner = await registerThrowawayUser()
|
|
const other = await registerThrowawayUser()
|
|
const address: any = await createAddress(owner.token)
|
|
|
|
// Read isolation.
|
|
const otherList = await listAddresses(other.token)
|
|
expect(otherList.some((r) => r.id === address.id)).toBe(false)
|
|
|
|
// Update isolation: the handler throws a raw Error -> 500.
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(other.token).user.address.updateAddress.mutate(
|
|
addrInput({ id: address.id }) as any
|
|
),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Address not found'
|
|
)
|
|
|
|
// Delete isolation: an explicit 404.
|
|
await expectTrpcError(
|
|
() => apiClient(other.token).user.address.deleteAddress.mutate({ id: address.id }),
|
|
'NOT_FOUND'
|
|
)
|
|
|
|
// The owner's row is untouched.
|
|
const ownerList = await listAddresses(owner.token)
|
|
expect(ownerList.some((r) => r.id === address.id)).toBe(true)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Reads + update */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('user.address reads + updateAddress (UADDR-14..22)', () => {
|
|
it('UADDR-14: getDefaultAddress returns the default, or null when there is none', async () => {
|
|
const user = await registerThrowawayUser()
|
|
expect(await defaultAddress(user.token)).toBeNull()
|
|
|
|
const created: any = await createAddress(user.token)
|
|
const def = await defaultAddress(user.token)
|
|
expect(def.id).toBe(created.id)
|
|
expect(def.isDefault).toBe(true)
|
|
})
|
|
|
|
it('UADDR-15: getUserAddresses returns only own rows in insertion order', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const a1: any = await createAddress(user.token)
|
|
const a2: any = await createAddress(user.token, { isDefault: false })
|
|
const a3: any = await createAddress(user.token, { isDefault: false })
|
|
|
|
const rows = await listAddresses(user.token)
|
|
expect(rows.map((r) => r.id)).toEqual([a1.id, a2.id, a3.id])
|
|
// Ownership: every row belongs to the caller.
|
|
expect(rows.every((r) => r.userId === user.userId)).toBe(true)
|
|
|
|
const other = await registerThrowawayUser()
|
|
expect(await listAddresses(other.token)).toEqual([])
|
|
})
|
|
|
|
it('UADDR-16: updateAddress round-trips every editable field', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const address: any = await createAddress(user.token)
|
|
const line2 = `Floor 2 ${uniqueSuffix()}`
|
|
|
|
const res: any = await apiClient(user.token).user.address.updateAddress.mutate(
|
|
addrInput({
|
|
id: address.id,
|
|
name: 'Updated Home',
|
|
phone: '6123456789',
|
|
addressLine1: '99 Updated Road',
|
|
addressLine2: line2,
|
|
city: 'Hyderabad',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
})
|
|
)
|
|
|
|
expect(res.success).toBe(true)
|
|
expect(res.data).toEqual(
|
|
expect.objectContaining({
|
|
id: address.id,
|
|
name: 'Updated Home',
|
|
phone: '6123456789',
|
|
addressLine1: '99 Updated Road',
|
|
addressLine2: line2,
|
|
city: 'Hyderabad',
|
|
state: 'Telangana',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('UADDR-17: an update that omits isDefault preserves the flag', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const address: any = await createAddress(user.token)
|
|
expect(address.isDefault).toBe(true)
|
|
|
|
const input: any = addrInput({ id: address.id, name: 'No Default Sent' })
|
|
delete input.isDefault
|
|
const res: any = await apiClient(user.token).user.address.updateAddress.mutate(input)
|
|
|
|
expect(res.data.isDefault).toBe(true)
|
|
})
|
|
|
|
it('UADDR-18: updating a non-default address to default unsets the old one', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const a1: any = await createAddress(user.token)
|
|
const a2: any = await createAddress(user.token, { isDefault: false })
|
|
|
|
await apiClient(user.token).user.address.updateAddress.mutate(
|
|
addrInput({ id: a2.id, addressLine1: '2 Now Default', isDefault: true })
|
|
)
|
|
|
|
const rows = await listAddresses(user.token)
|
|
const byId = Object.fromEntries(rows.map((r) => [r.id, r.isDefault]))
|
|
expect(byId[a2.id]).toBe(true)
|
|
expect(byId[a1.id]).toBe(false)
|
|
})
|
|
|
|
it('UADDR-19 (current): updateAddress keeps vagueBackfilledCoords false', async () => {
|
|
// The flag is only ever set by a non-API backfill; no endpoint can make it
|
|
// true, so the "coords changed clears the flag" branch is unobservable. The
|
|
// observable contract is that it stays false across a coordinate change.
|
|
const user = await registerThrowawayUser()
|
|
const address: any = await createAddress(user.token)
|
|
expect(address.vagueBackfilledCoords).toBe(false)
|
|
|
|
const res: any = await apiClient(user.token).user.address.updateAddress.mutate(
|
|
addrInput({
|
|
id: address.id,
|
|
latitude: COORDS.region1Bound2.lat,
|
|
longitude: COORDS.region1Bound2.lng,
|
|
})
|
|
)
|
|
expect(res.data.vagueBackfilledCoords).toBe(false)
|
|
expect(res.data.latitude).toBeCloseTo(COORDS.region1Bound2.lat, 6)
|
|
// Bounds overlap and the resolver returns the lowest-priority containing
|
|
// bound, so region1Bound2 still resolves to bound 1.
|
|
expect(res.data.boundId).toBe(1)
|
|
})
|
|
|
|
it('UADDR-20: updating coordinates can move the address to another region/bound', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const address: any = await createAddress(user.token)
|
|
expect(address.regionId).toBe(1)
|
|
|
|
const res: any = await apiClient(user.token).user.address.updateAddress.mutate(
|
|
addrInput({
|
|
id: address.id,
|
|
regionId: REGION_2.regionId,
|
|
latitude: COORDS.region2Bound3.lat,
|
|
longitude: COORDS.region2Bound3.lng,
|
|
})
|
|
)
|
|
|
|
expect(res.data.regionId).toBe(REGION_2.regionId)
|
|
expect(res.data.boundId).toBe(REGION_2.boundId)
|
|
|
|
const stored = (await listAddresses(user.token)).find((r) => r.id === address.id)
|
|
expect(stored.regionId).toBe(REGION_2.regionId)
|
|
expect(stored.boundId).toBe(REGION_2.boundId)
|
|
})
|
|
|
|
it("UADDR-21 (current): updating another user's address is a 500 'Address not found'", async () => {
|
|
// updateAddress throws a raw Error instead of an ApiError(404).
|
|
const owner = await registerThrowawayUser()
|
|
const other = await registerThrowawayUser()
|
|
const address: any = await createAddress(owner.token)
|
|
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(other.token).user.address.updateAddress.mutate(
|
|
addrInput({ id: address.id }) as any
|
|
),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Address not found'
|
|
)
|
|
})
|
|
|
|
it('UADDR-22 (current): updating a missing address is a 500', async () => {
|
|
const user = await registerThrowawayUser()
|
|
await createAddress(user.token)
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(user.token).user.address.updateAddress.mutate(
|
|
addrInput({ id: 99_999_999 }) as any
|
|
),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Address not found'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* deleteAddress */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('user.address.deleteAddress (UADDR-23..30)', () => {
|
|
it('UADDR-23: deleting the only address is 409 CONFLICT', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const address: any = await createAddress(user.token)
|
|
await expectTrpcError(
|
|
() => apiClient(user.token).user.address.deleteAddress.mutate({ id: address.id }),
|
|
'CONFLICT',
|
|
'only address'
|
|
)
|
|
})
|
|
|
|
it('UADDR-24: deleting the default address is 409 CONFLICT', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const a1: any = await createAddress(user.token)
|
|
await createAddress(user.token, { isDefault: false })
|
|
|
|
await expectTrpcError(
|
|
() => apiClient(user.token).user.address.deleteAddress.mutate({ id: a1.id }),
|
|
'CONFLICT',
|
|
'Cannot delete default address'
|
|
)
|
|
})
|
|
|
|
it('UADDR-25: deleting an address with an ongoing order is 409 CONFLICT', async () => {
|
|
const fixture = await createCatalogOrderFixture({ price: 349 })
|
|
// An extra, non-default address is the one the order delivers to.
|
|
const ordered: any = await createAddress(fixture.user.token, { isDefault: false })
|
|
const placed = await placeOrder(fixture.user.token, {
|
|
addressId: ordered.id,
|
|
skuId: fixture.product.skuId,
|
|
slotId: fixture.slot.slotId,
|
|
})
|
|
expect(placed.length).toBeGreaterThan(0)
|
|
|
|
await expectTrpcError(
|
|
() => apiClient(fixture.user.token).user.address.deleteAddress.mutate({ id: ordered.id }),
|
|
'CONFLICT',
|
|
'ongoing order'
|
|
)
|
|
})
|
|
|
|
it('UADDR-26: a non-default address with another address can be deleted', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const a1: any = await createAddress(user.token)
|
|
const a2: any = await createAddress(user.token, { isDefault: false })
|
|
|
|
const res: any = await apiClient(user.token).user.address.deleteAddress.mutate({ id: a2.id })
|
|
expect(res.success).toBe(true)
|
|
|
|
const rows = await listAddresses(user.token)
|
|
expect(rows.map((r) => r.id)).toEqual([a1.id])
|
|
})
|
|
|
|
it("UADDR-27: deleting another user's address is 404 NOT_FOUND", async () => {
|
|
const owner = await registerThrowawayUser()
|
|
const other = await registerThrowawayUser()
|
|
const address: any = await createAddress(owner.token)
|
|
|
|
await expectTrpcError(
|
|
() => apiClient(other.token).user.address.deleteAddress.mutate({ id: address.id }),
|
|
'NOT_FOUND'
|
|
)
|
|
expect((await listAddresses(owner.token)).some((r) => r.id === address.id)).toBe(true)
|
|
})
|
|
|
|
it('UADDR-28 (current): a region-2 delete leaves the region-1 default in place', async () => {
|
|
// The handler reconciles the user's region associations and moves the push
|
|
// token, but neither user_regions nor notif_creds.region_id is exposed by any
|
|
// API, so only the address-level effect is observable here.
|
|
const user = await registerThrowawayUser()
|
|
const region1: any = await createAddress(user.token)
|
|
const region2: any = await createAddress(user.token, { regionId: 2, isDefault: false })
|
|
expect(region2.regionId).toBe(2)
|
|
|
|
const res: any = await apiClient(user.token).user.address.deleteAddress.mutate({ id: region2.id })
|
|
expect(res.success).toBe(true)
|
|
|
|
const rows = await listAddresses(user.token)
|
|
expect(rows.map((r) => r.id)).toEqual([region1.id])
|
|
const def = await defaultAddress(user.token)
|
|
expect(def.regionId).toBe(1)
|
|
})
|
|
|
|
it('UADDR-29: no token is 401 on the address reads/writes', async () => {
|
|
await expectTrpcError(
|
|
() => apiClient().user.address.getUserAddresses.query(),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() => apiClient().user.address.getDefaultAddress.query(),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() => apiClient().user.address.deleteAddress.mutate({ id: 1 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
})
|
|
|
|
it('UADDR-30: an invalid id / region is BAD_REQUEST', async () => {
|
|
const user = await registerThrowawayUser()
|
|
await createAddress(user.token)
|
|
|
|
await expectTrpcError(
|
|
() => apiClient(user.token).user.address.deleteAddress.mutate({ id: 0 }),
|
|
'BAD_REQUEST'
|
|
)
|
|
await expectTrpcError(
|
|
() =>
|
|
apiClient(user.token).user.address.createAddress.mutate(addrInput({ regionId: -1 })),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
})
|