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('manual review purchase verification', () => { let ctx: IdentityApp; beforeAll(async () => { ctx = await identityApp(); }, 60000); afterAll(async () => { await ctx?.close(); }); beforeEach(async () => { await ctx.clearLimits(); }); it('replays evidence safely and prohibits self verification, evidence reuse and foreign customers', async () => { const f = await reviewFixture(ctx); const verify = (body = f.proofInput, token = ctx.token) => ctx .api() .post('/api/v1/review-verifications') .auth(token, auth) .send(body); expect((await verify().expect(201)).body.id).toBe(f.proof.id); expect( ( await verify({ ...f.proofInput, note: 'Different evidence' }).expect( 409, ) ).body.code, ).toBe('REVIEW_VERIFICATION_REPLAY_CONFLICT'); await verify({ ...f.proofInput, idempotencyKey: randomUUID() }).expect(409); await verify(f.proofInput, f.buyer.token).expect(403); for (const userId of [ctx.owner.userId, ctx.owner.userId.toUpperCase()]) expect( ( await verify({ ...f.proofInput, userId, idempotencyKey: randomUUID(), }).expect(403) ).body.code, ).toBe('REVIEW_SELF_VERIFICATION'); const foreign = await secondActor(ctx, false, ['reviews.verify']); await verify( { ...f.proofInput, idempotencyKey: randomUUID() }, foreign.token, ).expect(404); await verify({ ...f.proofInput, userId: foreign.userId, idempotencyKey: randomUUID(), }).expect(404); await verify({ ...f.proofInput, variantId: randomUUID(), idempotencyKey: randomUUID(), }).expect(404); const listed = await ctx .api() .get(`/api/v1/review-verifications?userId=${f.buyer.userId}&limit=1`) .auth(ctx.token, auth) .expect(200); expect(listed.body[0].id).toBe(f.proof.id); }); it('revokes eligibility idempotently and automatically withdraws public reviews', 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()) .expect(201); const revoke = { reason: 'External purchase evidence invalidated', idempotencyKey: randomUUID(), }; const url = `/api/v1/review-verifications/${f.proof.id}/revoke`; const first = await ctx .api() .post(url) .auth(ctx.token, auth) .send(revoke) .expect(201); expect(first.body.revocation.reason).toBe(revoke.reason); await ctx.api().post(url).auth(ctx.token, auth).send(revoke).expect(201); await ctx .api() .post(url) .auth(ctx.token, auth) .send({ ...revoke, reason: 'Changed reason' }) .expect(409); await ctx .api() .post(url) .auth(ctx.token, auth) .send({ ...revoke, idempotencyKey: randomUUID() }) .expect(409); expect((await ctx.api().get(f.publicUrl).expect(200)).body).toEqual([]); expect( ( await ctx .api() .get('/api/v1/reviews') .auth(f.buyer.token, auth) .expect(200) ).body[0].purchaseVerified, ).toBe(false); const fresh = await reviewFixture(ctx); await ctx .api() .post(`/api/v1/review-verifications/${fresh.proof.id}/revoke`) .auth(ctx.token, auth) .send({ ...revoke, idempotencyKey: randomUUID() }) .expect(201); expect( ( await ctx .api() .post('/api/v1/reviews') .auth(fresh.buyer.token, auth) .send(reviewInput(fresh.proof.id)) .expect(409) ).body.code, ).toBe('REVIEW_VERIFICATION_REVOKED'); const pending = await reviewFixture(ctx); const submitted = await ctx .api() .post('/api/v1/reviews') .auth(pending.buyer.token, auth) .send(reviewInput(pending.proof.id)) .expect(201); await ctx .api() .post(`/api/v1/review-verifications/${pending.proof.id}/revoke`) .auth(ctx.token, auth) .send({ ...revoke, idempotencyKey: randomUUID() }) .expect(201); await ctx .api() .post(`/api/v1/review-moderation/${submitted.body.id}/decision`) .auth(ctx.token, auth) .send(moderationInput()) .expect(409); }); it('enforces immutable records, independent moderation and proof ownership in the database', async () => { const f = await reviewFixture(ctx); const created = await ctx .api() .post('/api/v1/reviews') .auth(f.buyer.token, auth) .send(reviewInput(f.proof.id)) .expect(201); await expect( ctx.executeSql( `UPDATE product_reviews SET rating = 1 WHERE id = '${created.body.id}'`, ), ).rejects.toThrow('immutable'); await expect( ctx.executeSql( `DELETE FROM review_purchase_verifications WHERE id = '${f.proof.id}'`, ), ).rejects.toThrow('immutable'); await expect( ctx.executeSql(`INSERT INTO product_review_moderation (id, organization_id, review_id, actor_id, sequence, kind, note, idempotency_key, request_hash) VALUES ('${randomUUID()}', '${ctx.owner.organizationId}', '${created.body.id}', '${f.buyer.userId}', 1, 'APPROVED', 'Self moderation', '${randomUUID()}', '${'a'.repeat(64)}')`), ).rejects.toThrow('independent moderator'); const other = await secondActor(ctx); await expect( ctx.executeSql(`INSERT INTO product_reviews (id, organization_id, user_id, variant_id, verification_id, rating, display_name, title, body, idempotency_key, request_hash) VALUES ('${randomUUID()}', '${ctx.owner.organizationId}', '${other.userId}', '${f.variant.id}', '${f.proof.id}', 5, 'Customer', 'Good candle', 'Excellent candle quality', '${randomUUID()}', '${'a'.repeat(64)}')`), ).rejects.toThrow('matching purchase verification'); }); });