Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

38.5 KB
Raw Blame
import mongoose, { Types } from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { ErrorTypes, SystemRoles } from 'librechat-data-provider';
import { logger, tenantStorage, createMethods, createModels } from '@librechat/data-schemas';
import type { IUser, AppConfig, UserMethods } from '@librechat/data-schemas';
import type { CommandStartedEvent } from 'mongodb';
import type { FilterQuery } from 'mongoose';
import {
  findOpenIDUser,
  getOpenIdEmail,
  getOpenIdIssuer,
  createOpenIDUser,
  normalizeOpenIdIssuer,
} from './openid';
import { recordOpenIDUserLookup } from '~/app/metrics';

function newId() {
  return new Types.ObjectId();
}

jest.mock('@librechat/data-schemas', () => ({
  ...jest.requireActual('@librechat/data-schemas'),
  logger: {
    warn: jest.fn(),
    info: jest.fn(),
    error: jest.fn(),
  },
}));

jest.mock('~/app/metrics', () => ({
  isMetricsConfigured: jest.fn(() => true),
  recordOpenIDUserLookup: jest.fn(),
}));

describe('normalizeOpenIdIssuer', () => {
  it('normalizes blank, trailing-slash, and discovery-document issuers', () => {
    expect(normalizeOpenIdIssuer('')).toBeUndefined();
    expect(normalizeOpenIdIssuer('   ')).toBeUndefined();
    expect(normalizeOpenIdIssuer('https://issuer.example.com/')).toBe('https://issuer.example.com');
    expect(
      normalizeOpenIdIssuer('https://issuer.example.com/.well-known/openid-configuration/'),
    ).toBe('https://issuer.example.com');
    expect(
      normalizeOpenIdIssuer('https://issuer.example.com/realm/.well-known/openid-configuration'),
    ).toBe('https://issuer.example.com/realm');
  });
});

describe('getOpenIdIssuer', () => {
  const originalOpenIdIssuer = process.env.OPENID_ISSUER;

  afterEach(() => {
    if (originalOpenIdIssuer == null) {
      delete process.env.OPENID_ISSUER;
      return;
    }

    process.env.OPENID_ISSUER = originalOpenIdIssuer;
  });

  it('prefers token issuer and falls back to metadata and env issuer', () => {
    process.env.OPENID_ISSUER = 'https://env.example.com/.well-known/openid-configuration';

    expect(
      getOpenIdIssuer(
        { iss: 'https://token.example.com/' },
        { serverMetadata: () => ({ issuer: 'https://metadata.example.com' }) },
      ),
    ).toBe('https://token.example.com');
    expect(
      getOpenIdIssuer({}, { serverMetadata: () => ({ issuer: 'https://metadata.example.com/' }) }),
    ).toBe('https://metadata.example.com');
    expect(getOpenIdIssuer({})).toBe('https://env.example.com');
  });
});

describe('findOpenIDUser', () => {
  let mockFindUser: jest.MockedFunction<UserMethods['findUser']>;
  const originalOpenIdIssuer = process.env.OPENID_ISSUER;
  const issuer = 'https://issuer.example.com';

  beforeEach(() => {
    mockFindUser = jest.fn();
    delete process.env.OPENID_ISSUER;
    jest.clearAllMocks();
    (logger.warn as jest.Mock).mockClear();
    (logger.info as jest.Mock).mockClear();
  });

  afterAll(() => {
    if (originalOpenIdIssuer == null) {
      delete process.env.OPENID_ISSUER;
      return;
    }
    process.env.OPENID_ISSUER = originalOpenIdIssuer;
  });

  describe('Primary condition searches', () => {
    it('should find user by openidId', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        openidIssuer: issuer,
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenCalledWith({
        openidId: 'openid_123',
        openidIssuer: issuer,
      });
      expect(result).toEqual({
        user: mockUser,
        error: null,
        migration: false,
      });
      expect(recordOpenIDUserLookup).toHaveBeenCalledWith('found', expect.any(Number));
    });

    it('should find user by idOnTheSource', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        idOnTheSource: 'source_123',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        idOnTheSource: 'source_123',
      });

      expect(mockFindUser).toHaveBeenNthCalledWith(1, {
        openidId: 'openid_123',
        openidIssuer: issuer,
      });
      expect(mockFindUser).toHaveBeenNthCalledWith(2, {
        idOnTheSource: 'source_123',
        openidIssuer: issuer,
      });
      expect(result).toEqual({
        user: mockUser,
        error: null,
        migration: false,
      });
    });

    it('should find user by both openidId and idOnTheSource', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        idOnTheSource: 'source_123',
        openidIssuer: issuer,
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        idOnTheSource: 'source_123',
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenCalledTimes(1);
      expect(mockFindUser).toHaveBeenCalledWith({
        openidId: 'openid_123',
        openidIssuer: issuer,
      });
      expect(result).toEqual({
        user: mockUser,
        error: null,
        migration: false,
      });
    });

    it('should bind primary lookup to issuer', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com/',
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenCalledWith({
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com',
      });
      expect(result).toEqual({
        user: mockUser,
        error: null,
        migration: false,
      });
    });

    it('should allow legacy issuer-less lookup only for the configured OpenID login issuer', async () => {
      process.env.OPENID_ISSUER = 'https://issuer.example.com/';
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com',
        findUser: mockFindUser,
      });

      expect(mockFindUser).toHaveBeenNthCalledWith(1, {
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com',
      });
      expect(mockFindUser).toHaveBeenNthCalledWith(2, {
        openidId: 'openid_123',
        openidIssuer: { $exists: false },
      });
      expect(result).toEqual({
        user: { ...mockUser, openidIssuer: 'https://issuer.example.com' },
        error: null,
        migration: true,
      });
    });

    it('should allow legacy issuer-less lookup when login issuer is a discovery document URL', async () => {
      process.env.OPENID_ISSUER = 'https://issuer.example.com/.well-known/openid-configuration';
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com',
        findUser: mockFindUser,
      });

      expect(mockFindUser).toHaveBeenNthCalledWith(1, {
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com',
      });
      expect(mockFindUser).toHaveBeenNthCalledWith(2, {
        openidId: 'openid_123',
        openidIssuer: { $exists: false },
      });
      expect(result).toEqual({
        user: { ...mockUser, openidIssuer: 'https://issuer.example.com' },
        error: null,
        migration: true,
      });
    });

    it('should skip primary ID lookup when issuer context is missing', async () => {
      mockFindUser.mockResolvedValueOnce(null);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        idOnTheSource: 'source_123',
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenCalledTimes(1);
      expect(mockFindUser).toHaveBeenCalledWith({ email: '[email protected]' });
      expect(result).toEqual({
        user: null,
        error: null,
        migration: false,
      });
    });
  });

  describe('Email-based searches', () => {
    it('should find user by email when primary conditions fail and openidId matches', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        openidIssuer: issuer,
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenNthCalledWith(1, {
        openidId: 'openid_123',
        openidIssuer: issuer,
      });
      expect(mockFindUser).toHaveBeenNthCalledWith(2, {
        email: '[email protected]',
      });
      expect(result).toEqual({
        user: mockUser,
        error: null,
        migration: false,
      });
    });

    it('should return null user when email is not found', async () => {
      mockFindUser
        .mockResolvedValueOnce(null) // Primary condition fails
        .mockResolvedValueOnce(null); // Email search fails

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenCalledTimes(2);
      expect(result).toEqual({
        user: null,
        error: null,
        migration: false,
      });
    });

    it('should not search by email if not provided', async () => {
      mockFindUser.mockResolvedValueOnce(null);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
      });

      expect(mockFindUser).toHaveBeenCalledTimes(1);
      expect(mockFindUser).toHaveBeenCalledWith({
        openidId: 'openid_123',
        openidIssuer: issuer,
      });
      expect(result).toEqual({
        user: null,
        error: null,
        migration: false,
      });
    });
  });

  describe('Provider conflict handling', () => {
    it('should return error when user has different provider', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'google',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser
        .mockResolvedValueOnce(null) // Primary condition fails
        .mockResolvedValueOnce(mockUser); // Email search finds user with different provider

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: null,
        error: ErrorTypes.AUTH_FAILED,
        migration: false,
      });
    });

    it('should reject email fallback when existing openidId does not match token sub', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_456',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: null,
        error: ErrorTypes.AUTH_FAILED,
        migration: false,
      });
    });

    it('should allow email fallback when existing openidId matches token sub', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        openidIssuer: issuer,
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: mockUser,
        error: null,
        migration: false,
      });
    });

    it('should reject email fallback when stored openidIssuer does not match token issuer', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        openidIssuer: 'https://issuer-a.example.com',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: 'https://issuer-b.example.com',
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: null,
        error: ErrorTypes.AUTH_FAILED,
        migration: false,
      });
    });

    it('should reject legacy email fallback when token issuer is not the configured OpenID login issuer', async () => {
      process.env.OPENID_ISSUER = 'https://issuer-a.example.com';
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: 'https://issuer-b.example.com',
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: null,
        error: ErrorTypes.AUTH_FAILED,
        migration: false,
      });
    });
  });

  describe('User migration scenarios', () => {
    it('should prepare user for migration when email exists without openidId', async () => {
      const mockUser: IUser = {
        _id: newId(),
        email: '[email protected]',
        username: 'testuser',
        // No provider and no openidId - needs migration
      } as IUser;

      mockFindUser
        .mockResolvedValueOnce(null) // Primary condition fails
        .mockResolvedValueOnce(mockUser); // Email search finds user without openidId

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: {
          ...mockUser,
          provider: 'openid',
          openidId: 'openid_123',
          openidIssuer: issuer,
        },
        error: null,
        migration: true,
      });
    });

    it('should persist issuer when migrating a user by email', async () => {
      const mockUser: IUser = {
        _id: newId(),
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: 'https://issuer.example.com',
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: {
          ...mockUser,
          provider: 'openid',
          openidId: 'openid_123',
          openidIssuer: 'https://issuer.example.com',
        },
        error: null,
        migration: true,
      });
    });

    it('should reject when user already has a different openidId', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'existing_openid',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: null,
        error: ErrorTypes.AUTH_FAILED,
        migration: false,
      });
    });

    it('should reject when user has no provider but a different openidId', async () => {
      const mockUser: IUser = {
        _id: newId(),
        openidId: 'existing_openid',
        email: '[email protected]',
        username: 'testuser',
        // No provider field — tests a different branch than openid-provider mismatch
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(result).toEqual({
        user: null,
        error: ErrorTypes.AUTH_FAILED,
        migration: false,
      });
    });
  });

  describe('Custom strategy names', () => {
    it('should use custom strategy name in logs', async () => {
      const loggerWarn = logger.warn as jest.Mock;
      loggerWarn.mockClear();

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(null);

      await findOpenIDUser({
        openidId: 'openid_123',
        findUser: mockFindUser,
        email: '[email protected]',
        strategyName: 'customStrategy',
      });

      expect(loggerWarn).toHaveBeenCalledWith(expect.stringContaining('[customStrategy]'));
    });

    it('should default to openid strategy name', async () => {
      const loggerWarn = logger.warn as jest.Mock;
      loggerWarn.mockClear();

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(null);

      await findOpenIDUser({
        openidId: 'openid_123',
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(loggerWarn).toHaveBeenCalledWith(expect.stringContaining('[openid]'));
    });
  });

  describe('Edge cases', () => {
    it('should handle empty string openidId', async () => {
      mockFindUser.mockResolvedValueOnce(null);

      const result = await findOpenIDUser({
        openidId: '',
        findUser: mockFindUser,
      });

      expect(mockFindUser).not.toHaveBeenCalled();
      expect(result).toEqual({
        user: null,
        error: null,
        migration: false,
      });
    });

    it('should handle empty string idOnTheSource', async () => {
      mockFindUser.mockResolvedValueOnce(null);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        idOnTheSource: '',
      });

      expect(mockFindUser).toHaveBeenCalledWith({
        openidId: 'openid_123',
        openidIssuer: issuer,
      });
      expect(result).toEqual({
        user: null,
        error: null,
        migration: false,
      });
    });

    it('should handle both openidId and idOnTheSource as empty strings', async () => {
      await findOpenIDUser({
        openidId: '',
        findUser: mockFindUser,
        idOnTheSource: '',
        email: '[email protected]',
      });

      // Should skip primary search and go directly to email search
      expect(mockFindUser).toHaveBeenCalledTimes(1);
      expect(mockFindUser).toHaveBeenCalledWith({ email: '[email protected]' });
    });

    it('should pass email to findUser for case-insensitive lookup (findUser handles normalization)', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'openid_123',
        openidIssuer: issuer,
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(null).mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: 'openid_123',
        openidIssuer: issuer,
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenNthCalledWith(2, { email: '[email protected]' });
      expect(result).toEqual({
        user: mockUser,
        error: null,
        migration: false,
      });
    });

    it('should handle findUser throwing an error', async () => {
      mockFindUser.mockRejectedValueOnce(new Error('Database error'));

      await expect(
        findOpenIDUser({
          openidId: 'openid_123',
          openidIssuer: issuer,
          findUser: mockFindUser,
        }),
      ).rejects.toThrow('Database error');
      expect(recordOpenIDUserLookup).toHaveBeenCalledWith('error', expect.any(Number));
    });

    it('should reject email fallback when openidId is empty and user has a stored openidId', async () => {
      const mockUser: IUser = {
        _id: newId(),
        provider: 'openid',
        openidId: 'existing-real-id',
        email: '[email protected]',
        username: 'testuser',
      } as IUser;

      mockFindUser.mockResolvedValueOnce(mockUser);

      const result = await findOpenIDUser({
        openidId: '',
        findUser: mockFindUser,
        email: '[email protected]',
      });

      expect(mockFindUser).toHaveBeenCalledTimes(1);
      expect(mockFindUser).toHaveBeenCalledWith({ email: '[email protected]' });
      expect(result).toEqual({
        user: null,
        error: ErrorTypes.AUTH_FAILED,
        migration: false,
      });
    });
  });
});

type CapturedFindCommand = {
  find?: unknown;
  filter?: unknown;
};

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === 'object' && value !== null;
}

function planContainsStage(value: unknown, stage: string): boolean {
  if (!isRecord(value)) return false;
  if (value.stage === stage) return true;

  return Object.values(value).some((entry) => {
    if (Array.isArray(entry)) return entry.some((item) => planContainsStage(item, stage));
    return planContainsStage(entry, stage);
  });
}

function getTotalDocsExamined(explain: unknown): number | undefined {
  if (!isRecord(explain)) return undefined;
  const executionStats = explain.executionStats;
  if (!isRecord(executionStats)) return undefined;
  const totalDocsExamined = executionStats.totalDocsExamined;
  return typeof totalDocsExamined === 'number' ? totalDocsExamined : undefined;
}

describe('findOpenIDUser Mongo compatibility', () => {
  let mongoServer: MongoMemoryServer;
  let User: mongoose.Model<IUser>;
  let methods: ReturnType<typeof createMethods>;

  const issuer = 'https://issuer.example.com';
  const originalOpenIdIssuer = process.env.OPENID_ISSUER;

  async function seedUsers(count: number) {
    await User.insertMany(
      Array.from({ length: count }, (_, index) => ({
        email: `filler-${index}@example.com`,
        provider: 'openid',
        openidId: `filler-sub-${index}`,
        openidIssuer: issuer,
        idOnTheSource: `filler-oid-${index}`,
      })),
    );
  }

  async function captureFindFilters<T>(run: () => Promise<T>): Promise<{
    result: T;
    filters: unknown[];
  }> {
    const filters: unknown[] = [];
    const client = mongoose.connection.getClient();
    const listener = (event: CommandStartedEvent) => {
      const command = event.command as CapturedFindCommand;
      if (event.commandName === 'find' && command.find === User.collection.name) {
        filters.push(command.filter);
      }
    };

    client.on('commandStarted', listener);
    try {
      const result = await run();
      return { result, filters };
    } finally {
      client.off('commandStarted', listener);
    }
  }

  beforeAll(async () => {
    mongoServer = await MongoMemoryServer.create();
    /** Each test rebuilds the User indexes explicitly after resetting the database. */
    await mongoose.connect(mongoServer.getUri(), {
      monitorCommands: true,
      autoCreate: false,
      autoIndex: false,
    });
    createModels(mongoose);
    User = mongoose.models.User as mongoose.Model<IUser>;
    methods = createMethods(mongoose);
  });

  afterAll(async () => {
    if (originalOpenIdIssuer == null) {
      delete process.env.OPENID_ISSUER;
    } else {
      process.env.OPENID_ISSUER = originalOpenIdIssuer;
    }
    await mongoose.disconnect();
    await mongoServer.stop();
  });

  beforeEach(async () => {
    process.env.OPENID_ISSUER = issuer;
    await mongoose.connection.dropDatabase();
    await User.syncIndexes();
  });

  it('keeps exact issuer lookup indexable on a seeded user collection', async () => {
    await seedUsers(1500);
    await User.create({
      email: '[email protected]',
      provider: 'openid',
      openidId: 'target-sub',
      openidIssuer: issuer,
      idOnTheSource: 'target-oid',
    });

    const { result, filters } = await captureFindFilters(() =>
      findOpenIDUser({
        openidId: 'target-sub',
        idOnTheSource: 'target-oid',
        openidIssuer: issuer,
        findUser: methods.findUser,
      }),
    );

    expect(result.user?.email).toBe('[email protected]');
    expect(filters).toEqual([{ openidId: 'target-sub', openidIssuer: issuer }]);

    const explain = await User.findOne(filters[0] as FilterQuery<IUser>).explain('executionStats');
    expect(planContainsStage(explain, 'IXSCAN')).toBe(true);
    expect(getTotalDocsExamined(explain)).toBeLessThanOrEqual(1);
  });

  it('resolves legacy issuer-less users without nested or disjunctive filters', async () => {
    await User.create({
      email: '[email protected]',
      provider: 'openid',
      openidId: 'legacy-sub',
      idOnTheSource: 'legacy-oid',
    });

    const { result, filters } = await captureFindFilters(() =>
      findOpenIDUser({
        openidId: 'legacy-sub',
        idOnTheSource: 'legacy-oid',
        openidIssuer: issuer,
        findUser: methods.findUser,
      }),
    );

    expect(result.user?.email).toBe('[email protected]');
    expect(result.migration).toBe(true);
    expect(filters).toEqual([
      { openidId: 'legacy-sub', openidIssuer: issuer },
      { idOnTheSource: 'legacy-oid', openidIssuer: issuer },
      { openidId: 'legacy-sub', openidIssuer: { $exists: false } },
    ]);
  });
});

describe('getOpenIdEmail', () => {
  const originalEmailClaim = process.env.OPENID_EMAIL_CLAIM;

  beforeEach(() => {
    jest.clearAllMocks();
    delete process.env.OPENID_EMAIL_CLAIM;
  });

  afterAll(() => {
    if (originalEmailClaim == null) {
      delete process.env.OPENID_EMAIL_CLAIM;
      return;
    }
    process.env.OPENID_EMAIL_CLAIM = originalEmailClaim;
  });

  it('uses the default claim order', () => {
    expect(
      getOpenIdEmail({
        email: '[email protected]',
        preferred_username: '[email protected]',
        upn: '[email protected]',
      }),
    ).toBe('[email protected]');
  });

  it('returns undefined when default claims are absent', () => {
    expect(getOpenIdEmail({})).toBeUndefined();
  });

  it('skips empty fallback claims', () => {
    expect(
      getOpenIdEmail({
        email: '',
        preferred_username: '[email protected]',
        upn: '[email protected]',
      }),
    ).toBe('[email protected]');
  });

  it('uses OPENID_EMAIL_CLAIM when present', () => {
    process.env.OPENID_EMAIL_CLAIM = 'custom_identifier';

    expect(
      getOpenIdEmail({
        email: '[email protected]',
        custom_identifier: '[email protected]',
      }),
    ).toBe('[email protected]');
  });

  it('falls back with a warning when OPENID_EMAIL_CLAIM is missing', () => {
    process.env.OPENID_EMAIL_CLAIM = 'missing_identifier';

    expect(getOpenIdEmail({ email: '[email protected]' }, 'remoteAgentAuth')).toBe(
      '[email protected]',
    );
    expect(logger.warn).toHaveBeenCalledWith(
      expect.stringContaining('OPENID_EMAIL_CLAIM="missing_identifier" not present in userinfo'),
    );
  });

  it('falls back with a warning when OPENID_EMAIL_CLAIM is not a string', () => {
    process.env.OPENID_EMAIL_CLAIM = 'groups';

    expect(getOpenIdEmail({ email: '[email protected]', groups: ['a'] })).toBe('[email protected]');
    expect(logger.warn).toHaveBeenCalledWith(
      expect.stringContaining(
        'OPENID_EMAIL_CLAIM="groups" resolved to a non-string value (type: object)',
      ),
    );
  });
});

describe('createOpenIDUser', () => {
  let mongoServer: MongoMemoryServer;
  let User: mongoose.Model<IUser>;
  let methods: ReturnType<typeof createMethods>;

  const issuer = 'https://issuer.example.com';
  const email = '[email protected]';
  const openidId = 'first-login-sub';
  const tenantId = 'tenant-a';
  const profile = {
    openidId,
    openidIssuer: issuer,
    email,
    username: 'first-login',
    name: 'First Login',
    emailVerified: true,
  };
  const getAppConfig = jest.fn(async (): Promise<AppConfig> => appConfigWith());

  function appConfigWith(
    balance?: { enabled: boolean; startBalance: number },
    allowedDomains?: string[],
  ): AppConfig {
    return { balance, registration: { allowedDomains } } as Partial<AppConfig> as AppConfig;
  }

  function getLookup() {
    return { findUser: methods.findUser, email, openidId, openidIssuer: issuer };
  }

  /** Answers the new account's config read, then the read of the account a lookup recovers. */
  function configsFor(newAccount: AppConfig, recovered: AppConfig): void {
    getAppConfig.mockResolvedValueOnce(newAccount).mockResolvedValueOnce(recovered);
  }

  function firstLogin(overrides: Partial<Parameters<typeof createOpenIDUser>[0]> = {}) {
    return createOpenIDUser({
      lookup: getLookup(),
      profile,
      getAppConfig,
      getBalanceConfig: (config: AppConfig) => config.balance ?? null,
      createUserIfAbsent: methods.createUserIfAbsent,
      findBalanceByUser: (userId) => methods.findBalanceByUser(userId),
      ...overrides,
    });
  }

  beforeAll(async () => {
    mongoServer = await MongoMemoryServer.create();
    await mongoose.connect(mongoServer.getUri(), { autoIndex: false });
    createModels(mongoose);
    User = mongoose.models.User as mongoose.Model<IUser>;
    methods = createMethods(mongoose);
  });

  afterAll(async () => {
    await mongoose.disconnect();
    await mongoServer.stop();
  });

  beforeEach(async () => {
    jest.clearAllMocks();
    getAppConfig.mockReset().mockResolvedValue(appConfigWith());
    await mongoose.connection.dropDatabase();
    await User.syncIndexes();
  });

  it('resolves concurrent first logins to the one user that was created', async () => {
    const lookups = await Promise.all([findOpenIDUser(getLookup()), findOpenIDUser(getLookup())]);
    expect(lookups.map((lookup) => lookup.user)).toEqual([null, null]);

    const logins = await Promise.all([firstLogin(), firstLogin()]);

    expect(await User.countDocuments({ email })).toBe(1);
    expect(logins[0].user._id.toString()).toBe(logins[1].user._id.toString());
    expect(logins[1].user).toEqual(
      expect.objectContaining({ openidId, email, provider: 'openid' }),
    );
  });

  it("refreshes the recovered user with the losing request's claims", async () => {
    await firstLogin();
    const renamed = {
      ...profile,
      email: '[email protected]',
      name: 'Renamed',
      emailVerified: false,
    };

    const { user } = await firstLogin({
      lookup: { ...getLookup(), email: renamed.email },
      profile: renamed,
    });

    expect(await User.countDocuments()).toBe(1);
    expect(user).toEqual(
      expect.objectContaining({
        openidId,
        email: '[email protected]',
        name: 'Renamed',
        emailVerified: false,
      }),
    );
  });

  it('credits the start balance once across concurrent first logins', async () => {
    getAppConfig.mockResolvedValue(appConfigWith({ enabled: true, startBalance: 500 }));

    const [{ user }] = await Promise.all([firstLogin(), firstLogin()]);

    const balances = await mongoose.models.Balance.find({ user: user._id }).lean<
      Array<{ tokenCredits: number }>
    >();
    expect(balances.map((record) => record.tokenCredits)).toEqual([500]);
  });

  it('fails the recovered login while the winner has not credited its start balance yet', async () => {
    await User.create({ email, provider: 'openid', openidId, openidIssuer: issuer });
    getAppConfig.mockResolvedValue(appConfigWith({ enabled: true, startBalance: 500 }));

    const login = firstLogin();

    await expect(login).rejects.toThrow(ErrorTypes.AUTH_FAILED);
    expect(await mongoose.models.Balance.countDocuments()).toBe(0);
  });

  it('recovers an account created without a balance when no start balance is configured', async () => {
    await User.create({ email, provider: 'openid', openidId, openidIssuer: issuer });
    getAppConfig.mockResolvedValue(appConfigWith({ enabled: true, startBalance: 0 }));

    const { user } = await firstLogin();

    expect(user).toEqual(expect.objectContaining({ openidId, email }));
  });

  it("applies the recovered tenant account's email-domain policy", async () => {
    await tenantStorage.run({ tenantId }, () =>
      User.create({ email, provider: 'openid', openidId, openidIssuer: issuer }),
    );
    configsFor(appConfigWith(), appConfigWith(undefined, ['other.example']));

    const login = tenantStorage.run({ tenantId }, () => firstLogin());

    await expect(login).rejects.toThrow('Email domain not allowed');
    expect(getAppConfig).toHaveBeenCalledWith(expect.objectContaining({ tenantId }));
  });

  it("continues when only the recovered tenant account's config sets a start balance", async () => {
    await tenantStorage.run({ tenantId }, () =>
      User.create({ email, provider: 'openid', openidId, openidIssuer: issuer }),
    );
    configsFor(appConfigWith(), appConfigWith({ enabled: true, startBalance: 500 }));

    const login = await tenantStorage.run({ tenantId }, () => firstLogin());

    expect(login.user).toEqual(expect.objectContaining({ openidId, tenantId }));
  });

  it('holds the recovered login on the start balance new users are created with', async () => {
    await tenantStorage.run({ tenantId }, () =>
      User.create({ email, provider: 'openid', openidId, openidIssuer: issuer }),
    );
    configsFor(
      appConfigWith({ enabled: true, startBalance: 500 }),
      appConfigWith({ enabled: false, startBalance: 0 }),
    );

    const login = tenantStorage.run({ tenantId }, () => firstLogin());

    await expect(login).rejects.toThrow(ErrorTypes.AUTH_FAILED);
  });

  it("continues under the recovered tenant account's config when its policy admits the email", async () => {
    await tenantStorage.run({ tenantId }, () =>
      User.create({ email, provider: 'openid', openidId, openidIssuer: issuer }),
    );
    const tenantConfig = appConfigWith(undefined, ['example.com']);
    configsFor(appConfigWith(), tenantConfig);

    const login = await tenantStorage.run({ tenantId }, () => firstLogin());

    expect(login.appConfig).toBe(tenantConfig);
    expect(login.user).toEqual(expect.objectContaining({ openidId, tenantId }));
  });

  it("refuses a first login the USER role's domain list excludes, creating nothing", async () => {
    getAppConfig.mockResolvedValueOnce(appConfigWith(undefined, ['other.example']));

    await expect(firstLogin()).rejects.toThrow('Email domain not allowed');
    expect(getAppConfig).toHaveBeenCalledWith({
      role: SystemRoles.USER,
      skipRuntimeAugmentation: true,
      failClosed: true,
    });
    expect(await User.countDocuments()).toBe(0);
  });

  it('creates the account in the USER role and reports that this call created it', async () => {
    const login = await firstLogin({ claimedRole: SystemRoles.ADMIN });

    expect(login.created).toBe(true);
    expect(login.user).toEqual(expect.objectContaining({ role: SystemRoles.USER }));
    expect(getAppConfig).toHaveBeenCalledWith({
      role: SystemRoles.ADMIN,
      skipRuntimeAugmentation: true,
      failClosed: true,
    });
  });

  it('refuses a first login the role its claims assign excludes, creating nothing', async () => {
    getAppConfig
      .mockResolvedValueOnce(appConfigWith({ enabled: true, startBalance: 500 }))
      .mockResolvedValueOnce(appConfigWith(undefined, ['other.example']));

    await expect(firstLogin({ claimedRole: SystemRoles.ADMIN })).rejects.toThrow(
      'Email domain not allowed',
    );
    expect(await User.countDocuments()).toBe(0);
    expect(await mongoose.models.Balance.countDocuments()).toBe(0);
  });

  it('reports a recovered account as not created by this call', async () => {
    await firstLogin();

    const login = await firstLogin();

    expect(login.created).toBe(false);
  });

  it('fails the login when the email was taken by another provider in the meantime', async () => {
    await User.create({ email, provider: 'local', username: 'local-user' });

    await expect(firstLogin()).rejects.toThrow(ErrorTypes.AUTH_FAILED);
    expect(await User.countDocuments({ email })).toBe(1);
  });

  it('throws when the lookup cannot account for the existing account', async () => {
    await User.create({ email: '[email protected]', provider: 'openid', openidId: 'other-sub' });

    const login = firstLogin({
      lookup: { findUser: methods.findUser, openidId, openidIssuer: issuer },
      profile: { ...profile, email: '[email protected]' },
    });

    await expect(login).rejects.toThrow('conflicts with an account the lookup cannot resolve');
  });

  it('rethrows other create failures without a second lookup', async () => {
    const findUser = jest.fn(methods.findUser);

    const login = firstLogin({
      lookup: { ...getLookup(), findUser },
      profile: { ...profile, email: 'not-an-email' },
    });

    await expect(login).rejects.toMatchObject({ name: 'ValidationError' });
    expect(findUser).not.toHaveBeenCalled();
  });
});