AuthManager.integration.test.ts•4.37 kB
/**
* AuthManager Integration Tests
*
* Tests with real WhaTap API
* Requires test credentials in .env.test
*/
import * as fs from 'fs/promises';
import * as path from 'path';
import * as os from 'os';
import { AuthManager } from '../../../src/core/auth/AuthManager';
import { SessionStore } from '../../../src/core/auth/SessionStore';
// Check if integration tests should run
const runIntegrationTests =
process.env.RUN_INTEGRATION_TESTS === 'true' &&
process.env.WHATAP_TEST_EMAIL &&
process.env.WHATAP_TEST_PASSWORD;
const describeIf = runIntegrationTests ? describe : describe.skip;
describeIf('AuthManager (Integration Tests)', () => {
let testDir: string;
let sessionStore: SessionStore;
let authManager: AuthManager;
const credentials = {
email: process.env.WHATAP_TEST_EMAIL!,
password: process.env.WHATAP_TEST_PASSWORD!,
serviceUrl: process.env.WHATAP_SERVICE_URL || 'https://service.whatap.io',
};
beforeEach(async () => {
// Create temporary test directory
testDir = path.join(os.tmpdir(), `whatap-test-${Date.now()}`);
await fs.mkdir(testDir, { recursive: true });
sessionStore = new SessionStore(testDir);
authManager = new AuthManager(sessionStore);
});
afterEach(async () => {
// Clean up
try {
await fs.rm(testDir, { recursive: true, force: true });
} catch (error) {
// Ignore cleanup errors
}
});
test('should login successfully with real credentials', async () => {
const session = await authManager.login(credentials);
// Verify session structure
expect(session).toHaveProperty('email');
expect(session).toHaveProperty('accountId');
expect(session).toHaveProperty('apiToken');
expect(session).toHaveProperty('cookies');
expect(session).toHaveProperty('serviceUrl');
expect(session).toHaveProperty('createdAt');
expect(session).toHaveProperty('expiresAt');
// Verify values
expect(session.email).toBe(credentials.email);
expect(session.accountId).toBeGreaterThan(0);
expect(session.apiToken).toBeTruthy();
expect(session.cookies.wa).toBeTruthy();
expect(session.cookies.jsessionid).toBeTruthy();
// Verify authenticated
expect(authManager.isAuthenticated()).toBe(true);
console.log('✅ Login successful!');
console.log(` Account ID: ${session.accountId}`);
console.log(` Email: ${session.email}`);
console.log(` API Token: ${session.apiToken?.substring(0, 20)}...`);
}, 30000); // 30 second timeout for real API
test('should save and restore session', async () => {
// Login
const session = await authManager.login(credentials);
const accountId = session.accountId;
// Create new AuthManager and load session
const newAuthManager = new AuthManager(sessionStore);
const loaded = await newAuthManager.loadSession();
expect(loaded).not.toBeNull();
expect(loaded?.accountId).toBe(accountId);
expect(loaded?.email).toBe(credentials.email);
expect(newAuthManager.isAuthenticated()).toBe(true);
console.log('✅ Session saved and restored successfully');
}, 30000);
test('should get cookie header for API requests', async () => {
await authManager.login(credentials);
const cookieHeader = authManager.getCookieHeader();
expect(cookieHeader).toContain('WHATAP=');
expect(cookieHeader).toContain('JSESSIONID=');
console.log('✅ Cookie header:', cookieHeader);
}, 30000);
test('should logout and clear session', async () => {
await authManager.login(credentials);
expect(authManager.isAuthenticated()).toBe(true);
await authManager.logout();
expect(authManager.isAuthenticated()).toBe(false);
const loaded = await sessionStore.load();
expect(loaded).toBeNull();
console.log('✅ Logout successful');
}, 30000);
});
// If integration tests are skipped, log a message
if (!runIntegrationTests) {
describe('AuthManager (Integration Tests - SKIPPED)', () => {
test('integration tests skipped', () => {
console.log('⚠️ Integration tests skipped');
console.log(' To run integration tests:');
console.log(' 1. Copy .env.example to .env.test');
console.log(' 2. Set RUN_INTEGRATION_TESTS=true');
console.log(' 3. Set WHATAP_TEST_EMAIL and WHATAP_TEST_PASSWORD');
expect(true).toBe(true);
});
});
}