manicanldes-backend/test/campaign-offers.spec.ts

101 lines
2.9 KiB
TypeScript

import { identityApp, type IdentityApp } from './helpers/identity-app';
import { campaignDiscountFixture } from './helpers/campaign-discounts';
describe('public campaign offer terms', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('exposes active offer terms without internal IDs or usage limits', async () => {
const f = await campaignDiscountFixture(ctx);
const publicRow = (await ctx.api().get(f.publicUrl).expect(200)).body;
expect(publicRow.offer).toMatchObject({
code: f.coupon.code,
kind: 'PERCENT',
percentBps: 1000,
amount: null,
currency: 'INR',
minimumSubtotal: '100.00',
});
for (const field of [
'id',
'campaignId',
'organizationId',
'maxUses',
'perUserLimit',
'active',
])
expect(publicRow.offer).not.toHaveProperty(field);
const rows = (
await ctx
.api()
.get(
`/api/v1/storefront/${ctx.owner.organizationId}/campaigns?limit=100`,
)
.expect(200)
).body;
expect(
rows.find((row: { id: string }) => row.id === f.campaignId).offer.code,
).toBe(f.coupon.code);
});
it('suppresses paused offers while retaining collection discovery and blocking new discount use', async () => {
const f = await campaignDiscountFixture(ctx, { fixed: '100.00' });
expect(
(await ctx.api().get(f.publicUrl).expect(200)).body.offer.amount,
).toBe('100.00');
await ctx
.api()
.patch(`/api/v1/coupons/${f.coupon.id}/status`)
.auth(ctx.token, auth)
.send({ active: false })
.expect(200);
expect(
(await ctx.api().get(f.publicUrl).expect(200)).body,
).not.toHaveProperty('offer');
expect(
(
await ctx
.api()
.post('/api/v1/checkout')
.auth(f.actor.token, auth)
.send(f.input)
.expect(409)
).body.code,
).toBe('COUPON_INELIGIBLE');
await ctx
.api()
.patch(`/api/v1/coupons/${f.coupon.id}/status`)
.auth(ctx.token, auth)
.send({ active: true })
.expect(200);
expect((await ctx.api().get(f.publicUrl).expect(200)).body.offer.code).toBe(
f.coupon.code,
);
});
it('does not advertise a coupon before its own window begins', async () => {
const f = await campaignDiscountFixture(ctx, {
couponStartsAt: new Date(Date.now() + 1800000).toISOString(),
couponEndsAt: new Date(Date.now() + 3600000).toISOString(),
});
expect(
(await ctx.api().get(f.publicUrl).expect(200)).body,
).not.toHaveProperty('offer');
await ctx
.api()
.post('/api/v1/checkout')
.auth(f.actor.token, auth)
.send(f.input)
.expect(409);
});
});