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;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,14 @@
|
||||
* Note: Uses real crypto implementation to test actual encryption/decryption
|
||||
*/
|
||||
|
||||
import { saveTokens, getTokens, clearTokens, isStorageAvailable } from '@/lib/auth/storage';
|
||||
import {
|
||||
saveTokens,
|
||||
getTokens,
|
||||
clearTokens,
|
||||
isStorageAvailable,
|
||||
getStorageMethod,
|
||||
setStorageMethod,
|
||||
} from '@/lib/auth/storage';
|
||||
import { clearEncryptionKey } from '@/lib/auth/crypto';
|
||||
|
||||
describe('Storage Module', () => {
|
||||
@@ -127,5 +134,82 @@ describe('Storage Module', () => {
|
||||
|
||||
Storage.prototype.setItem = originalSetItem;
|
||||
});
|
||||
|
||||
it('should handle getStorageMethod errors gracefully', () => {
|
||||
const originalGetItem = localStorage.getItem;
|
||||
localStorage.getItem = jest.fn(() => {
|
||||
throw new Error('Storage access denied');
|
||||
});
|
||||
|
||||
// Should still return default method
|
||||
const method = getStorageMethod();
|
||||
expect(method).toBe('localStorage');
|
||||
|
||||
localStorage.getItem = originalGetItem;
|
||||
});
|
||||
|
||||
it('should handle setStorageMethod errors gracefully', () => {
|
||||
const originalSetItem = localStorage.setItem;
|
||||
localStorage.setItem = jest.fn(() => {
|
||||
throw new Error('Storage quota exceeded');
|
||||
});
|
||||
|
||||
// Should not throw
|
||||
expect(() => setStorageMethod('cookie')).not.toThrow();
|
||||
|
||||
localStorage.setItem = originalSetItem;
|
||||
});
|
||||
|
||||
it('should handle clearTokens localStorage errors gracefully', async () => {
|
||||
const originalRemoveItem = localStorage.removeItem;
|
||||
localStorage.removeItem = jest.fn(() => {
|
||||
throw new Error('Storage access denied');
|
||||
});
|
||||
|
||||
// Should not throw
|
||||
await expect(clearTokens()).resolves.not.toThrow();
|
||||
|
||||
localStorage.removeItem = originalRemoveItem;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Storage method handling', () => {
|
||||
it('should return stored method when set to cookie', () => {
|
||||
setStorageMethod('cookie');
|
||||
expect(getStorageMethod()).toBe('cookie');
|
||||
});
|
||||
|
||||
it('should return stored method when set to localStorage', () => {
|
||||
setStorageMethod('localStorage');
|
||||
expect(getStorageMethod()).toBe('localStorage');
|
||||
});
|
||||
|
||||
it('should handle cookie method in saveTokens', async () => {
|
||||
setStorageMethod('cookie');
|
||||
|
||||
const tokens = {
|
||||
accessToken: 'test.access.token',
|
||||
refreshToken: 'test.refresh.token',
|
||||
};
|
||||
|
||||
// Should not throw and return immediately (cookie handling is server-side)
|
||||
await expect(saveTokens(tokens)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('should handle cookie method in getTokens', async () => {
|
||||
setStorageMethod('cookie');
|
||||
|
||||
// Should return null (cookie reading is server-side)
|
||||
const result = await getTokens();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle cookie method in clearTokens', async () => {
|
||||
setStorageMethod('cookie');
|
||||
|
||||
// Should not throw
|
||||
await expect(clearTokens()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user