37 lines
1.9 KiB
JavaScript
37 lines
1.9 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__' });
|
|
});
|