adding tests for generateRndKey (#2932)
changing jest.config.js transform option from - transform: { '^.+\\.ts?$': 'ts-jest' }, to transform: { '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }], },
This commit is contained in:
parent
d68b70b850
commit
1a63880d01
@ -34,12 +34,11 @@ const saveKeys = async (keys, setError) => {
|
|||||||
setError(error);
|
setError(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
export const generateRndKey = () => {
|
||||||
const generateRndKey = () => {
|
|
||||||
let defaultKey = '';
|
let defaultKey = '';
|
||||||
let isValidStreamKey = false;
|
let isValidStreamKey = false;
|
||||||
const streamKeyRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$%^&*]).{8,192}$/;
|
const streamKeyRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$^&*]).{8,192}$/;
|
||||||
const s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*';
|
const s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$^&*';
|
||||||
|
|
||||||
while (!isValidStreamKey) {
|
while (!isValidStreamKey) {
|
||||||
const temp = Array.apply(20, Array(30))
|
const temp = Array.apply(20, Array(30))
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
transform: { '^.+\\.ts?$': 'ts-jest' },
|
transform: { '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }], },
|
||||||
testEnvironment: 'node',
|
testEnvironment: 'node',
|
||||||
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
|
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
|
||||||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
||||||
|
26
web/tests/streamkeys.test.ts
Normal file
26
web/tests/streamkeys.test.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { generateRndKey } from '../components/admin/config/server/StreamKeys';
|
||||||
|
describe('generateRndKey', () => {
|
||||||
|
test('should generate a key that matches the regular expression', () => {
|
||||||
|
const key = generateRndKey();
|
||||||
|
const regex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$^&*]).{8,192}$/;
|
||||||
|
expect(regex.test(key)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns a string', () => {
|
||||||
|
const result = generateRndKey();
|
||||||
|
expect(typeof result).toBe('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('should generate a key of length between 8 and 192 characters', () => {
|
||||||
|
const key = generateRndKey();
|
||||||
|
expect(key.length).toBeGreaterThanOrEqual(8);
|
||||||
|
expect(key.length).toBeLessThanOrEqual(192);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should generate a unique key on each invocation', () => {
|
||||||
|
const key1 = generateRndKey();
|
||||||
|
const key2 = generateRndKey();
|
||||||
|
expect(key1).not.toBe(key2);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user