Initial commit: Complete NodeJS-native setup
- Migrated from Python pre-commit to NodeJS-native solution - Reorganized documentation structure - Set up Husky + lint-staged for efficient pre-commit hooks - Fixed Dockerfile healthcheck issue - Added comprehensive documentation index
This commit is contained in:
59
services/auth/__tests__/auth.integration.test.ts
Normal file
59
services/auth/__tests__/auth.integration.test.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { authService } from '../auth.service';
|
||||
import { AccountStatus } from '../auth.constants';
|
||||
import { User } from '../../../types';
|
||||
|
||||
// Helper to clear localStorage and reset the mock DB before each test
|
||||
beforeEach(() => {
|
||||
// Clear all localStorage keys used by dbService and authService
|
||||
Object.keys(localStorage).forEach(key => localStorage.removeItem(key));
|
||||
});
|
||||
|
||||
describe('Authentication Integration Tests', () => {
|
||||
const username = 'testuser';
|
||||
const password = 'Passw0rd!';
|
||||
const email = 'testuser@example.com';
|
||||
|
||||
test('User registration creates a pending account', async () => {
|
||||
const result = await authService.register(email, password, username);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.user.username).toBe(username);
|
||||
expect(result.user.email).toBe(email);
|
||||
expect(result.user.status).toBe(AccountStatus.PENDING);
|
||||
expect(result.user.emailVerified).toBeFalsy();
|
||||
});
|
||||
|
||||
test('Login fails for unverified (pending) account', async () => {
|
||||
await expect(authService.login({ email, password })).rejects.toThrow();
|
||||
});
|
||||
|
||||
test('Email verification activates the account', async () => {
|
||||
// Register a user first to get the verification token
|
||||
const result = await authService.register(email, password, username);
|
||||
const verificationToken = result.verificationToken.token;
|
||||
|
||||
const verifiedUser = await authService.verifyEmail(verificationToken);
|
||||
expect(verifiedUser.status).toBe(AccountStatus.ACTIVE);
|
||||
expect(verifiedUser.emailVerified).toBeTruthy();
|
||||
});
|
||||
|
||||
test('Login succeeds after email verification', async () => {
|
||||
const tokens = await authService.login({ email, password });
|
||||
expect(tokens).toBeDefined();
|
||||
expect(tokens.accessToken).toBeTruthy();
|
||||
expect(tokens.refreshToken).toBeTruthy();
|
||||
});
|
||||
|
||||
test('OAuth flow registers or logs in a user', async () => {
|
||||
const oauthEmail = 'oauthuser@example.com';
|
||||
const oauthName = 'OAuth User';
|
||||
const result = await authService.loginWithOAuth('google', {
|
||||
email: oauthEmail,
|
||||
username: oauthName,
|
||||
});
|
||||
expect(result).toBeDefined();
|
||||
expect(result.user.email).toBe(oauthEmail);
|
||||
// OAuth users should be active immediately
|
||||
expect(result.user.status).toBe(AccountStatus.ACTIVE);
|
||||
expect(result.user.emailVerified).toBeTruthy();
|
||||
});
|
||||
});
|
||||
59
services/auth/__tests__/emailVerification.test.ts
Normal file
59
services/auth/__tests__/emailVerification.test.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { EmailVerificationService } from '../emailVerification.service';
|
||||
import { dbService } from '../../couchdb.factory';
|
||||
|
||||
jest.mock('../../couchdb.factory');
|
||||
jest.mock('../../email');
|
||||
|
||||
describe('EmailVerificationService', () => {
|
||||
let emailVerificationService: EmailVerificationService;
|
||||
|
||||
beforeEach(() => {
|
||||
emailVerificationService = new EmailVerificationService();
|
||||
});
|
||||
|
||||
test('should generate and validate verification token', async () => {
|
||||
const user = {
|
||||
_id: 'user1',
|
||||
email: 'test@example.com',
|
||||
username: 'testuser',
|
||||
password: 'password',
|
||||
};
|
||||
|
||||
const verificationToken =
|
||||
await emailVerificationService.generateVerificationToken(user as any);
|
||||
|
||||
expect(verificationToken).toBeDefined();
|
||||
expect(verificationToken.token).toBeDefined();
|
||||
expect(verificationToken.expiresAt).toBeDefined();
|
||||
|
||||
const validatedUser =
|
||||
await emailVerificationService.validateVerificationToken(
|
||||
verificationToken.token
|
||||
);
|
||||
|
||||
expect(validatedUser).toBeDefined();
|
||||
expect(validatedUser!._id).toBe(user._id);
|
||||
});
|
||||
|
||||
test('should not validate expired token', async () => {
|
||||
const user = {
|
||||
_id: 'user2',
|
||||
email: 'test2@example.com',
|
||||
username: 'testuser2',
|
||||
password: 'password2',
|
||||
};
|
||||
|
||||
const verificationToken =
|
||||
await emailVerificationService.generateVerificationToken(user as any);
|
||||
|
||||
// Set expiresAt to past date
|
||||
verificationToken.expiresAt = new Date(Date.now() - 1000 * 60 * 60 * 24);
|
||||
|
||||
const validatedUser =
|
||||
await emailVerificationService.validateVerificationToken(
|
||||
verificationToken.token
|
||||
);
|
||||
|
||||
expect(validatedUser).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user