Files
ReadMeABook/tests/services/encryption.service.test.ts
T
kikootwo 94dbaf073b Add backend unit test framework and modularize settings UI
Introduced a Vitest-based backend unit testing framework with supporting scripts, helpers, and GitHub Actions integration. Refactored the admin settings page to a modular architecture, splitting monolithic logic into feature-specific tabs and hooks for improved maintainability and testability. Updated documentation to reflect the new testing setup and settings architecture, and added new dependencies for testing utilities.
2026-01-28 11:41:59 -05:00

55 lines
1.7 KiB
TypeScript

/**
* Component: Encryption Service Tests
* Documentation: documentation/backend/services/config.md
*/
import { afterEach, describe, expect, it, vi } from 'vitest';
const ORIGINAL_KEY = process.env.CONFIG_ENCRYPTION_KEY;
afterEach(() => {
process.env.CONFIG_ENCRYPTION_KEY = ORIGINAL_KEY;
vi.resetModules();
});
describe('EncryptionService', () => {
it('throws when encryption key is missing', async () => {
delete process.env.CONFIG_ENCRYPTION_KEY;
vi.resetModules();
const { EncryptionService } = await import('@/lib/services/encryption.service');
expect(() => new EncryptionService()).toThrow(/CONFIG_ENCRYPTION_KEY/);
});
it('encrypts and decrypts values', async () => {
process.env.CONFIG_ENCRYPTION_KEY = 'a'.repeat(32);
vi.resetModules();
const { EncryptionService } = await import('@/lib/services/encryption.service');
const service = new EncryptionService();
const encrypted = service.encrypt('secret');
const decrypted = service.decrypt(encrypted);
expect(decrypted).toBe('secret');
});
it('rejects invalid encrypted data formats', async () => {
process.env.CONFIG_ENCRYPTION_KEY = 'b'.repeat(32);
vi.resetModules();
const { EncryptionService } = await import('@/lib/services/encryption.service');
const service = new EncryptionService();
expect(() => service.decrypt('invalid')).toThrow(/Decryption failed/);
});
it('generates a random key', async () => {
const { EncryptionService } = await import('@/lib/services/encryption.service');
const key = EncryptionService.generateKey();
expect(typeof key).toBe('string');
expect(key.length).toBeGreaterThan(40);
});
});