mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 21:00:09 +00:00
94dbaf073b
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.
55 lines
1.7 KiB
TypeScript
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);
|
|
});
|
|
});
|