forked from lunanting/cangku
50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
_test: {
|
|
buildKeyPreview,
|
|
generateApiKey,
|
|
hashApiKey,
|
|
maskApiKey,
|
|
normalizeApiKeyInput,
|
|
normalizeAllowedIps,
|
|
},
|
|
} = require('../src/services/selectionFissionApiKeyService');
|
|
|
|
test('selection fission API key generator returns a strong scoped key', () => {
|
|
const first = generateApiKey();
|
|
const second = generateApiKey();
|
|
|
|
assert.match(first, /^sf_live_[A-Za-z0-9_-]{40,}$/);
|
|
assert.notEqual(first, second);
|
|
});
|
|
|
|
test('selection fission API key IP whitelist normalizes and validates addresses', () => {
|
|
assert.deepEqual(
|
|
normalizeAllowedIps('203.0.113.10,\n::ffff:127.0.0.1;2001:db8::10'),
|
|
['203.0.113.10', '127.0.0.1', '2001:db8::10']
|
|
);
|
|
assert.throws(() => normalizeAllowedIps('not-an-ip'), /无效地址/);
|
|
});
|
|
|
|
test('selection fission API key helpers expose only a masked preview', () => {
|
|
const apiKey = 'sf_live_1234567890abcdefghijklmnop';
|
|
const preview = buildKeyPreview(apiKey);
|
|
|
|
assert.equal(preview.keyPrefix, apiKey.slice(0, 10));
|
|
assert.equal(preview.keySuffix, apiKey.slice(-4));
|
|
assert.equal(maskApiKey(preview.keyPrefix, preview.keySuffix), `${apiKey.slice(0, 10)}••••••••${apiKey.slice(-4)}`);
|
|
assert.equal(hashApiKey(apiKey).length, 64);
|
|
assert.equal(hashApiKey(apiKey), hashApiKey(apiKey));
|
|
});
|
|
|
|
test('custom selection fission API keys must be at least 16 characters', () => {
|
|
assert.throws(
|
|
() => normalizeApiKeyInput('too-short', { required: true }),
|
|
/长度不能少于 16 位/
|
|
);
|
|
assert.equal(normalizeApiKeyInput('1234567890abcdef', { required: true }), '1234567890abcdef');
|
|
});
|