Add extensive form tests and enhanced error handling for auth components.
- Introduced comprehensive tests for `RegisterForm`, `PasswordResetRequestForm`, and `PasswordResetConfirmForm` covering successful submissions, validation errors, and API error handling. - Refactored forms to handle unexpected errors gracefully and improve test coverage for edge cases. - Updated `crypto` and `storage` modules with robust error handling for storage issues and encryption key management. - Removed unused `axios-mock-adapter` dependency for cleaner dependency management.
This commit is contained in:
@@ -105,5 +105,53 @@ describe('Crypto Utilities', () => {
|
||||
expect(decrypted1).toBe(plaintext);
|
||||
expect(decrypted2).toBe(plaintext);
|
||||
});
|
||||
|
||||
it('should handle corrupted stored key gracefully', async () => {
|
||||
// Store invalid key data in sessionStorage
|
||||
sessionStorage.setItem('auth_encryption_key', 'invalid-json-data{]');
|
||||
|
||||
// Should generate new key and encrypt successfully
|
||||
const plaintext = 'test data';
|
||||
const encrypted = await encryptData(plaintext);
|
||||
const decrypted = await decryptData(encrypted);
|
||||
|
||||
expect(decrypted).toBe(plaintext);
|
||||
// Key should have been regenerated
|
||||
expect(sessionStorage.getItem('auth_encryption_key')).not.toBe('invalid-json-data{]');
|
||||
});
|
||||
|
||||
it('should handle sessionStorage.setItem errors when storing key', async () => {
|
||||
// Mock setItem to throw error
|
||||
const originalSetItem = sessionStorage.setItem;
|
||||
sessionStorage.setItem = jest.fn(() => {
|
||||
throw new Error('Storage quota exceeded');
|
||||
});
|
||||
|
||||
// Should still work even if key can't be stored
|
||||
const plaintext = 'test data';
|
||||
const encrypted = await encryptData(plaintext);
|
||||
|
||||
// Restore for decryption (which needs to get the key)
|
||||
sessionStorage.setItem = originalSetItem;
|
||||
|
||||
// Should succeed despite storage error (key is kept in memory for the session)
|
||||
expect(encrypted).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error handling', () => {
|
||||
it('should handle clearEncryptionKey errors gracefully', () => {
|
||||
// Mock removeItem to throw error
|
||||
const originalRemoveItem = sessionStorage.removeItem;
|
||||
sessionStorage.removeItem = jest.fn(() => {
|
||||
throw new Error('Storage access denied');
|
||||
});
|
||||
|
||||
// Should not throw, just warn
|
||||
expect(() => clearEncryptionKey()).not.toThrow();
|
||||
|
||||
// Restore
|
||||
sessionStorage.removeItem = originalRemoveItem;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user