Files
rxminder/services/auth/__tests__/emailVerification.test.ts

59 lines
1.6 KiB
TypeScript

import { EmailVerificationService } from '../emailVerification.service';
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();
});
});