|
| 1 | +import { StorageAuthService, IStorageProvider } from '../src/AuthService'; |
| 2 | + |
| 3 | +class SyncMockStorage implements IStorageProvider { |
| 4 | + private store: { [key: string]: string } = {}; |
| 5 | + |
| 6 | + getItem(key: string): string | null { |
| 7 | + return this.store[key] || null; |
| 8 | + } |
| 9 | + |
| 10 | + setItem(key: string, value: string): void { |
| 11 | + this.store[key] = value; |
| 12 | + } |
| 13 | + |
| 14 | + removeItem(key: string): void { |
| 15 | + delete this.store[key]; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class AsyncMockStorage implements IStorageProvider { |
| 20 | + private store: { [key: string]: string } = {}; |
| 21 | + |
| 22 | + async getItem(key: string): Promise<string | null> { |
| 23 | + return this.store[key] || null; |
| 24 | + } |
| 25 | + |
| 26 | + async setItem(key: string, value: string): Promise<void> { |
| 27 | + this.store[key] = value; |
| 28 | + } |
| 29 | + |
| 30 | + async removeItem(key: string): Promise<void> { |
| 31 | + delete this.store[key]; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +describe('StorageAuthService', () => { |
| 36 | + describe('with Synchronous Storage', () => { |
| 37 | + let storage: SyncMockStorage; |
| 38 | + let authService: StorageAuthService; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + storage = new SyncMockStorage(); |
| 42 | + authService = new StorageAuthService(storage); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return null when credentials do not exist', async () => { |
| 46 | + const creds = await authService.getCredentials(); |
| 47 | + expect(creds).toBeNull(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should save and retrieve credentials successfully', async () => { |
| 51 | + await authService.saveCredentials('key123', 'secret456'); |
| 52 | + const creds = await authService.getCredentials(); |
| 53 | + expect(creds).toEqual({ apiKey: 'key123', apiSecret: 'secret456' }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should clear credentials successfully', async () => { |
| 57 | + await authService.saveCredentials('key123', 'secret456'); |
| 58 | + await authService.clearCredentials(); |
| 59 | + const creds = await authService.getCredentials(); |
| 60 | + expect(creds).toBeNull(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle corrupted JSON data gracefully by returning null', async () => { |
| 64 | + storage.setItem('frappe_credentials', '{invalid_json}'); |
| 65 | + const creds = await authService.getCredentials(); |
| 66 | + expect(creds).toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle missing fields in stored JSON gracefully by returning null', async () => { |
| 70 | + storage.setItem('frappe_credentials', JSON.stringify({ apiKey: 'only_key' })); |
| 71 | + const creds = await authService.getCredentials(); |
| 72 | + expect(creds).toBeNull(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('with Asynchronous Storage', () => { |
| 77 | + let storage: AsyncMockStorage; |
| 78 | + let authService: StorageAuthService; |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + storage = new AsyncMockStorage(); |
| 82 | + authService = new StorageAuthService(storage); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should save and retrieve credentials successfully over async boundary', async () => { |
| 86 | + await authService.saveCredentials('asyncKey', 'asyncSecret'); |
| 87 | + const creds = await authService.getCredentials(); |
| 88 | + expect(creds).toEqual({ apiKey: 'asyncKey', apiSecret: 'asyncSecret' }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should clear credentials successfully over async boundary', async () => { |
| 92 | + await authService.saveCredentials('asyncKey', 'asyncSecret'); |
| 93 | + await authService.clearCredentials(); |
| 94 | + const creds = await authService.getCredentials(); |
| 95 | + expect(creds).toBeNull(); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments