manicanldes-backend/test/wishlist.spec.ts

190 lines
6.2 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { seedProduct, secondActor } from './helpers/commerce';
describe('private wishlists', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const url = '/api/v1/wishlist';
beforeAll(async () => {
ctx = await identityApp(true);
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
const save = (id: string, token = ctx.token) =>
ctx.api().put(`${url}/${id}`).auth(token, auth);
it('saves and removes idempotently without duplicate audit events', async () => {
const { variant } = await seedProduct(ctx, true);
const [first, retry] = await Promise.all([
save(variant.id).expect(200),
save(variant.id).expect(200),
]);
expect(retry.body).toEqual(first.body);
expect(first.body).toMatchObject({
variantId: variant.id,
available: true,
variant: { sku: variant.sku, price: '499.00', currency: 'INR' },
});
expect(first.body).not.toHaveProperty('userId');
expect(first.body).not.toHaveProperty('organizationId');
expect(
await ctx.db.auditEvent.count({
where: { targetId: first.body.id, action: 'wishlist.saved' },
}),
).toBe(1);
for (let attempt = 0; attempt < 2; attempt++)
await ctx
.api()
.delete(`${url}/${variant.id}`)
.auth(ctx.token, auth)
.expect(204);
expect(
await ctx.db.auditEvent.count({
where: { targetId: first.body.id, action: 'wishlist.removed' },
}),
).toBe(1);
expect((await save(variant.id).expect(200)).body.id).not.toBe(
first.body.id,
);
});
it('keeps user lists private and rejects cross-organization variants', async () => {
const { variant } = await seedProduct(ctx, true);
await save(variant.id).expect(200);
const other = await secondActor(ctx);
expect(
(await ctx.api().get(url).auth(other.token, auth).expect(200)).body,
).toEqual([]);
await ctx
.api()
.delete(`${url}/${variant.id}`)
.auth(other.token, auth)
.expect(204);
expect(
await ctx.db.wishlistItem.count({
where: { userId: ctx.owner.userId, variantId: variant.id },
}),
).toBe(1);
await save(variant.id, other.token).expect(200);
const outsider = await secondActor(ctx, false);
await save(variant.id, outsider.token).expect(404);
expect(
(await ctx.api().get(url).auth(outsider.token, auth).expect(200)).body,
).toEqual([]);
await expect(
ctx.executeSql(`INSERT INTO wishlist_items
(id, organization_id, user_id, variant_id)
VALUES ('${randomUUID()}', '${outsider.organizationId}', '${outsider.userId}', '${variant.id}')`),
).rejects.toThrow();
});
it('retains unavailable items without disclosing unpublished catalog details', async () => {
const { product, variant } = await seedProduct(ctx, true);
await save(variant.id).expect(200);
await ctx
.api()
.patch(`/api/v1/products/${product.id}/status`)
.auth(ctx.token, auth)
.send({ status: 'DRAFT' })
.expect(200);
const actor = await secondActor(ctx);
await save(variant.id, actor.token).expect(404);
expect((await save(variant.id).expect(200)).body).toMatchObject({
available: false,
variant: null,
});
const listed = await ctx.api().get(url).auth(ctx.token, auth).expect(200);
expect(
listed.body.find(
(item: { variantId: string }) => item.variantId === variant.id,
).variant,
).toBeNull();
await ctx
.api()
.delete(`${url}/${variant.id}`)
.auth(ctx.token, auth)
.expect(204);
await save(variant.id).expect(404);
await ctx.db.productVariant.update({
where: { id: variant.id },
data: { active: false },
});
await save(variant.id).expect(404);
});
it('validates pagination, identifiers and ownership injection, and exposes Swagger', async () => {
await ctx.api().get(url).expect(401);
const actor = await secondActor(ctx);
const variants = await Promise.all([
seedProduct(ctx, true),
seedProduct(ctx, true),
]);
for (const { variant } of variants)
await save(variant.id, actor.token).expect(200);
const page = (offset: number) =>
ctx
.api()
.get(`${url}?limit=1&offset=${offset}`)
.auth(actor.token, auth)
.expect(200);
const first = (await page(0)).body;
const next = (await page(1)).body;
expect(first).toHaveLength(1);
expect(next).toHaveLength(1);
expect(first[0].id).not.toBe(next[0].id);
await ctx
.api()
.get(`${url}?userId=${ctx.owner.userId}`)
.auth(actor.token, auth)
.expect(400);
await save(variants[0].variant.id, actor.token)
.send({ userId: ctx.owner.userId })
.expect(400);
await save('invalid').expect(400);
await save(randomUUID()).expect(404);
const docs = await ctx.api().get('/api/docs-json').expect(200);
expect(docs.body.paths['/api/v1/wishlist/{variantId}']).toBeDefined();
});
it('enforces the 100-item quota and allows existing-item retries at the limit', async () => {
const actor = await secondActor(ctx);
const { product, variant } = await seedProduct(ctx, true);
const ids = Array.from({ length: 100 }, () => randomUUID());
await ctx.db.productVariant.createMany({
data: ids.map((id) => ({
id,
productId: product.id,
organizationId: actor.organizationId,
name: 'Quota fixture',
sku: id,
price: '100.00',
})),
});
await ctx.db.wishlistItem.createMany({
data: ids.map((variantId) => ({
organizationId: actor.organizationId,
userId: actor.userId,
variantId,
})),
});
expect((await save(variant.id, actor.token).expect(409)).body.code).toBe(
'WISHLIST_LIMIT',
);
await save(ids[0], actor.token).expect(200);
await ctx
.api()
.delete(`${url}/${ids[0]}`)
.auth(actor.token, auth)
.expect(204);
await save(variant.id, actor.token).expect(200);
expect(
await ctx.db.wishlistItem.count({ where: { userId: actor.userId } }),
).toBe(100);
});
});