61 lines
2.8 KiB
JavaScript
61 lines
2.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { merchantScope } from '../dist/access.js';
|
|
import { hashPassword, verifyPassword } from '@merchantflow/database';
|
|
import { LeadRateLimiter } from '../dist/public-leads.js';
|
|
test('passwords use distinct salts and reject incorrect values', () => {
|
|
const hash = hashPassword('example-password');
|
|
assert.notEqual(hash, hashPassword('example-password'));
|
|
assert.equal(verifyPassword('example-password', hash), true);
|
|
assert.equal(verifyPassword('wrong', hash), false);
|
|
assert.equal(verifyPassword('wrong', 'broken'), false);
|
|
});
|
|
test('public lead limits expire, bound storage and reserve each submission once', () => {
|
|
let now = 0;
|
|
const limiter = new LeadRateLimiter(() => now, 3);
|
|
for (let i = 0; i < 10; i++) assert.equal(limiter.attemptIp('127.0.0.1'), 0);
|
|
assert.equal(limiter.attemptIp('127.0.0.1'), 900);
|
|
for (let i = 0; i < 3; i++) assert.equal(limiter.reserveEmail('[email protected]', String(i)), 0);
|
|
assert.equal(limiter.reserveEmail('[email protected]', '0'), 0);
|
|
assert.equal(limiter.reserveEmail('[email protected]', 'new'), 900);
|
|
limiter.releaseEmail('[email protected]', '0');
|
|
assert.equal(limiter.reserveEmail('[email protected]', 'new'), 0);
|
|
assert.equal(limiter.attemptIp('another'), 0);
|
|
assert.equal(limiter.attemptIp('map-full'), 900);
|
|
now = 900001;
|
|
assert.equal(limiter.attemptIp('map-full'), 0);
|
|
assert.equal(limiter.reserveEmail('[email protected]', 'another'), 0);
|
|
});
|
|
test('every role receives its exact merchant scope', () => {
|
|
assert.deepEqual(merchantScope({ role: 'HEAD_OF_SALES' }), {});
|
|
assert.deepEqual(merchantScope({ role: 'ACCOUNT_MANAGER', id: 'manager-1' }), {
|
|
ownerId: 'manager-1',
|
|
});
|
|
assert.deepEqual(merchantScope({ role: 'COMPLIANCE_OFFICER' }), {});
|
|
assert.deepEqual(merchantScope({ role: 'unexpected' }), { id: '__no_access__' });
|
|
});
|
|
|
|
test('task day boundaries use the viewer time zone through DST and include exact due instants', async () => {
|
|
const { localDayBounds, taskWhere } = await import('../dist/task-policy.js');
|
|
for (const [date, hours] of [
|
|
['2026-03-08T12:00:00Z', 23],
|
|
['2026-11-01T12:00:00Z', 25],
|
|
]) {
|
|
const { start, end } = localDayBounds(new Date(date), 'America/New_York');
|
|
assert.equal((end - start) / 3600000, hours);
|
|
}
|
|
const now = new Date('2026-09-24T22:00:00Z');
|
|
const { start, end } = localDayBounds(now, 'Europe/Kyiv');
|
|
assert.equal(start.toISOString(), '2026-09-24T21:00:00.000Z');
|
|
assert.equal(end.toISOString(), '2026-09-25T21:00:00.000Z');
|
|
const user = { id: 'manager', role: 'ACCOUNT_MANAGER' };
|
|
assert.deepEqual(
|
|
taskWhere(user, { filter: 'TODAY', timeZone: 'Europe/Kyiv' }, now).AND[1].dueAt,
|
|
{ gte: start, lt: end },
|
|
);
|
|
assert.deepEqual(
|
|
taskWhere(user, { filter: 'UPCOMING', timeZone: 'Europe/Kyiv' }, now).AND[1].dueAt,
|
|
{ gte: end },
|
|
);
|
|
});
|