0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00
verdaccio/packages/plugins/htpasswd/tests/crypt3.test.ts
Juan Picado 124e5f2de7
chore: migrate htpasswd package vitest (#4889)
* migrate htpasswd package

* update versions

* Update htpasswd.passwords.test.ts
2024-10-07 08:28:30 +02:00

32 lines
1.1 KiB
TypeScript

import { describe, expect, test, vi } from 'vitest';
import { EncryptionMethod, createSalt } from '../src/crypt3';
vi.mock('../src/crypto-utils', async (importOriginal) => ({
...(await importOriginal<typeof import('../src/crypto-utils')>()),
randomBytes: (len: number): { toString: () => string } => {
return {
toString: (): string => '/UEGzD0RxSNDZA=='.substring(0, len),
};
},
}));
describe('createSalt', () => {
test('should match with the correct salt type', () => {
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');
});
test('should fails on unkwon type', () => {
expect(function () {
createSalt('bad' as any);
}).toThrow(/Unknown salt type at crypt3.createSalt: bad/);
});
test('should generate legacy crypt salt by default', () => {
expect(createSalt()).toEqual(createSalt(EncryptionMethod.crypt));
});
});