-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat(auth): trigger handleSignupOrganization on verifyEmail success (best-effort) #3765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
modules/auth/tests/auth.verifyEmail.signup-org.unit.tests.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| import { jest, describe, test, expect, beforeEach } from '@jest/globals'; | ||
|
|
||
| /** | ||
| * Unit tests for auth.controller verifyEmail() — handleSignupOrganization wiring. | ||
| * | ||
| * Verifies that: | ||
| * 1. verifyEmail calls handleSignupOrganization after the user's emailVerified flag is set. | ||
| * 2. A failure in handleSignupOrganization does NOT cause verifyEmail to fail (best-effort). | ||
| */ | ||
| describe('auth.controller verifyEmail — handleSignupOrganization wiring:', () => { | ||
| let handleSignupOrganizationMock; | ||
| let mockUserService; | ||
| let mockResponses; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetModules(); | ||
|
|
||
| handleSignupOrganizationMock = jest.fn().mockResolvedValue({ _id: 'org_001' }); | ||
|
|
||
| mockUserService = { | ||
| create: jest.fn(), | ||
| getBrut: jest.fn().mockResolvedValue({ | ||
| _id: 'user_001', | ||
| id: 'user_001', | ||
| email: 'user@example.com', | ||
| emailVerificationToken: 'tok', | ||
| emailVerificationExpires: Date.now() + 3600000, | ||
| }), | ||
| update: jest.fn().mockResolvedValue({}), | ||
| remove: jest.fn(), | ||
| search: jest.fn(), | ||
| count: jest.fn().mockResolvedValue(0), | ||
| }; | ||
|
|
||
| mockResponses = { | ||
| successCb: jest.fn(), | ||
| errorCb: jest.fn(), | ||
| }; | ||
|
|
||
| jest.unstable_mockModule('../../../lib/services/logger.js', () => ({ | ||
| default: { warn: jest.fn(), error: jest.fn(), info: jest.fn() }, | ||
| })); | ||
| jest.unstable_mockModule('../../../config/index.js', () => ({ | ||
| default: { | ||
| sign: { up: true, in: true }, | ||
| jwt: { secret: 's', expiresIn: 3600 }, | ||
| cookie: { secure: false, sameSite: 'lax' }, | ||
| organizations: { enabled: true }, | ||
| app: { title: 'Test', contact: 'a@b.com' }, | ||
| }, | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/users/services/users.service.js', () => ({ | ||
| default: mockUserService, | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/auth/services/auth.invitation.service.js', () => ({ | ||
| default: { findValid: jest.fn().mockResolvedValue(null), consume: jest.fn().mockResolvedValue(null) }, | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/auth/services/auth.signupCapacity.js', () => ({ | ||
| computeSignupCapacity: jest.fn().mockResolvedValue({ cap: null, remaining: null }), | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/users/repositories/users.repository.js', () => ({ | ||
| default: { update: jest.fn() }, | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/organizations/services/organizations.service.js', () => ({ | ||
| default: { handleSignupOrganization: handleSignupOrganizationMock }, | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/organizations/services/organizations.crud.service.js', () => ({ | ||
| default: { autoSetCurrentOrganization: jest.fn() }, | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/organizations/services/organizations.membership.service.js', () => ({ | ||
| default: { findByUserAndOrganization: jest.fn(), listPendingByUser: jest.fn().mockResolvedValue([]) }, | ||
| })); | ||
| jest.unstable_mockModule('../../../modules/users/models/users.schema.js', () => ({ | ||
| default: { User: {} }, | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/middlewares/model.js', () => ({ | ||
| default: { getResultFromZod: jest.fn(), checkError: jest.fn() }, | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/middlewares/policy.js', () => ({ | ||
| default: { defineAbilityFor: jest.fn().mockResolvedValue({}) }, | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/helpers/mailer/index.js', () => ({ | ||
| default: { isConfigured: jest.fn().mockReturnValue(false), sendMail: jest.fn() }, | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/helpers/responses.js', () => ({ | ||
| default: { | ||
| success: jest.fn().mockReturnValue(mockResponses.successCb), | ||
| error: jest.fn().mockReturnValue(mockResponses.errorCb), | ||
| }, | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/helpers/errors.js', () => ({ | ||
| default: { getMessage: jest.fn().mockReturnValue('error') }, | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/helpers/AppError.js', () => ({ | ||
| default: class AppError extends Error { | ||
| constructor(msg, opts) { | ||
| super(msg); | ||
| this.code = opts?.code; | ||
| this.details = opts?.details; | ||
| } | ||
| }, | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/helpers/abilities.js', () => ({ | ||
| default: jest.fn().mockReturnValue([]), | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/helpers/getBaseUrl.js', () => ({ | ||
| default: jest.fn().mockReturnValue('http://localhost:3000'), | ||
| })); | ||
| jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({ | ||
| default: { identify: jest.fn(), capture: jest.fn(), groupIdentify: jest.fn() }, | ||
| })); | ||
| }); | ||
|
|
||
| test('calls handleSignupOrganization when email verification succeeds', async () => { | ||
| const { default: AuthController } = await import('../../../modules/auth/controllers/auth.controller.js'); | ||
|
|
||
| const req = { params: { token: 'tok' } }; | ||
| const res = {}; | ||
|
|
||
| await AuthController.verifyEmail(req, res); | ||
|
|
||
| expect(handleSignupOrganizationMock).toHaveBeenCalledTimes(1); | ||
| // Must be called with a user that has emailVerified=true (marked before the call) | ||
| expect(handleSignupOrganizationMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ emailVerified: true }), | ||
| ); | ||
| }); | ||
|
|
||
| test('does not crash if handleSignupOrganization throws (best-effort)', async () => { | ||
| handleSignupOrganizationMock.mockRejectedValue(new Error('org boom')); | ||
|
|
||
| const { default: AuthController } = await import('../../../modules/auth/controllers/auth.controller.js'); | ||
|
|
||
| const req = { params: { token: 'tok' } }; | ||
| const res = {}; | ||
|
|
||
| // Email verification must NOT fail because of an org-setup error | ||
| await AuthController.verifyEmail(req, res); | ||
|
|
||
| // The success response must still be sent | ||
| expect(mockResponses.successCb).toHaveBeenCalledWith({ emailVerified: true }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Best-effort swallow is correct, but consider a reconciliation path for permanent provisioning failures.
Wrapping
handleSignupOrganizationin try/catch and logging is the right call for the "must not block verification" requirement, and the service's idempotent convergence guard makes re-invocation safe. However, when provisioning fails here the error is only logged — a verified user can end up with no organization/grants and no automatic recovery, sincesignin/tokenonly callautoSetCurrentOrganization(which sets an existing membership, never creates one). Consider a reconciliation trigger (e.g., re-attempthandleSignupOrganizationon next signin when the user has verified email but no active membership, or a background sweep) so a transient failure here doesn't strand the account.🤖 Prompt for AI Agents