2021-10-29 10:33:05 -05:00
|
|
|
import { EncryptionMethod, createSalt } from '../src/crypt3';
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
jest.mock('crypto', () => {
|
|
|
|
return {
|
2021-01-30 17:41:33 -05:00
|
|
|
randomBytes: (len: number): { toString: () => string } => {
|
2020-08-19 13:27:35 -05:00
|
|
|
return {
|
2021-01-30 17:41:33 -05:00
|
|
|
toString: (): string => '/UEGzD0RxSNDZA=='.substring(0, len),
|
2020-08-19 13:27:35 -05:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createSalt', () => {
|
|
|
|
test('should match with the correct salt type', () => {
|
2021-01-30 17:41:33 -05:00
|
|
|
expect(createSalt(EncryptionMethod.crypt)).toEqual('/U');
|
|
|
|
expect(createSalt(EncryptionMethod.md5)).toEqual('$1$/UEGzD0RxS');
|
|
|
|
expect(createSalt(EncryptionMethod.blowfish)).toEqual('$2a$/UEGzD0RxS');
|
|
|
|
expect(createSalt(EncryptionMethod.sha256)).toEqual('$5$/UEGzD0RxS');
|
|
|
|
expect(createSalt(EncryptionMethod.sha512)).toEqual('$6$/UEGzD0RxS');
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails on unkwon type', () => {
|
|
|
|
expect(function () {
|
2021-01-30 17:41:33 -05:00
|
|
|
createSalt('bad' as any);
|
2020-08-19 13:27:35 -05:00
|
|
|
}).toThrow(/Unknown salt type at crypt3.createSalt: bad/);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should generate legacy crypt salt by default', () => {
|
2021-01-30 17:41:33 -05:00
|
|
|
expect(createSalt()).toEqual(createSalt(EncryptionMethod.crypt));
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
});
|