|
| 1 | +import { describe, it, expect, jest, beforeEach } from '@jest/globals' |
| 2 | +import { QueryRunner } from 'typeorm' |
| 3 | +import { Platform } from '../../Interface' |
| 4 | +import { UserStatus } from '../database/entities/user.entity' |
| 5 | +import { UserErrorMessage } from '../services/user.service' |
| 6 | + |
| 7 | +const mockReadUserByEmail = jest.fn() as jest.MockedFunction<(_email: string, _queryRunner: QueryRunner) => Promise<any>> |
| 8 | +const mockCreateQueryRunner = jest.fn() as jest.MockedFunction<() => any> |
| 9 | +const mockGetPlatformType = jest.fn() as jest.MockedFunction<() => Platform> |
| 10 | + |
| 11 | +jest.mock('../../utils/getRunningExpressApp', () => ({ |
| 12 | + getRunningExpressApp: jest.fn().mockImplementation(() => ({ |
| 13 | + AppDataSource: { createQueryRunner: mockCreateQueryRunner }, |
| 14 | + identityManager: { getPlatformType: mockGetPlatformType } |
| 15 | + })) |
| 16 | +})) |
| 17 | + |
| 18 | +jest.mock('../services/user.service', () => ({ |
| 19 | + UserService: jest.fn().mockImplementation(() => ({ readUserByEmail: mockReadUserByEmail })) |
| 20 | +})) |
| 21 | + |
| 22 | +jest.mock('../services/organization.service', () => ({ |
| 23 | + OrganizationService: jest.fn().mockImplementation(() => ({})) |
| 24 | +})) |
| 25 | + |
| 26 | +jest.mock('../services/workspace-user.service', () => ({ |
| 27 | + WorkspaceUserService: jest.fn().mockImplementation(() => ({})) |
| 28 | +})) |
| 29 | + |
| 30 | +import SSOBase from './SSOBase' |
| 31 | + |
| 32 | +class TestSSO extends SSOBase { |
| 33 | + getProviderName(): string { |
| 34 | + return 'Test SSO' |
| 35 | + } |
| 36 | + initialize(): void {} |
| 37 | + async refreshToken(): Promise<{ [key: string]: any }> { |
| 38 | + return {} |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function makeQueryRunner() { |
| 43 | + return { connect: jest.fn(), release: jest.fn(), isReleased: false } |
| 44 | +} |
| 45 | + |
| 46 | +describe('SSOBase.verifyAndLogin', () => { |
| 47 | + beforeEach(() => { |
| 48 | + jest.clearAllMocks() |
| 49 | + }) |
| 50 | + |
| 51 | + it('rejects an INVITED user instead of auto-activating them (invite-token bypass fix)', async () => { |
| 52 | + const queryRunner = makeQueryRunner() |
| 53 | + mockCreateQueryRunner.mockReturnValue(queryRunner) |
| 54 | + mockGetPlatformType.mockReturnValue(Platform.ENTERPRISE) |
| 55 | + mockReadUserByEmail.mockResolvedValue({ |
| 56 | + id: 'user-1', |
| 57 | + email: 'invitee@example.com', |
| 58 | + status: UserStatus.INVITED, |
| 59 | + tempToken: 'secret-invite-token', |
| 60 | + tokenExpiry: new Date(Date.now() + 3600_000) |
| 61 | + }) |
| 62 | + |
| 63 | + const sso = new TestSSO({} as any) |
| 64 | + const done = jest.fn() |
| 65 | + |
| 66 | + await sso.verifyAndLogin( |
| 67 | + {} as any, |
| 68 | + 'invitee@example.com', |
| 69 | + done, |
| 70 | + { displayName: 'Attacker' } as any, |
| 71 | + 'access-token', |
| 72 | + 'refresh-token' |
| 73 | + ) |
| 74 | + |
| 75 | + expect(mockReadUserByEmail).toHaveBeenCalledWith('invitee@example.com', queryRunner) |
| 76 | + expect(done).toHaveBeenCalledWith( |
| 77 | + { name: 'SSO_LOGIN_FAILED', message: UserErrorMessage.USER_INVITED_PENDING_ACTIVATION }, |
| 78 | + undefined |
| 79 | + ) |
| 80 | + expect(queryRunner.release).toHaveBeenCalled() |
| 81 | + }) |
| 82 | +}) |
0 commit comments