|
| 1 | +const request = require('supertest'); |
| 2 | + |
| 3 | +// Mock dependencies before requiring app |
| 4 | +jest.mock('../models/User.model', () => { |
| 5 | + const mockSave = jest.fn(); |
| 6 | + const MockUser = jest.fn().mockImplementation(function (data) { |
| 7 | + Object.assign(this, data); |
| 8 | + this._id = '507f1f77bcf86cd799439011'; |
| 9 | + this.role = 'user'; |
| 10 | + this.isVerified = false; |
| 11 | + this.createdAt = new Date('2026-01-01T00:00:00.000Z'); |
| 12 | + this.save = mockSave; |
| 13 | + }); |
| 14 | + MockUser.findOne = jest.fn(); |
| 15 | + MockUser._mockSave = mockSave; |
| 16 | + return MockUser; |
| 17 | +}); |
| 18 | + |
| 19 | +jest.mock('../services/email.service', () => ({ |
| 20 | + sendEmail: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +const User = require('../models/User.model'); |
| 24 | +const { sendEmail } = require('../services/email.service'); |
| 25 | +const app = require('../app'); |
| 26 | + |
| 27 | +describe('POST /api/auth/register', () => { |
| 28 | + beforeEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + User._mockSave.mockResolvedValue(undefined); |
| 31 | + }); |
| 32 | + |
| 33 | + const validBody = { |
| 34 | + fullName: 'Jane Doe', |
| 35 | + email: 'jane@example.com', |
| 36 | + password: 'securePass1', |
| 37 | + }; |
| 38 | + |
| 39 | + describe('successful registration', () => { |
| 40 | + it('returns 201 with user data and sends a verification email', async () => { |
| 41 | + User.findOne.mockResolvedValue(null); // no existing user |
| 42 | + sendEmail.mockResolvedValue({ messageId: 'test-id' }); |
| 43 | + |
| 44 | + const response = await request(app) |
| 45 | + .post('/api/auth/register') |
| 46 | + .send(validBody) |
| 47 | + .expect(201); |
| 48 | + |
| 49 | + expect(response.body.success).toBe(true); |
| 50 | + expect(response.body.message).toBe( |
| 51 | + 'User registered successfully. Please verify your email.' |
| 52 | + ); |
| 53 | + |
| 54 | + const { user } = response.body.data; |
| 55 | + expect(user.email).toBe('jane@example.com'); |
| 56 | + expect(user.isVerified).toBe(false); |
| 57 | + // Token must NOT be exposed in the response |
| 58 | + expect(user.emailVerificationToken).toBeUndefined(); |
| 59 | + expect(user.emailVerificationExpires).toBeUndefined(); |
| 60 | + |
| 61 | + // Verify the email was sent |
| 62 | + expect(sendEmail).toHaveBeenCalledTimes(1); |
| 63 | + const emailCall = sendEmail.mock.calls[0][0]; |
| 64 | + expect(emailCall.to).toBe('jane@example.com'); |
| 65 | + expect(emailCall.subject).toContain('erif'); // "Verify" |
| 66 | + expect(emailCall.html).toBeDefined(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('stores emailVerificationToken and emailVerificationExpires on the user', async () => { |
| 70 | + User.findOne.mockResolvedValue(null); |
| 71 | + sendEmail.mockResolvedValue({}); |
| 72 | + |
| 73 | + let capturedInstance; |
| 74 | + User.mockImplementationOnce(function (data) { |
| 75 | + Object.assign(this, data); |
| 76 | + this._id = '507f1f77bcf86cd799439011'; |
| 77 | + this.role = 'user'; |
| 78 | + this.isVerified = false; |
| 79 | + this.createdAt = new Date(); |
| 80 | + this.save = User._mockSave; |
| 81 | + capturedInstance = this; |
| 82 | + }); |
| 83 | + |
| 84 | + await request(app).post('/api/auth/register').send(validBody).expect(201); |
| 85 | + |
| 86 | + expect(capturedInstance.emailVerificationToken).toBeDefined(); |
| 87 | + expect(typeof capturedInstance.emailVerificationToken).toBe('string'); |
| 88 | + expect(capturedInstance.emailVerificationToken).toHaveLength(64); // 32 bytes hex |
| 89 | + expect(capturedInstance.emailVerificationExpires).toBeInstanceOf(Date); |
| 90 | + // Expiry should be ~24 hours from now |
| 91 | + const msUntilExpiry = |
| 92 | + capturedInstance.emailVerificationExpires.getTime() - Date.now(); |
| 93 | + expect(msUntilExpiry).toBeGreaterThan(23 * 60 * 60 * 1000); |
| 94 | + expect(msUntilExpiry).toBeLessThanOrEqual(24 * 60 * 60 * 1000 + 1000); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('duplicate email', () => { |
| 99 | + it('returns 409 when the email is already registered', async () => { |
| 100 | + User.findOne.mockResolvedValue({ email: 'jane@example.com' }); |
| 101 | + |
| 102 | + const response = await request(app) |
| 103 | + .post('/api/auth/register') |
| 104 | + .send(validBody) |
| 105 | + .expect(409); |
| 106 | + |
| 107 | + expect(response.body.success).toBe(false); |
| 108 | + expect(response.body.message).toBe('Email already exists'); |
| 109 | + expect(sendEmail).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('validation errors', () => { |
| 114 | + it('returns 400 when email is missing', async () => { |
| 115 | + const response = await request(app) |
| 116 | + .post('/api/auth/register') |
| 117 | + .send({ fullName: 'Jane', password: 'securePass1' }) |
| 118 | + .expect(400); |
| 119 | + |
| 120 | + expect(response.body.success).toBe(false); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns 400 when password is too short', async () => { |
| 124 | + const response = await request(app) |
| 125 | + .post('/api/auth/register') |
| 126 | + .send({ |
| 127 | + fullName: 'Jane', |
| 128 | + email: 'jane@example.com', |
| 129 | + password: 'short', |
| 130 | + }) |
| 131 | + .expect(400); |
| 132 | + |
| 133 | + expect(response.body.success).toBe(false); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('email send failure', () => { |
| 138 | + it('still returns 201 even when the email service throws', async () => { |
| 139 | + User.findOne.mockResolvedValue(null); |
| 140 | + sendEmail.mockRejectedValue(new Error('SMTP unavailable')); |
| 141 | + |
| 142 | + const response = await request(app) |
| 143 | + .post('/api/auth/register') |
| 144 | + .send(validBody) |
| 145 | + .expect(201); |
| 146 | + |
| 147 | + // Registration succeeds — the token is stored in the DB even if email fails |
| 148 | + expect(response.body.success).toBe(true); |
| 149 | + expect(response.body.data.user.email).toBe('jane@example.com'); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments