Replace crypto tests with comprehensive unit tests for authStore, storage, and configuration modules

- Removed outdated `crypto` tests; added dedicated and structured tests for `authStore`, `storage`, and `app.config`.
- Enhanced test coverage for user and token validation, secure persistence, state management, and configuration parsing.
- Consolidated encryption and storage error handling with thorough validation to ensure SSR-safety and resilience.
- Improved runtime validations for tokens and configuration with stricter type checks and fallback mechanisms.
This commit is contained in:
Felipe Cardoso
2025-10-31 22:25:50 +01:00
parent 6d811747ee
commit 8a7a3b9521
5 changed files with 640 additions and 5 deletions

View File

@@ -134,14 +134,17 @@ export async function getTokens(): Promise<TokenStorage | null> {
}
const decrypted = await decryptData(encrypted);
const tokens = JSON.parse(decrypted) as TokenStorage;
const parsed = JSON.parse(decrypted);
// Validate structure
if (!tokens || typeof tokens !== 'object') {
// Validate structure - must have required fields
if (!parsed || typeof parsed !== 'object' ||
!('accessToken' in parsed) || !('refreshToken' in parsed) ||
(parsed.accessToken !== null && typeof parsed.accessToken !== 'string') ||
(parsed.refreshToken !== null && typeof parsed.refreshToken !== 'string')) {
throw new Error('Invalid token structure');
}
return tokens;
return parsed as TokenStorage;
} catch (error) {
console.error('Failed to retrieve tokens:', error);
// If decryption fails, clear invalid data