|
| 1 | +/** |
| 2 | + * Bug Condition Fix Validation — Controller-level |
| 3 | + * |
| 4 | + * Verifies that after the fix, the register controller always passes |
| 5 | + * otp: result.otp to sendSuccess unconditionally, regardless of environment. |
| 6 | + * |
| 7 | + * The mock returns { userId, email, otp: '048291' } — simulating the FIXED |
| 8 | + * service — and the test asserts that data.otp === '048291' in the sendSuccess |
| 9 | + * call, confirming the controller passes it through without any conditional guard. |
| 10 | + * |
| 11 | + * Validates: Requirements 1.1, 1.2 |
| 12 | + */ |
| 13 | + |
| 14 | +import { jest } from '@jest/globals'; |
| 15 | + |
| 16 | +// ─── Mock: apiResponse utility ──────────────────────────────────────────────── |
| 17 | +let capturedSendSuccessArgs = null; |
| 18 | +jest.unstable_mockModule('../../../src/utils/apiResponse.js', () => ({ |
| 19 | + sendSuccess: jest.fn().mockImplementation((res, args) => { |
| 20 | + capturedSendSuccessArgs = args; |
| 21 | + }), |
| 22 | + sendError: jest.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +// ─── Mock: auth.service.js — simulates the FIXED service ───────────────────── |
| 26 | +// Returns { userId, email, otp } — the fixed service always includes otp |
| 27 | +jest.unstable_mockModule('../../../src/services/auth.service.js', () => ({ |
| 28 | + registerUser: jest.fn().mockResolvedValue({ |
| 29 | + userId: 'user123', |
| 30 | + email: 'alice@example.com', |
| 31 | + otp: '048291', |
| 32 | + }), |
| 33 | + verifyEmail: jest.fn(), |
| 34 | + resendVerificationEmail: jest.fn(), |
| 35 | + loginUser: jest.fn(), |
| 36 | + refreshAccessToken: jest.fn(), |
| 37 | + logoutUser: jest.fn(), |
| 38 | + forgotPassword: jest.fn(), |
| 39 | + resetPassword: jest.fn(), |
| 40 | + changePassword: jest.fn(), |
| 41 | + listRefreshTokens: jest.fn(), |
| 42 | + revokeRefreshToken: jest.fn(), |
| 43 | + revokeAllRefreshTokens: jest.fn(), |
| 44 | + buildRefreshCookieOptions: jest.fn().mockReturnValue({}), |
| 45 | +})); |
| 46 | + |
| 47 | +// ─── Mock: enrichment service (used by controller) ─────────────────────────── |
| 48 | +jest.unstable_mockModule('../../../src/services/enrichment.service.js', () => ({ |
| 49 | + checkDisposableEmail: jest.fn().mockResolvedValue({ disposable: false }), |
| 50 | + getIPInfo: jest.fn().mockResolvedValue(null), |
| 51 | +})); |
| 52 | + |
| 53 | +// ─── Mock: audit service (used by controller) ──────────────────────────────── |
| 54 | +jest.unstable_mockModule('../../../src/services/audit.service.js', () => ({ |
| 55 | + log: jest.fn().mockResolvedValue(undefined), |
| 56 | +})); |
| 57 | + |
| 58 | +// ─── Import the controller AFTER all mocks are registered ──────────────────── |
| 59 | +const { register } = await import('../../../src/controllers/auth.controller.js'); |
| 60 | + |
| 61 | +// ───────────────────────────────────────────────────────────────────────────── |
| 62 | +// Test Suite: Controller-level — OTP passed through to sendSuccess |
| 63 | +// ───────────────────────────────────────────────────────────────────────────── |
| 64 | + |
| 65 | +describe('Fix Validation: Controller — OTP passed through to sendSuccess unconditionally', () => { |
| 66 | + beforeEach(() => { |
| 67 | + capturedSendSuccessArgs = null; |
| 68 | + jest.clearAllMocks(); |
| 69 | + }); |
| 70 | + |
| 71 | + /** |
| 72 | + * After the fix, the controller uses `otp: result.otp` unconditionally |
| 73 | + * (no `if (result.otp)` guard). This test mocks registerUser to return |
| 74 | + * { userId, email, otp: '048291' } and asserts that data.otp === '048291' |
| 75 | + * in the sendSuccess call — confirming the controller passes it through. |
| 76 | + * |
| 77 | + * Validates: Requirements 1.1, 1.2 |
| 78 | + */ |
| 79 | + it('data.otp passed to sendSuccess equals the otp returned by the service', async () => { |
| 80 | + const req = { |
| 81 | + body: { |
| 82 | + name: 'Alice', |
| 83 | + email: 'alice@example.com', |
| 84 | + password: 'Secret1!', |
| 85 | + }, |
| 86 | + ip: '127.0.0.1', |
| 87 | + get: jest.fn().mockReturnValue('test-agent'), |
| 88 | + }; |
| 89 | + |
| 90 | + const res = { |
| 91 | + status: jest.fn().mockReturnThis(), |
| 92 | + json: jest.fn().mockReturnThis(), |
| 93 | + }; |
| 94 | + |
| 95 | + await register(req, res); |
| 96 | + |
| 97 | + expect(capturedSendSuccessArgs).not.toBeNull(); |
| 98 | + expect(capturedSendSuccessArgs.data).toBeDefined(); |
| 99 | + expect(capturedSendSuccessArgs.data.otp).toBe('048291'); |
| 100 | + }); |
| 101 | +}); |
0 commit comments