import { randomUUID } from 'node:crypto'; import { identityApp, type IdentityApp } from './helpers/identity-app'; import { secondActor } from './helpers/commerce'; import { reviewFixture, reviewInput, moderationInput, reviewAuth as auth, } from './helpers/reviews'; describe('verified product reviews', () => { let ctx: IdentityApp; beforeAll(async () => { ctx = await identityApp(true); }, 60000); afterAll(async () => { await ctx?.close(); }); beforeEach(async () => { await ctx.clearLimits(); }); it('requires buyer-owned evidence, records one submission and publishes only after moderation', async () => { const f = await reviewFixture(ctx); const input = reviewInput(f.proof.id); const outsider = await secondActor(ctx); await ctx .api() .post('/api/v1/reviews') .auth(outsider.token, auth) .send(input) .expect(404); const eligible = await ctx .api() .get('/api/v1/reviews/eligibility') .auth(f.buyer.token, auth) .expect(200); expect(eligible.body).toEqual([ expect.objectContaining({ id: f.proof.id, variantId: f.variant.id }), ]); expect(eligible.body[0]).not.toHaveProperty('reference'); const submit = () => ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(input) .expect(201); const [created, retry] = await Promise.all([submit(), submit()]); expect(retry.body.id).toBe(created.body.id); expect(created.body).toMatchObject({ moderationStatus: 'PENDING', purchaseVerified: true, }); await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send({ ...input, rating: 4 }) .expect(409); await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(reviewInput(f.proof.id)) .expect(409); expect((await ctx.api().get(f.publicUrl).expect(200)).body).toEqual([]); const queue = await ctx .api() .get('/api/v1/review-moderation?status=PENDING') .auth(ctx.token, auth) .expect(200); expect(queue.body.map((row: { id: string }) => row.id)).toContain( created.body.id, ); expect( queue.body.find((row: { id: string }) => row.id === created.body.id) .verification, ).not.toHaveProperty('reference'); const decision = moderationInput(); const moderate = () => ctx .api() .post(`/api/v1/review-moderation/${created.body.id}/decision`) .auth(ctx.token, auth) .send(decision) .expect(201); const [approved, approvalRetry] = await Promise.all([ moderate(), moderate(), ]); expect(approvalRetry.body.events).toHaveLength(1); expect(approved.body.events[0].kind).toBe('APPROVED'); const publicRows = (await ctx.api().get(f.publicUrl).expect(200)).body; expect(publicRows).toEqual([ expect.objectContaining({ id: created.body.id, rating: 5, verifiedPurchase: true, verificationMethod: 'MANUAL', displayName: input.displayName, }), ]); for (const field of [ 'userId', 'organizationId', 'verificationId', 'verification', 'events', 'requestHash', ]) expect(publicRows[0]).not.toHaveProperty(field); expect( ( await ctx .api() .get('/api/v1/reviews/eligibility') .auth(f.buyer.token, auth) .expect(200) ).body, ).toEqual([]); expect( ( await ctx .api() .get('/api/v1/reviews') .auth(outsider.token, auth) .expect(200) ).body, ).toEqual([]); expect( await ctx.db.auditEvent.count({ where: { targetId: created.body.id, action: { startsWith: 'review.' } }, }), ).toBe(2); }); it('prohibits author moderation and audits permanent hiding of approved content', async () => { const f = await reviewFixture(ctx, ['reviews.moderate']); const created = await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(reviewInput(f.proof.id)) .expect(201); const url = `/api/v1/review-moderation/${created.body.id}/decision`; expect( ( await ctx .api() .post(url) .auth(f.buyer.token, auth) .send(moderationInput()) .expect(403) ).body.code, ).toBe('REVIEW_SELF_MODERATION'); await ctx .api() .post(url) .auth(ctx.token, auth) .send(moderationInput('HIDDEN')) .expect(409); const approve = moderationInput(); await ctx.api().post(url).auth(ctx.token, auth).send(approve).expect(201); await ctx .api() .post(url) .auth(ctx.token, auth) .send({ ...approve, note: 'Conflicting retry' }) .expect(409); await ctx .api() .post(url) .auth(ctx.token, auth) .send(moderationInput('REJECTED')) .expect(409); const hide = moderationInput('HIDDEN'); await ctx.api().post(url).auth(ctx.token, auth).send(hide).expect(201); await ctx.api().post(url).auth(ctx.token, auth).send(hide).expect(201); expect((await ctx.api().get(f.publicUrl).expect(200)).body).toEqual([]); const own = await ctx .api() .get('/api/v1/reviews') .auth(f.buyer.token, auth) .expect(200); expect(own.body[0].moderationStatus).toBe('HIDDEN'); await ctx .api() .post(url) .auth(ctx.token, auth) .send(moderationInput()) .expect(409); }); it('keeps rejected reviews private and redacts reviews when catalog eligibility changes', async () => { const f = await reviewFixture(ctx); const review = await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(reviewInput(f.proof.id)) .expect(201); await ctx .api() .post(`/api/v1/review-moderation/${review.body.id}/decision`) .auth(ctx.token, auth) .send(moderationInput('REJECTED')) .expect(201); expect((await ctx.api().get(f.publicUrl).expect(200)).body).toEqual([]); const approvedFixture = await reviewFixture(ctx); const approved = await ctx .api() .post('/api/v1/reviews') .auth(approvedFixture.buyer.token, auth) .send(reviewInput(approvedFixture.proof.id)) .expect(201); await ctx .api() .post(`/api/v1/review-moderation/${approved.body.id}/decision`) .auth(ctx.token, auth) .send(moderationInput()) .expect(201); await ctx.db.productVariant.update({ where: { id: approvedFixture.variant.id }, data: { active: false }, }); expect( (await ctx.api().get(approvedFixture.publicUrl).expect(200)).body, ).toEqual([]); await ctx .api() .patch(`/api/v1/products/${f.product.id}/status`) .auth(ctx.token, auth) .send({ status: 'DRAFT' }) .expect(200); await ctx.api().get(f.publicUrl).expect(404); const fresh = await reviewFixture(ctx); await ctx .api() .patch(`/api/v1/products/${fresh.product.id}/status`) .auth(ctx.token, auth) .send({ status: 'DRAFT' }) .expect(200); expect( ( await ctx .api() .post('/api/v1/reviews') .auth(fresh.buyer.token, auth) .send(reviewInput(fresh.proof.id)) .expect(409) ).body.code, ).toBe('REVIEW_VARIANT_UNAVAILABLE'); }); it('enforces permissions, scoped moderation and input boundaries', async () => { await ctx.api().get('/api/v1/reviews').expect(401); const f = await reviewFixture(ctx); const input = reviewInput(f.proof.id); for (const body of [ { ...input, rating: 0 }, { ...input, rating: 6 }, { ...input, rating: 1.5 }, { ...input, userId: ctx.owner.userId }, { ...input, body: '' }, ]) await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(body) .expect(400); await ctx .api() .get('/api/v1/review-moderation') .auth(f.buyer.token, auth) .expect(403); await ctx .api() .get('/api/v1/review-verifications') .auth(f.buyer.token, auth) .expect(403); const created = await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(input) .expect(201); const outsider = await secondActor(ctx, false, [ 'reviews.read', 'reviews.moderate', ]); await ctx .api() .post(`/api/v1/review-moderation/${created.body.id}/decision`) .auth(outsider.token, auth) .send(moderationInput()) .expect(404); await ctx .api() .get( `/api/v1/storefront/${outsider.organizationId}/products/${f.product.id}/reviews`, ) .expect(404); await ctx.api().get(`${f.publicUrl}?limit=101`).expect(400); await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(reviewInput(randomUUID())) .expect(404); const docs = await ctx.api().get('/api/docs-json').expect(200); expect( docs.body.paths[ '/api/v1/storefront/{organizationId}/products/{productId}/reviews' ], ).toBeDefined(); }); });