Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions poc-frappe-api/src/AuthService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export interface IAuthService {
getCredentials(): Promise<{ apiKey: string; apiSecret: string } | null>;
saveCredentials(apiKey: string, apiSecret: string): Promise<void>;
clearCredentials(): Promise<void>;
}

export class MockAuthService implements IAuthService {
private apiKey: string | null = null;
private apiSecret: string | null = null;

async getCredentials() {
if (!this.apiKey || !this.apiSecret) return null;
return { apiKey: this.apiKey, apiSecret: this.apiSecret };
}

async saveCredentials(apiKey: string, apiSecret: string) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
}

async clearCredentials() {
this.apiKey = null;
this.apiSecret = null;
}
}

export interface IStorageProvider {
getItem(key: string): string | null | Promise<string | null>;
setItem(key: string, value: string): void | Promise<void>;
removeItem(key: string): void | Promise<void>;
}

export class StorageAuthService implements IAuthService {
private storage: IStorageProvider;
private readonly storageKey: string;

constructor(storage: IStorageProvider, storageKey: string = 'frappe_credentials') {
this.storage = storage;
this.storageKey = storageKey;
}

public async getCredentials(): Promise<{ apiKey: string; apiSecret: string } | null> {
try {
const data = await this.storage.getItem(this.storageKey);
if (!data) {
return null;
}
const parsed = JSON.parse(data);
if (parsed && typeof parsed.apiKey === 'string' && typeof parsed.apiSecret === 'string') {
return { apiKey: parsed.apiKey, apiSecret: parsed.apiSecret };
}
return null;
} catch {
return null;
}
}

public async saveCredentials(apiKey: string, apiSecret: string): Promise<void> {
const data = JSON.stringify({ apiKey, apiSecret });
await this.storage.setItem(this.storageKey, data);
}

public async clearCredentials(): Promise<void> {
await this.storage.removeItem(this.storageKey);
}
}
98 changes: 98 additions & 0 deletions poc-frappe-api/tests/AuthService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { StorageAuthService, IStorageProvider } from '../src/AuthService';

class SyncMockStorage implements IStorageProvider {
private store: { [key: string]: string } = {};

getItem(key: string): string | null {
return this.store[key] || null;
}

setItem(key: string, value: string): void {
this.store[key] = value;
}

removeItem(key: string): void {
delete this.store[key];
}
}

class AsyncMockStorage implements IStorageProvider {
private store: { [key: string]: string } = {};

async getItem(key: string): Promise<string | null> {
return this.store[key] || null;
}

async setItem(key: string, value: string): Promise<void> {
this.store[key] = value;
}

async removeItem(key: string): Promise<void> {
delete this.store[key];
}
}

describe('StorageAuthService', () => {
describe('with Synchronous Storage', () => {
let storage: SyncMockStorage;
let authService: StorageAuthService;

beforeEach(() => {
storage = new SyncMockStorage();
authService = new StorageAuthService(storage);
});

it('should return null when credentials do not exist', async () => {
const creds = await authService.getCredentials();
expect(creds).toBeNull();
});

it('should save and retrieve credentials successfully', async () => {
await authService.saveCredentials('key123', 'secret456');
const creds = await authService.getCredentials();
expect(creds).toEqual({ apiKey: 'key123', apiSecret: 'secret456' });
});

it('should clear credentials successfully', async () => {
await authService.saveCredentials('key123', 'secret456');
await authService.clearCredentials();
const creds = await authService.getCredentials();
expect(creds).toBeNull();
});

it('should handle corrupted JSON data gracefully by returning null', async () => {
storage.setItem('frappe_credentials', '{invalid_json}');
const creds = await authService.getCredentials();
expect(creds).toBeNull();
});

it('should handle missing fields in stored JSON gracefully by returning null', async () => {
storage.setItem('frappe_credentials', JSON.stringify({ apiKey: 'only_key' }));
const creds = await authService.getCredentials();
expect(creds).toBeNull();
});
});

describe('with Asynchronous Storage', () => {
let storage: AsyncMockStorage;
let authService: StorageAuthService;

beforeEach(() => {
storage = new AsyncMockStorage();
authService = new StorageAuthService(storage);
});

it('should save and retrieve credentials successfully over async boundary', async () => {
await authService.saveCredentials('asyncKey', 'asyncSecret');
const creds = await authService.getCredentials();
expect(creds).toEqual({ apiKey: 'asyncKey', apiSecret: 'asyncSecret' });
});

it('should clear credentials successfully over async boundary', async () => {
await authService.saveCredentials('asyncKey', 'asyncSecret');
await authService.clearCredentials();
const creds = await authService.getCredentials();
expect(creds).toBeNull();
});
});
});