Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
20.3 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest'
import express from 'express'
import request from 'supertest'
import { config } from '../src/config.js'
import { rootLogger } from '../src/observability/logger.js'
import { createAdminAuthRouter } from '../src/routes/admin/auth.js'
import { PasswordAdmissionError } from '../src/services/auth/passwordCredentialVerification.js'

const directorySvc = vi.hoisted(() => ({
  provisionAdminDesktopWorkspace: vi.fn(),
  acceptInvitationById: vi.fn(),
  getPendingMemberInvitationForEmail: vi.fn(),
  setInvitationPasswordForUser: vi.fn(),
  withMemberPasswordWorkLease: vi.fn(),
}))
// auth.ts imports provisionAdminDesktopWorkspace from the directory barrel; mock the barrel here.
vi.mock('../src/services/directory/index.js', () => directorySvc)
vi.mock('../src/services/directory/membership.js', () => ({
  withMemberPasswordWorkLease: directorySvc.withMemberPasswordWorkLease,
}))

const adminSvc = vi.hoisted(() => ({
  completeControlAdminEmailChangeRequest: vi.fn(),
  completeControlAdminInvitation: vi.fn(),
  completeControlAdminPasswordResetRequest: vi.fn(),
  createControlAdminPasswordResetRequest: vi.fn(),
  findAdminById: vi.fn(),
  findAdminByLogin: vi.fn(),
  getPendingControlAdminEmailChangeRequest: vi.fn(),
  getPendingControlAdminInvitation: vi.fn(),
  getPendingControlAdminPasswordResetRequest: vi.fn(),
  isValidAdminEmail: vi.fn(() => true),
  isValidAdminUsername: vi.fn(() => true),
  registerAdminFailedLogin: vi.fn(),
  registerAdminSuccessfulLogin: vi.fn(),
  revokeControlAdminPasswordResetRequest: vi.fn(),
  revokeAdminTokenJti: vi.fn(),
  setupInitialAdminCredentials: vi.fn(),
  setupInitialAdminWithDesktopWorkspace: vi.fn(),
}))

const adminToken = vi.hoisted(() => ({
  signAdminToken: vi.fn(() => 'admin-jwt'),
}))

const controlAdminInvitationRegistrationSvc = vi.hoisted(() => ({
  registerAndSendControlAdminPasswordReset: vi.fn(),
  validateControlAdminEmailConfirmationToken: vi.fn(),
  validateControlAdminInvitationToken: vi.fn(),
  validateControlAdminPasswordResetToken: vi.fn(),
}))

const uiAuth = vi.hoisted(() => ({
  requireAuthForControlUI: vi.fn((req: any, _res: any, next: any) => {
    req.adminAuth = { sub: 'admin-id', role: 'admin', jti: 'j1', exp: 9999999999, typ: 'user' }
    next()
  }),
}))

const rateLimit = vi.hoisted(() => ({
  clearRateLimitHeaders: vi.fn(),
  rateLimitMiddleware: vi.fn(() => (_req: any, _res: any, next: any) => next()),
}))

vi.mock('../src/services/adminAuthService.js', () => adminSvc)
vi.mock('../src/services/initialAdminSetupService.js', () => ({
  setupInitialAdminWithDesktopWorkspace: adminSvc.setupInitialAdminWithDesktopWorkspace,
}))
vi.mock(
  '../src/services/controlAdminInvitationRegistrationService.js',
  () => controlAdminInvitationRegistrationSvc
)
vi.mock('../src/utils/auth/adminAuthToken.js', () => adminToken)
vi.mock('../src/middleware/controlUIAuth.js', () => uiAuth)
vi.mock('../src/middleware/rateLimitMiddleware.js', () => rateLimit)

describe('routes/adminAuth', () => {
  beforeEach(() => {
    Object.values(directorySvc).forEach(fn => fn.mockReset())
    Object.values(adminSvc).forEach(fn => fn.mockReset())
    Object.values(adminToken).forEach(fn => fn.mockReset())
    Object.values(controlAdminInvitationRegistrationSvc).forEach(fn => fn.mockReset())
    Object.values(uiAuth).forEach(fn => fn.mockReset())
    Object.values(rateLimit).forEach(fn => fn.mockClear())
    directorySvc.withMemberPasswordWorkLease.mockImplementation(
      async (work: (lease: unknown) => Promise<unknown>) =>
        work({
          commitWith: vi.fn(),
        })
    )
    adminSvc.isValidAdminEmail.mockReturnValue(true)
    adminSvc.isValidAdminUsername.mockReturnValue(true)
    config.desktopGfsOperatorLinkingEnabled = false
    adminToken.signAdminToken.mockReturnValue('admin-jwt')
    uiAuth.requireAuthForControlUI.mockImplementation((req: any, _res: any, next: any) => {
      req.adminAuth = { sub: 'admin-id', role: 'admin', jti: 'j1', exp: 9999999999, typ: 'user' }
      next()
    })
  })

  it('logs in with valid username/password', async () => {
    adminSvc.findAdminByLogin.mockResolvedValue({
      id: 'admin-id',
      username: 'admin',
      email: '[email protected]',
      passwordHash: '$2b$12$9QdfGGp5KYg8osGa1n0.DuwQiB1RopCWIDJhmsuK4ygjTmIT8pvgy',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    adminSvc.registerAdminSuccessfulLogin.mockResolvedValue(undefined)

    const app = express()
    app.set('trust proxy', 1)
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/login')
      .set('x-forwarded-proto', 'https')
      .send({ username: 'admin', password: 'changeme123!' })
      .expect(200)

    expect(res.body.token).toBeUndefined()
    expect(String(res.headers['set-cookie'])).toContain('control_ui_admin_session=admin-jwt')
    expect(String(res.headers['set-cookie'])).toContain('HttpOnly')
    expect(String(res.headers['set-cookie'])).toContain('Secure')
    expect(String(res.headers['set-cookie'])).toContain('SameSite=Lax')
    expect(adminSvc.registerAdminSuccessfulLogin).toHaveBeenCalledWith('admin-id')
  })

  it('sets up the initial admin with email, username, and password', async () => {
    adminSvc.setupInitialAdminCredentials.mockResolvedValue({
      id: 'admin-id',
      username: 'admin',
      email: '[email protected]',
      passwordHash: 'hash',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/setup')
      .send({ email: '[email protected]', username: 'admin', password: 'changeme123!' })
      .expect(200)

    expect(res.body.me).toMatchObject({
      id: 'admin-id',
      username: 'admin',
      email: '[email protected]',
      role: 'admin',
    })
    expect(adminSvc.setupInitialAdminCredentials).toHaveBeenCalledWith(
      expect.any(String),
      '[email protected]',
      'admin',
      expect.stringMatching(/^\$2/)
    )
  })

  it('rejects invalid password and updates failure count', async () => {
    adminSvc.findAdminByLogin.mockResolvedValue({
      id: 'admin-id',
      username: 'admin',
      email: '[email protected]',
      passwordHash: '$2b$12$9QdfGGp5KYg8osGa1n0.DuwQiB1RopCWIDJhmsuK4ygjTmIT8pvgy',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    adminSvc.registerAdminFailedLogin.mockResolvedValue(undefined)

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    await request(app)
      .post('/admin/auth/login')
      .send({ username: 'admin', password: 'wrong-pass' })
      .expect(401)

    expect(adminSvc.registerAdminFailedLogin).toHaveBeenCalled()
  })

  it('provisions the desktop workspace after first-run setup', async () => {
    adminSvc.setupInitialAdminCredentials.mockResolvedValue({
      id: 'admin-1',
      username: 'newadmin',
      email: '[email protected]',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    directorySvc.provisionAdminDesktopWorkspace.mockResolvedValue(undefined)

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/setup')
      .send({ email: '[email protected]', username: 'newadmin', password: 'secret123' })

    expect(res.status).toBe(200)
    expect(directorySvc.provisionAdminDesktopWorkspace).toHaveBeenCalledWith(
      expect.objectContaining({
        controlAdminId: 'admin-1',
        email: '[email protected]',
        displayName: 'newadmin',
        passwordHash: expect.any(String),
        agentNames: expect.any(Array),
        contextIds: expect.any(Array),
        linkDesktopOperator: false,
      })
    )
  })

  it('requests the exact initial-setup link only when the narrow self-hosted flag is enabled', async () => {
    config.desktopGfsOperatorLinkingEnabled = true
    adminSvc.setupInitialAdminWithDesktopWorkspace.mockResolvedValue({
      id: 'admin-1',
      username: 'newadmin',
      email: '[email protected]',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    await request(app)
      .post('/admin/auth/setup')
      .send({ email: '[email protected]', username: 'newadmin', password: 'secret123' })
      .expect(200)

    expect(adminSvc.setupInitialAdminWithDesktopWorkspace).toHaveBeenCalledWith(
      expect.objectContaining({
        bootstrapUsername: expect.any(String),
        email: '[email protected]',
        username: 'newadmin',
        linkDesktopOperator: true,
      })
    )
  })

  it('threads seedDesktopPassword:false through to provisioning as seedPassword:false', async () => {
    adminSvc.setupInitialAdminCredentials.mockResolvedValue({
      id: 'admin-1',
      username: 'newadmin',
      email: '[email protected]',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    directorySvc.provisionAdminDesktopWorkspace.mockResolvedValue(undefined)

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app).post('/admin/auth/setup').send({
      email: '[email protected]',
      username: 'newadmin',
      password: 'secret123',
      seedDesktopPassword: false,
    })

    expect(res.status).toBe(200)
    expect(directorySvc.provisionAdminDesktopWorkspace).toHaveBeenCalledWith(
      expect.objectContaining({ seedPassword: false })
    )
  })

  it('defaults seedPassword:true when the flag is absent or non-boolean', async () => {
    adminSvc.setupInitialAdminCredentials.mockResolvedValue({
      id: 'admin-1',
      username: 'newadmin',
      email: '[email protected]',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    directorySvc.provisionAdminDesktopWorkspace.mockResolvedValue(undefined)

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    // absent → true
    await request(app)
      .post('/admin/auth/setup')
      .send({ email: '[email protected]', username: 'newadmin', password: 'secret123' })
    expect(directorySvc.provisionAdminDesktopWorkspace).toHaveBeenLastCalledWith(
      expect.objectContaining({ seedPassword: true })
    )

    // string "false" (non-boolean junk) → true; only literal false opts out
    adminSvc.setupInitialAdminCredentials.mockResolvedValue({
      id: 'admin-1',
      username: 'newadmin',
      email: '[email protected]',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    await request(app).post('/admin/auth/setup').send({
      email: '[email protected]',
      username: 'newadmin2',
      password: 'secret123',
      seedDesktopPassword: 'false',
    })
    expect(directorySvc.provisionAdminDesktopWorkspace).toHaveBeenLastCalledWith(
      expect.objectContaining({ seedPassword: true })
    )
  })

  it('fails closed and remains retryable when self-hosted provisioning throws', async () => {
    config.desktopGfsOperatorLinkingEnabled = true
    adminSvc.setupInitialAdminWithDesktopWorkspace.mockRejectedValue(new Error('boom'))
    adminSvc.setupInitialAdminCredentials.mockResolvedValue({
      id: 'admin-1',
      username: 'newadmin',
      email: '[email protected]',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/setup')
      .send({ email: '[email protected]', username: 'newadmin', password: 'secret123' })

    expect(res.status).toBe(503)
    expect(res.body.token).toBeUndefined()
    expect(String(res.headers['set-cookie'] || '')).not.toContain('control_ui_admin_session=')
    expect(res.body).toEqual({ error: 'Initial admin setup is incomplete; retry' })
  })

  it('returns me and supports logout', async () => {
    adminSvc.findAdminById.mockResolvedValue({
      id: 'admin-id',
      username: 'admin',
      email: '[email protected]',
      passwordHash: 'hash',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    adminSvc.revokeAdminTokenJti.mockResolvedValue(undefined)

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const meRes = await request(app)
      .get('/admin/auth/me')
      .set('authorization', 'Bearer token')
      .expect(200)
    // Exposes the recipe-secret namespaces the UI must write secrets to (the
    // same source of truth POST /admin/recipes/secrets validates against).
    expect(meRes.body.namespaces).toEqual({
      sandbox: config.sandboxNamespace,
      mcpServer: config.mcpServersNamespace,
    })
    const logoutRes = await request(app)
      .post('/admin/auth/logout')
      .set('authorization', 'Bearer token')
      .expect(200)
    expect(String(logoutRes.headers['set-cookie'])).toContain('control_ui_admin_session=')
    expect(String(logoutRes.headers['set-cookie'])).toContain('Expires=Thu, 01 Jan 1970')
    expect(adminSvc.revokeAdminTokenJti).toHaveBeenCalledWith('j1', 9999999999)
  })

  it('validates control admin email confirmation tokens', async () => {
    controlAdminInvitationRegistrationSvc.validateControlAdminEmailConfirmationToken.mockResolvedValue(
      {
        email: '[email protected]',
        confirmationUuid: 'confirmation-id',
      }
    )
    adminSvc.getPendingControlAdminEmailChangeRequest.mockResolvedValue({
      id: 'confirmation-id',
      adminId: 'admin-id',
      email: '[email protected]',
      status: 'pending',
      expiresAt: new Date('2026-04-23T10:00:00.000Z'),
      createdAt: new Date('2026-04-21T10:00:00.000Z'),
      confirmedAt: null,
    })

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/control-admin-email-confirmations/validate')
      .send({ token: 'signed.jwt.value' })
      .expect(200)

    expect(res.body).toEqual({
      valid: true,
      email: '[email protected]',
      confirmationUuid: 'confirmation-id',
    })
    expect(
      controlAdminInvitationRegistrationSvc.validateControlAdminEmailConfirmationToken
    ).toHaveBeenCalledWith('signed.jwt.value', undefined)
  })

  it('completes control admin email confirmations', async () => {
    controlAdminInvitationRegistrationSvc.validateControlAdminEmailConfirmationToken.mockResolvedValue(
      {
        email: '[email protected]',
        confirmationUuid: 'confirmation-id',
      }
    )
    adminSvc.completeControlAdminEmailChangeRequest.mockResolvedValue({
      id: 'admin-id',
      username: 'admin',
      email: '[email protected]',
      passwordHash: 'hash',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/control-admin-email-confirmations/complete')
      .send({ token: 'signed.jwt.value', email: '[email protected]' })
      .expect(200)

    expect(res.body).toEqual({
      completed: true,
      alreadyConfirmed: false,
      login: {
        username: '[email protected]',
      },
    })
    expect(adminSvc.completeControlAdminEmailChangeRequest).toHaveBeenCalledWith({
      requestId: 'confirmation-id',
      email: '[email protected]',
    })
  })

  it('silently accepts password reset requests when no email-backed admin is found', async () => {
    adminSvc.createControlAdminPasswordResetRequest.mockResolvedValue({ skipped: true })

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/password-reset/request')
      .send({ username: 'missing-admin' })
      .expect(202)

    expect(res.body).toEqual({ requested: true })
    expect(
      controlAdminInvitationRegistrationSvc.registerAndSendControlAdminPasswordReset
    ).not.toHaveBeenCalled()
  })

  it('completes control admin password resets', async () => {
    controlAdminInvitationRegistrationSvc.validateControlAdminPasswordResetToken.mockResolvedValue({
      email: '[email protected]',
      resetUuid: 'reset-id',
    })
    adminSvc.completeControlAdminPasswordResetRequest.mockResolvedValue({
      id: 'admin-id',
      username: 'admin',
      email: '[email protected]',
      passwordHash: 'hash',
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app)
      .post('/admin/auth/password-reset/complete')
      .send({ token: 'signed.jwt.value', email: '[email protected]', password: 'newpass!' })
      .expect(200)

    expect(res.body).toEqual({
      completed: true,
      login: {
        username: '[email protected]',
      },
    })
    expect(adminSvc.completeControlAdminPasswordResetRequest).toHaveBeenCalledWith({
      requestId: 'reset-id',
      email: '[email protected]',
      passwordHash: expect.stringMatching(/^\$2/),
    })
  })

  it('logs a structured error when desktop acceptance fails after the admin invitation commits', async () => {
    const invitationId = '11111111-1111-4111-8111-111111111111'
    const adminId = '22222222-2222-4222-8222-222222222222'
    controlAdminInvitationRegistrationSvc.validateControlAdminInvitationToken.mockResolvedValue({
      email: '[email protected]',
      invitationUuid: invitationId,
    })
    adminSvc.completeControlAdminInvitation.mockResolvedValue({
      id: adminId,
      username: 'new-admin',
      email: '[email protected]',
      passwordHash: 'hash',
      sessionVersion: 1,
      role: 'admin',
      status: 'active',
      failedAttempts: 0,
      lockedUntil: null,
    })
    directorySvc.getPendingMemberInvitationForEmail.mockResolvedValue({ id: 'desk-invite' })
    directorySvc.acceptInvitationById.mockResolvedValue({ error: 'user_retired' })
    const errorLog = vi.spyOn(rootLogger, 'error').mockImplementation(() => undefined as never)

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app).post('/admin/auth/control-admin-invitations/complete').send({
      token: 'example.jwt.value',
      email: '[email protected]',
      username: 'new-admin',
      password: 'example-password',
    })

    expect(res.status).toBe(409)
    expect(res.body).toEqual({ error: 'user_retired' })
    expect(errorLog).toHaveBeenCalledWith(
      expect.objectContaining({
        event: 'control_admin_desktop_acceptance_failed',
        invitationId,
        adminId,
        errorCode: 'user_retired',
      }),
      expect.any(String)
    )
    const payload = errorLog.mock.calls[0]?.[0] as Record<string, unknown>
    expect(payload).not.toHaveProperty('email')
    expect(JSON.stringify(payload)).not.toContain('[email protected]')
    errorLog.mockRestore()
  })

  it('denies bundled desktop completion before consuming the admin invitation when password work is busy', async () => {
    controlAdminInvitationRegistrationSvc.validateControlAdminInvitationToken.mockResolvedValue({
      email: '[email protected]',
      invitationUuid: '11111111-1111-4111-8111-111111111111',
    })
    directorySvc.getPendingMemberInvitationForEmail.mockResolvedValue({ id: 'desk-invite' })
    directorySvc.withMemberPasswordWorkLease.mockRejectedValue(
      new PasswordAdmissionError(429, 8, 'verification_busy')
    )

    const app = express()
    app.use(express.json())
    app.use(createAdminAuthRouter())

    const res = await request(app).post('/admin/auth/control-admin-invitations/complete').send({
      token: 'example.jwt.value',
      email: '[email protected]',
      username: 'new-admin',
      password: 'example-password',
    })

    expect(res.status).toBe(429)
    expect(res.headers['retry-after']).toBe('8')
    expect(res.body).toEqual({ error: 'rate_limited', retryAfterSeconds: 8 })
    expect(adminSvc.completeControlAdminInvitation).not.toHaveBeenCalled()
    expect(directorySvc.acceptInvitationById).not.toHaveBeenCalled()
  })
})