feat: implement ERP authentication service
Implement ErpAuthService with TDD approach: - Add login(), close(), getSession(), isActive() methods - Use Playwright chromium with headless:false for debugging - Manage browser lifecycle and session state - Handle authentication flow with ERP_LOCATORS - Add integration tests for login scenarios - Add unit tests for session management - Fix dotenv config path in test setup Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
40
tests/integration/erp-auth.test.ts
Normal file
40
tests/integration/erp-auth.test.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||
import { ErpAuthService } from '../../src/main/services/erp/erp-auth';
|
||||
import type { ErpConfig } from '../../src/main/types/erp.types';
|
||||
|
||||
describe('ERP Authentication Service (Integration)', () => {
|
||||
let authService: ErpAuthService;
|
||||
const config: ErpConfig = {
|
||||
url: process.env.ERP_URL || '',
|
||||
username: process.env.ERP_USERNAME || '',
|
||||
password: process.env.ERP_PASSWORD || '',
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
if (!config.url || !config.username || !config.password) {
|
||||
throw new Error('Missing ERP credentials in environment variables');
|
||||
}
|
||||
authService = new ErpAuthService(config);
|
||||
});
|
||||
|
||||
it('should login successfully', async () => {
|
||||
const session = await authService.login();
|
||||
|
||||
expect(session).toBeDefined();
|
||||
expect(session.browser).toBeDefined();
|
||||
expect(session.context).toBeDefined();
|
||||
expect(session.page).toBeDefined();
|
||||
expect(session.isLoggedIn).toBe(true);
|
||||
}, 30000);
|
||||
|
||||
it('should navigate to main page after login', async () => {
|
||||
const session = await authService.login();
|
||||
|
||||
const url = session.page.url();
|
||||
expect(url).toContain(config.url);
|
||||
}, 30000);
|
||||
|
||||
afterAll(async () => {
|
||||
await authService?.close();
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,9 @@
|
||||
import { beforeAll, afterAll } from 'vitest';
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
|
||||
// Load environment variables
|
||||
dotenv.config({ path: '.env' });
|
||||
// Load environment variables from project root
|
||||
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
|
||||
|
||||
beforeAll(async () => {
|
||||
// Global test setup
|
||||
|
||||
59
tests/unit/erp-auth.unit.test.ts
Normal file
59
tests/unit/erp-auth.unit.test.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { ErpAuthService } from '../../src/main/services/erp/erp-auth';
|
||||
import type { ErpConfig } from '../../src/main/types/erp.types';
|
||||
|
||||
describe('ERP Authentication Service (Unit)', () => {
|
||||
describe('Session Management', () => {
|
||||
it('should create service instance with config', () => {
|
||||
const config: ErpConfig = {
|
||||
url: 'https://test.example.com',
|
||||
username: 'testuser',
|
||||
password: 'testpass',
|
||||
};
|
||||
|
||||
const service = new ErpAuthService(config);
|
||||
|
||||
expect(service).toBeDefined();
|
||||
expect(service.isActive()).toBe(false);
|
||||
});
|
||||
|
||||
it('should throw error when getting session before login', () => {
|
||||
const config: ErpConfig = {
|
||||
url: 'https://test.example.com',
|
||||
username: 'testuser',
|
||||
password: 'testpass',
|
||||
};
|
||||
|
||||
const service = new ErpAuthService(config);
|
||||
|
||||
expect(() => service.getSession()).toThrow('Not logged in. Call login() first.');
|
||||
});
|
||||
|
||||
it('should report inactive status before login', () => {
|
||||
const config: ErpConfig = {
|
||||
url: 'https://test.example.com',
|
||||
username: 'testuser',
|
||||
password: 'testpass',
|
||||
};
|
||||
|
||||
const service = new ErpAuthService(config);
|
||||
|
||||
expect(service.isActive()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Close Method', () => {
|
||||
it('should handle close when no session exists', async () => {
|
||||
const config: ErpConfig = {
|
||||
url: 'https://test.example.com',
|
||||
username: 'testuser',
|
||||
password: 'testpass',
|
||||
};
|
||||
|
||||
const service = new ErpAuthService(config);
|
||||
|
||||
// Should not throw when closing without session
|
||||
await expect(service.close()).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user