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

3.9 KB
Raw Blame
import { RecoilRoot } from 'recoil';
import { renderHook } from '@testing-library/react';
import type { TPullRequestConnection, TStartupConfig, TUser } from 'librechat-data-provider';
import type { PropsWithChildren } from 'react';
import { useSettingsContext } from '../context';

type LegacyProviderUser = Omit<TUser, 'provider'> & { provider?: string | null };

let mockUser: LegacyProviderUser | undefined;
let mockStartup: Partial<TStartupConfig> | undefined;
let mockConnection: TPullRequestConnection | undefined;

jest.mock('~/hooks', () => ({
  useAuthContext: () => ({ user: mockUser }),
  useHasAccess: () => false,
  useGetAgentsConfig: () => ({ agentsConfig: undefined }),
}));

jest.mock('~/hooks/usePersonalizationAccess', () => () => ({
  hasAnyPersonalizationFeature: false,
  hasMemoryOptOut: false,
}));

jest.mock('~/data-provider', () => ({
  useGetStartupConfig: () => ({ data: mockStartup }),
  usePullRequestConnectionStatus: () => mockConnection,
}));

jest.mock('../../SettingsTabs/ProviderKeys/useProviderKeys', () => () => []);

const baseUser: Omit<TUser, 'provider'> = {
  id: 'user-1',
  username: 'policy-user',
  email: '[email protected]',
  name: 'Policy User',
  avatar: '',
  role: 'USER',
  createdAt: '2026-01-01T00:00:00.000Z',
  updatedAt: '2026-01-01T00:00:00.000Z',
};

const wrapper = ({ children }: PropsWithChildren) => <RecoilRoot>{children}</RecoilRoot>;

function policyProviderFor(user: LegacyProviderUser | undefined): boolean {
  mockUser = user;
  return renderHook(() => useSettingsContext(), { wrapper }).result.current
    .isTwoFactorPolicyProvider;
}

const userWith = (provider: string | null | undefined): LegacyProviderUser => ({
  ...baseUser,
  provider,
});

describe('useSettingsContext two-factor policy scope', () => {
  afterEach(() => {
    mockUser = undefined;
  });

  it.each(['local', 'ldap', null, undefined])(
    'derives the policy capability from an authenticated user with provider %s',
    (provider) => {
      expect(policyProviderFor(userWith(provider))).toBe(true);
    },
  );

  it('keeps a legacy user document with no provider field eligible', () => {
    expect(policyProviderFor(baseUser)).toBe(true);
  });

  it.each(['openid', 'google', 'saml'])(
    'withholds the policy capability from federated provider %s',
    (provider) => {
      expect(policyProviderFor(userWith(provider))).toBe(false);
    },
  );

  it('withholds the policy capability when no authenticated user is loaded', () => {
    expect(policyProviderFor(undefined)).toBe(false);
  });
});

describe('useSettingsContext connected accounts', () => {
  afterEach(() => {
    mockStartup = undefined;
    mockConnection = undefined;
  });

  const hasConnect = () =>
    renderHook(() => useSettingsContext(), { wrapper }).result.current.hasPullRequestConnect;

  it('offers the section when the deployment advertises an app, before the status loads', () => {
    mockStartup = { pullRequestsConnect: 'github' };
    expect(hasConnect()).toBe(true);
  });

  it('hides the section once the server says no app is configured', () => {
    mockStartup = { pullRequestsConnect: 'github' };
    mockConnection = { configured: false, connected: false };
    expect(hasConnect()).toBe(false);
  });

  it('keeps the section while a stored grant outlives its app', () => {
    mockStartup = { pullRequestsConnect: 'github' };
    mockConnection = { configured: false, connected: true };
    expect(hasConnect()).toBe(true);
  });

  it('offers the section for a surviving grant when no app is advertised any more', () => {
    mockStartup = { pullRequestsEnabled: true };
    mockConnection = { configured: false, connected: true };
    expect(hasConnect()).toBe(true);
  });

  it('never offers the section when the deployment advertises no app', () => {
    mockConnection = { configured: true, provider: 'github', connected: false };
    expect(hasConnect()).toBe(false);
  });
});