verae-edge-billing/tests/timestamps-batch.test.ts

48 lines
1.7 KiB
TypeScript

import request from 'supertest';
import { buildApp, DEFAULT_CUSTOMERS } from '../src/app';
import { emptyChannelRates } from '../src/billing-channels';
describe('POST /v1/timestamps batch', () => {
const prevAllowMock = process.env.ALLOW_MOCK_CERTS;
const prevUpstream = process.env.ZAPPIER_UPSTREAM;
beforeAll(() => {
delete process.env.ZAPPIER_UPSTREAM;
process.env.ALLOW_MOCK_CERTS = 'true';
});
afterAll(() => {
if (prevAllowMock === undefined) delete process.env.ALLOW_MOCK_CERTS;
else process.env.ALLOW_MOCK_CERTS = prevAllowMock;
if (prevUpstream === undefined) delete process.env.ZAPPIER_UPSTREAM;
else process.env.ZAPPIER_UPSTREAM = prevUpstream;
});
it('registers many hashes in one call and skips existing for free', async () => {
const table = emptyChannelRates();
table.rates.zapier.individual.timestamp = 10;
const { app } = buildApp({
channelRates: { load: () => table, save: () => {} },
});
const h1 = 'a'.repeat(64);
const h2 = 'b'.repeat(64);
const first = await request(app)
.post('/v1/timestamps')
.set('x-api-key', 'key-ada')
.set('x-billing-channel', 'zapier')
.send({ sha256s: [h1, h2] });
expect(first.status).toBe(202);
expect(first.body.created).toBe(2);
expect(first.body.totalCredits).toBe(20);
expect(first.body.results).toHaveLength(2);
const second = await request(app)
.post('/v1/timestamps')
.set('x-api-key', 'key-ada')
.set('x-billing-channel', 'zapier')
.send({ sha256s: [h1, h2] });
expect(second.status).toBe(202);
expect(second.body.created).toBe(0);
expect(second.body.existing).toBe(2);
expect(second.body.totalCredits).toBe(0);
});
});