|
| 1 | +/** |
| 2 | + * Unit tests — config-gated email-verification policy |
| 3 | + * (config.organizations.emailVerification.mode). |
| 4 | + * |
| 5 | + * Covers BOTH modes on the two gated surfaces (signup org provisioning + domain |
| 6 | + * search), with the mailer reported as configured and the user UNVERIFIED — the |
| 7 | + * only case the mode actually changes: |
| 8 | + * - 'strict' (default) → blocks the unverified user (no org, empty search). |
| 9 | + * - 'off' → always provisions / always searches (mailer-on path |
| 10 | + * behaves like a mailer-not-configured env). |
| 11 | + * |
| 12 | + * The default-strict + mailer-on + verified path stays byte-identical to today |
| 13 | + * and is exercised by organizations.emailVerification.unit.tests.js. |
| 14 | + */ |
| 15 | +import mongoose from 'mongoose'; |
| 16 | +import { jest, describe, test, expect, beforeEach } from '@jest/globals'; |
| 17 | + |
| 18 | +// --- Mutable config mock (so each test can flip emailVerification.mode) --- |
| 19 | + |
| 20 | +const orgConfig = { |
| 21 | + enabled: false, |
| 22 | + domainMatching: false, |
| 23 | + emailVerification: { mode: 'strict' }, |
| 24 | +}; |
| 25 | +const configMock = { |
| 26 | + organizations: orgConfig, |
| 27 | + cookie: { secure: false, sameSite: 'strict' }, |
| 28 | + jwt: { secret: 'test-secret', expiresIn: 3600 }, |
| 29 | + get: jest.fn(), |
| 30 | +}; |
| 31 | +jest.unstable_mockModule('../../../config/index.js', () => ({ default: configMock })); |
| 32 | + |
| 33 | +// --- Mocks --- |
| 34 | + |
| 35 | +jest.unstable_mockModule('../../../lib/services/logger.js', () => ({ |
| 36 | + default: { error: jest.fn(), warn: jest.fn(), info: jest.fn(), debug: jest.fn() }, |
| 37 | +})); |
| 38 | + |
| 39 | +const mockIsConfigured = jest.fn(); |
| 40 | +jest.unstable_mockModule('../../../lib/helpers/mailer/index.js', () => ({ |
| 41 | + default: { isConfigured: mockIsConfigured, sendMail: jest.fn() }, |
| 42 | +})); |
| 43 | + |
| 44 | +const mockOrganizationsRepositoryCreate = jest.fn(); |
| 45 | +const mockOrganizationsRepositoryList = jest.fn(); |
| 46 | +const mockOrganizationsRepositoryExists = jest.fn(); |
| 47 | +jest.unstable_mockModule('../repositories/organizations.repository.js', () => ({ |
| 48 | + default: { |
| 49 | + create: mockOrganizationsRepositoryCreate, |
| 50 | + list: mockOrganizationsRepositoryList, |
| 51 | + exists: mockOrganizationsRepositoryExists, |
| 52 | + findOne: jest.fn(), |
| 53 | + get: jest.fn(), |
| 54 | + }, |
| 55 | +})); |
| 56 | + |
| 57 | +const mockMembershipRepositoryCreate = jest.fn(); |
| 58 | +const mockMembershipRepositoryFindOne = jest.fn(); |
| 59 | +jest.unstable_mockModule('../repositories/organizations.membership.repository.js', () => ({ |
| 60 | + default: { |
| 61 | + create: mockMembershipRepositoryCreate, |
| 62 | + findOne: mockMembershipRepositoryFindOne, |
| 63 | + list: jest.fn(), |
| 64 | + count: jest.fn(), |
| 65 | + }, |
| 66 | +})); |
| 67 | + |
| 68 | +const mockUpdateById = jest.fn(); |
| 69 | +jest.unstable_mockModule('../../users/services/users.service.js', () => ({ |
| 70 | + default: { |
| 71 | + getBrut: jest.fn(), |
| 72 | + updateById: mockUpdateById, |
| 73 | + findByEmail: jest.fn(), |
| 74 | + searchByNameOrEmail: jest.fn(), |
| 75 | + }, |
| 76 | +})); |
| 77 | + |
| 78 | +jest.unstable_mockModule('../../../lib/middlewares/policy.js', () => ({ |
| 79 | + default: { defineAbilityFor: jest.fn().mockResolvedValue({ rules: [] }) }, |
| 80 | +})); |
| 81 | + |
| 82 | +jest.unstable_mockModule('../../../lib/helpers/abilities.js', () => ({ |
| 83 | + default: jest.fn().mockReturnValue([]), |
| 84 | +})); |
| 85 | + |
| 86 | +jest.unstable_mockModule('../helpers/organizations.slug.js', () => ({ |
| 87 | + slugify: (str) => str.toLowerCase().replace(/\s+/g, '-'), |
| 88 | + generateOrganizationSlug: jest.fn().mockResolvedValue('test-slug'), |
| 89 | +})); |
| 90 | + |
| 91 | +const mockBillingGrantOnSignup = jest.fn().mockResolvedValue(undefined); |
| 92 | +jest.unstable_mockModule('../../billing/services/billing.signupGrant.service.js', () => ({ |
| 93 | + default: { grantOnSignup: mockBillingGrantOnSignup }, |
| 94 | +})); |
| 95 | + |
| 96 | +// --- Dynamic imports after mocks --- |
| 97 | + |
| 98 | +const { default: OrganizationsService } = await import('../services/organizations.service.js'); |
| 99 | + |
| 100 | +describe('Email-verification policy modes:', () => { |
| 101 | + const fakeUserId = new mongoose.Types.ObjectId(); |
| 102 | + |
| 103 | + beforeEach(() => { |
| 104 | + jest.clearAllMocks(); |
| 105 | + orgConfig.enabled = false; |
| 106 | + orgConfig.domainMatching = false; |
| 107 | + orgConfig.emailVerification = { mode: 'strict' }; |
| 108 | + mockMembershipRepositoryFindOne.mockResolvedValue(null); |
| 109 | + }); |
| 110 | + |
| 111 | + // --- handleSignupOrganization --- |
| 112 | + |
| 113 | + describe('handleSignupOrganization', () => { |
| 114 | + test("strict mode (default) blocks an unverified user when mailer is configured", async () => { |
| 115 | + orgConfig.emailVerification = { mode: 'strict' }; |
| 116 | + mockIsConfigured.mockReturnValue(true); |
| 117 | + |
| 118 | + const user = { id: fakeUserId.toString(), email: 'test@acme.com', firstName: 'Test', lastName: 'User', emailVerified: false }; |
| 119 | + const result = await OrganizationsService.handleSignupOrganization(user); |
| 120 | + |
| 121 | + expect(result.organization).toBeNull(); |
| 122 | + expect(result.membership).toBeNull(); |
| 123 | + expect(result.emailVerificationRequired).toBe(true); |
| 124 | + expect(mockOrganizationsRepositoryCreate).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | + |
| 127 | + test("off mode provisions an org for an unverified user even when mailer is configured", async () => { |
| 128 | + orgConfig.emailVerification = { mode: 'off' }; |
| 129 | + mockIsConfigured.mockReturnValue(true); |
| 130 | + |
| 131 | + const fakeOrg = { _id: new mongoose.Types.ObjectId(), name: 'Test', toJSON: () => ({ name: 'Test' }) }; |
| 132 | + const fakeMembership = { _id: new mongoose.Types.ObjectId(), role: 'owner' }; |
| 133 | + mockOrganizationsRepositoryCreate.mockResolvedValue(fakeOrg); |
| 134 | + mockMembershipRepositoryCreate.mockResolvedValue(fakeMembership); |
| 135 | + mockUpdateById.mockResolvedValue({}); |
| 136 | + |
| 137 | + const user = { id: fakeUserId.toString(), email: 'test@acme.com', firstName: 'Test', lastName: 'User', emailVerified: false }; |
| 138 | + const result = await OrganizationsService.handleSignupOrganization(user); |
| 139 | + |
| 140 | + expect(result.emailVerificationRequired).toBeUndefined(); |
| 141 | + expect(result.organization).not.toBeNull(); |
| 142 | + expect(mockOrganizationsRepositoryCreate).toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + |
| 145 | + test("unknown/typo mode fails closed (treated as strict) — blocks an unverified user", async () => { |
| 146 | + orgConfig.emailVerification = { mode: 'stict' }; // typo: not the explicit permissive 'off' |
| 147 | + mockIsConfigured.mockReturnValue(true); |
| 148 | + |
| 149 | + const user = { id: fakeUserId.toString(), email: 'test@acme.com', firstName: 'Test', lastName: 'User', emailVerified: false }; |
| 150 | + const result = await OrganizationsService.handleSignupOrganization(user); |
| 151 | + |
| 152 | + expect(result.emailVerificationRequired).toBe(true); |
| 153 | + expect(result.organization).toBeNull(); |
| 154 | + expect(mockOrganizationsRepositoryCreate).not.toHaveBeenCalled(); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + // --- search controller gate --- |
| 159 | + |
| 160 | + describe('search controller gate', () => { |
| 161 | + /** |
| 162 | + * @desc Build a minimal Express-like res object with spies. |
| 163 | + * @returns {Object} mock response |
| 164 | + */ |
| 165 | + function mockRes() { |
| 166 | + const res = {}; |
| 167 | + res.status = jest.fn().mockReturnValue(res); |
| 168 | + res.json = jest.fn().mockReturnValue(res); |
| 169 | + return res; |
| 170 | + } |
| 171 | + |
| 172 | + test("strict mode (default) returns an empty array for an unverified user when mailer is configured", async () => { |
| 173 | + orgConfig.emailVerification = { mode: 'strict' }; |
| 174 | + const { default: controller } = await import('../controllers/organizations.controller.js'); |
| 175 | + |
| 176 | + mockIsConfigured.mockReturnValue(true); |
| 177 | + mockOrganizationsRepositoryList.mockResolvedValue([]); |
| 178 | + |
| 179 | + const req = { user: { email: 'test@acme.com', emailVerified: false } }; |
| 180 | + const res = mockRes(); |
| 181 | + |
| 182 | + await controller.search(req, res); |
| 183 | + |
| 184 | + expect(mockOrganizationsRepositoryList).not.toHaveBeenCalled(); |
| 185 | + expect(res.status).toHaveBeenCalledWith(200); |
| 186 | + expect(res.json).toHaveBeenCalledWith( |
| 187 | + expect.objectContaining({ type: 'success', data: [] }), |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + test("off mode runs the domain search for an unverified user even when mailer is configured", async () => { |
| 192 | + orgConfig.emailVerification = { mode: 'off' }; |
| 193 | + const { default: controller } = await import('../controllers/organizations.controller.js'); |
| 194 | + |
| 195 | + mockIsConfigured.mockReturnValue(true); |
| 196 | + mockOrganizationsRepositoryList.mockResolvedValue([]); |
| 197 | + |
| 198 | + const req = { user: { email: 'test@acme.com', emailVerified: false } }; |
| 199 | + const res = mockRes(); |
| 200 | + |
| 201 | + await controller.search(req, res); |
| 202 | + |
| 203 | + expect(mockOrganizationsRepositoryList).toHaveBeenCalled(); |
| 204 | + expect(res.status).toHaveBeenCalledWith(200); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments