diff --git a/web/components/admin/config/server/StreamKeys.tsx b/web/components/admin/config/server/StreamKeys.tsx index 2a680a3fa..21373c446 100644 --- a/web/components/admin/config/server/StreamKeys.tsx +++ b/web/components/admin/config/server/StreamKeys.tsx @@ -34,12 +34,11 @@ const saveKeys = async (keys, setError) => { setError(error); } }; - -const generateRndKey = () => { +export const generateRndKey = () => { let defaultKey = ''; let isValidStreamKey = false; - const streamKeyRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$%^&*]).{8,192}$/; - const s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*'; + const streamKeyRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$^&*]).{8,192}$/; + const s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$^&*'; while (!isValidStreamKey) { const temp = Array.apply(20, Array(30)) diff --git a/web/jest.config.js b/web/jest.config.js index b7737724e..e33a11cd1 100644 --- a/web/jest.config.js +++ b/web/jest.config.js @@ -1,5 +1,5 @@ module.exports = { - transform: { '^.+\\.ts?$': 'ts-jest' }, + transform: { '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }], }, testEnvironment: 'node', testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], diff --git a/web/tests/streamkeys.test.ts b/web/tests/streamkeys.test.ts new file mode 100644 index 000000000..43566f15f --- /dev/null +++ b/web/tests/streamkeys.test.ts @@ -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); + }); +}); \ No newline at end of file