2023-10-26 02:21:53 -07:00
|
|
|
import Container from 'typedi';
|
2024-09-30 06:38:56 -07:00
|
|
|
|
2023-10-26 02:21:53 -07:00
|
|
|
import { Cipher } from '@/Cipher';
|
2024-09-30 06:38:56 -07:00
|
|
|
import { InstanceSettings } from '@/InstanceSettings';
|
|
|
|
|
2023-10-26 02:21:53 -07:00
|
|
|
import { mockInstance } from './utils';
|
|
|
|
|
|
|
|
describe('Cipher', () => {
|
|
|
|
mockInstance(InstanceSettings, { encryptionKey: 'test_key' });
|
|
|
|
const cipher = Container.get(Cipher);
|
|
|
|
|
|
|
|
describe('encrypt', () => {
|
|
|
|
it('should encrypt strings', () => {
|
|
|
|
const encrypted = cipher.encrypt('random-string');
|
|
|
|
const decrypted = cipher.decrypt(encrypted);
|
|
|
|
expect(decrypted).toEqual('random-string');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should encrypt objects', () => {
|
|
|
|
const encrypted = cipher.encrypt({ key: 'value' });
|
|
|
|
const decrypted = cipher.decrypt(encrypted);
|
|
|
|
expect(decrypted).toEqual('{"key":"value"}');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('decrypt', () => {
|
|
|
|
it('should decrypt string', () => {
|
|
|
|
const decrypted = cipher.decrypt('U2FsdGVkX194VEoX27o3+y5jUd1JTTmVwkOKjVhB6Jg=');
|
|
|
|
expect(decrypted).toEqual('random-string');
|
|
|
|
});
|
2023-11-03 05:26:20 -07:00
|
|
|
|
|
|
|
it('should not try to decrypt if the input is shorter than 16 bytes', () => {
|
|
|
|
const decrypted = cipher.decrypt('U2FsdGVkX194VEo');
|
|
|
|
expect(decrypted).toEqual('');
|
|
|
|
});
|
2023-10-26 02:21:53 -07:00
|
|
|
});
|
|
|
|
});
|