|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import { jest, describe, test, expect, beforeEach } from '@jest/globals'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Unit tests — verify that logger.warn/error is called when fire-and-forget |
| 8 | + * email sends or DB rollback operations fail (replaces silent .catch(() => {})). |
| 9 | + */ |
| 10 | + |
| 11 | +describe('auth.controller silent-catch error logging:', () => { |
| 12 | + let mockWarn; |
| 13 | + let mockError; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + jest.resetModules(); |
| 17 | + |
| 18 | + mockWarn = jest.fn(); |
| 19 | + mockError = jest.fn(); |
| 20 | + |
| 21 | + jest.unstable_mockModule('../../../lib/services/logger.js', () => ({ |
| 22 | + default: { warn: mockWarn, error: mockError, info: jest.fn() }, |
| 23 | + })); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('signup: verification email failure logs a warning', () => { |
| 27 | + test('should call logger.warn when sendVerificationEmail rejects', async () => { |
| 28 | + const emailError = new Error('SMTP down'); |
| 29 | + |
| 30 | + jest.unstable_mockModule('../../../modules/users/services/users.service.js', () => ({ |
| 31 | + default: { |
| 32 | + create: jest.fn().mockResolvedValue({ |
| 33 | + id: 'u1', email: 'x@y.com', firstName: 'A', lastName: 'B', provider: 'local', |
| 34 | + }), |
| 35 | + getBrut: jest.fn().mockResolvedValue({ id: 'u1' }), |
| 36 | + update: jest.fn().mockResolvedValue({}), |
| 37 | + remove: jest.fn(), |
| 38 | + }, |
| 39 | + })); |
| 40 | + |
| 41 | + jest.unstable_mockModule('../../../modules/organizations/services/organizations.service.js', () => ({ |
| 42 | + default: { |
| 43 | + handleSignupOrganization: jest.fn().mockResolvedValue({ |
| 44 | + organization: null, joined: false, pendingJoin: false, |
| 45 | + abilities: [], organizationSetupRequired: false, |
| 46 | + emailVerificationRequired: false, suggestedOrganization: null, |
| 47 | + }), |
| 48 | + }, |
| 49 | + })); |
| 50 | + |
| 51 | + jest.unstable_mockModule('../../../modules/organizations/services/organizations.crud.service.js', () => ({ |
| 52 | + default: { autoSetCurrentOrganization: jest.fn() }, |
| 53 | + })); |
| 54 | + |
| 55 | + jest.unstable_mockModule('../../../modules/organizations/services/organizations.membership.service.js', () => ({ |
| 56 | + default: { findByUserAndOrganization: jest.fn(), listPendingByUser: jest.fn().mockResolvedValue([]) }, |
| 57 | + })); |
| 58 | + |
| 59 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 60 | + default: { |
| 61 | + sign: { up: true, in: true }, |
| 62 | + jwt: { secret: 'test-secret', expiresIn: 3600 }, |
| 63 | + cookie: { secure: false, sameSite: 'lax' }, |
| 64 | + organizations: { enabled: false }, |
| 65 | + app: { title: 'Test', contact: 'test@test.com' }, |
| 66 | + }, |
| 67 | + })); |
| 68 | + |
| 69 | + jest.unstable_mockModule('../../../lib/middlewares/model.js', () => ({ |
| 70 | + default: { getResultFromZod: jest.fn(), checkError: jest.fn() }, |
| 71 | + })); |
| 72 | + |
| 73 | + // Mailer configured, sendMail rejects |
| 74 | + jest.unstable_mockModule('../../../lib/helpers/mailer/index.js', () => ({ |
| 75 | + default: { |
| 76 | + isConfigured: jest.fn().mockReturnValue(true), |
| 77 | + sendMail: jest.fn().mockRejectedValue(emailError), |
| 78 | + }, |
| 79 | + })); |
| 80 | + |
| 81 | + jest.unstable_mockModule('../../../lib/helpers/responses.js', () => ({ |
| 82 | + default: { |
| 83 | + success: jest.fn().mockReturnValue(jest.fn()), |
| 84 | + error: jest.fn().mockReturnValue(jest.fn()), |
| 85 | + }, |
| 86 | + })); |
| 87 | + |
| 88 | + jest.unstable_mockModule('../../../lib/helpers/errors.js', () => ({ |
| 89 | + default: { getMessage: jest.fn().mockReturnValue('error') }, |
| 90 | + })); |
| 91 | + |
| 92 | + jest.unstable_mockModule('../../../lib/helpers/AppError.js', () => ({ |
| 93 | + default: class AppError extends Error { |
| 94 | + constructor(msg, opts) { |
| 95 | + super(msg); |
| 96 | + this.code = opts?.code; |
| 97 | + this.details = opts?.details; |
| 98 | + } |
| 99 | + }, |
| 100 | + })); |
| 101 | + |
| 102 | + jest.unstable_mockModule('../../../modules/users/models/users.schema.js', () => ({ |
| 103 | + default: { User: {} }, |
| 104 | + })); |
| 105 | + |
| 106 | + jest.unstable_mockModule('../../../lib/middlewares/policy.js', () => ({ |
| 107 | + default: { defineAbilityFor: jest.fn().mockResolvedValue({}) }, |
| 108 | + })); |
| 109 | + |
| 110 | + jest.unstable_mockModule('../../../lib/helpers/abilities.js', () => ({ |
| 111 | + default: jest.fn().mockReturnValue([]), |
| 112 | + })); |
| 113 | + |
| 114 | + jest.unstable_mockModule('../../../lib/helpers/getBaseUrl.js', () => ({ |
| 115 | + default: jest.fn().mockReturnValue('http://localhost:3000'), |
| 116 | + })); |
| 117 | + |
| 118 | + jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({ |
| 119 | + default: { identify: jest.fn(), groupIdentify: jest.fn() }, |
| 120 | + })); |
| 121 | + |
| 122 | + const { default: AuthController } = await import('../../../modules/auth/controllers/auth.controller.js'); |
| 123 | + |
| 124 | + const req = { body: { email: 'x@y.com', firstName: 'A', lastName: 'B', password: 'P@ss1234!' } }; |
| 125 | + const res = { |
| 126 | + status: jest.fn().mockReturnThis(), |
| 127 | + cookie: jest.fn().mockReturnThis(), |
| 128 | + json: jest.fn().mockReturnThis(), |
| 129 | + }; |
| 130 | + |
| 131 | + await AuthController.signup(req, res); |
| 132 | + |
| 133 | + // Allow the fire-and-forget promise to settle |
| 134 | + await new Promise((r) => setTimeout(r, 10)); |
| 135 | + |
| 136 | + expect(mockWarn).toHaveBeenCalledWith( |
| 137 | + 'auth.signup: verification email failed', |
| 138 | + { message: emailError.message, stack: emailError.stack }, |
| 139 | + ); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('auth.password.controller silent-catch error logging:', () => { |
| 145 | + let mockWarn; |
| 146 | + let mockError; |
| 147 | + |
| 148 | + beforeEach(() => { |
| 149 | + jest.resetModules(); |
| 150 | + |
| 151 | + mockWarn = jest.fn(); |
| 152 | + mockError = jest.fn(); |
| 153 | + |
| 154 | + jest.unstable_mockModule('../../../lib/services/logger.js', () => ({ |
| 155 | + default: { warn: mockWarn, error: mockError, info: jest.fn() }, |
| 156 | + })); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('reset: confirmation email failure logs a warning', () => { |
| 160 | + test('should call logger.warn when confirmation email rejects', async () => { |
| 161 | + const emailError = new Error('SMTP unavailable'); |
| 162 | + |
| 163 | + jest.unstable_mockModule('../../../modules/users/services/users.service.js', () => ({ |
| 164 | + default: { |
| 165 | + getBrut: jest.fn().mockResolvedValue({ |
| 166 | + id: 'u1', email: 'a@b.com', firstName: 'A', lastName: 'B', |
| 167 | + resetPasswordToken: 'tok', resetPasswordExpires: Date.now() + 3600000, |
| 168 | + }), |
| 169 | + update: jest.fn().mockResolvedValue({ id: 'u1', email: 'a@b.com', firstName: 'A', lastName: 'B' }), |
| 170 | + }, |
| 171 | + })); |
| 172 | + |
| 173 | + jest.unstable_mockModule('../../../modules/auth/services/auth.service.js', () => ({ |
| 174 | + default: { hashPassword: jest.fn().mockResolvedValue('hashed') }, |
| 175 | + })); |
| 176 | + |
| 177 | + // sendMail rejects |
| 178 | + jest.unstable_mockModule('../../../lib/helpers/mailer/index.js', () => ({ |
| 179 | + default: { sendMail: jest.fn().mockRejectedValue(emailError) }, |
| 180 | + })); |
| 181 | + |
| 182 | + jest.unstable_mockModule('../../../lib/helpers/getBaseUrl.js', () => ({ |
| 183 | + default: jest.fn().mockReturnValue('http://localhost:3000'), |
| 184 | + })); |
| 185 | + |
| 186 | + jest.unstable_mockModule('../../../lib/helpers/errors.js', () => ({ |
| 187 | + default: { getMessage: jest.fn().mockReturnValue('error') }, |
| 188 | + })); |
| 189 | + |
| 190 | + jest.unstable_mockModule('../../../lib/helpers/responses.js', () => ({ |
| 191 | + default: { |
| 192 | + success: jest.fn().mockReturnValue(jest.fn()), |
| 193 | + error: jest.fn().mockReturnValue(jest.fn()), |
| 194 | + }, |
| 195 | + })); |
| 196 | + |
| 197 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 198 | + default: { |
| 199 | + jwt: { secret: 'test-secret', expiresIn: 3600 }, |
| 200 | + cookie: { secure: false, sameSite: 'lax' }, |
| 201 | + app: { title: 'Test', contact: 'test@test.com' }, |
| 202 | + }, |
| 203 | + })); |
| 204 | + |
| 205 | + const { default: PasswordController } = await import('../../../modules/auth/controllers/auth.password.controller.js'); |
| 206 | + |
| 207 | + const req = { body: { token: 'tok', newPassword: 'NewP@ss1!' } }; |
| 208 | + const res = { |
| 209 | + status: jest.fn().mockReturnThis(), |
| 210 | + cookie: jest.fn().mockReturnThis(), |
| 211 | + json: jest.fn().mockReturnThis(), |
| 212 | + }; |
| 213 | + |
| 214 | + await PasswordController.reset(req, res); |
| 215 | + |
| 216 | + // Allow fire-and-forget to settle |
| 217 | + await new Promise((r) => setTimeout(r, 10)); |
| 218 | + |
| 219 | + expect(mockWarn).toHaveBeenCalledWith( |
| 220 | + 'auth.password.reset: confirmation email failed', |
| 221 | + { message: emailError.message, stack: emailError.stack }, |
| 222 | + ); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments