Skip to content

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

TypeScript
5.1 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest'
import express from 'express'
import request from 'supertest'
import { ControlApiError } from '../src/controlApiClient.js'
import { createAuthRouter } from '../src/routes/auth.js'

const authServiceMock = vi.hoisted(() => ({
  loginWithGoogle: vi.fn(),
  loginWithPassword: vi.fn(),
  requestPasswordReset: vi.fn(),
}))

vi.mock('../src/services/authService.js', () => authServiceMock)

function buildApp() {
  const app = express()
  app.set('trust proxy', 1)
  app.use(express.json())
  app.use('/api/v1', createAuthRouter())
  app.use(
    (err: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
      res.status(500).json({
        error: err instanceof Error ? err.message : 'Unknown error',
      })
    }
  )
  return app
}

describe('routes/auth password-login', () => {
  beforeEach(() => {
    authServiceMock.loginWithPassword.mockReset()
    authServiceMock.requestPasswordReset.mockReset()
  })

  it('propagates invalid credentials as a 401 instead of a 500', async () => {
    authServiceMock.loginWithPassword.mockRejectedValueOnce(
      new ControlApiError('control-api error (401)', 401, { error: 'Unauthorized' })
    )

    const res = await request(buildApp())
      .post('/api/v1/auth/password-login')
      .send({ email: '[email protected]', password: 'wrong-password' })

    expect(res.status).toBe(401)
    expect(res.body).toEqual({ error: 'invalid_credentials' })
  })

  it('sanitizes password-operation programming errors without marking them retryable', async () => {
    authServiceMock.loginWithPassword.mockRejectedValueOnce(
      new TypeError('private URL and credential detail')
    )

    const res = await request(buildApp())
      .post('/api/v1/auth/password-login')
      .send({ email: '[email protected]', password: 'synthetic-password' })

    expect(res.status).toBe(500)
    expect(res.body).toEqual({ error: 'Internal Server Error' })
    expect(res.headers['retry-after']).toBeUndefined()
    expect(res.text).not.toContain('private URL')
    expect(res.text).not.toContain('credential detail')
  })

  it('sets an HttpOnly profile session cookie and omits bearer token body for browser login', async () => {
    authServiceMock.loginWithPassword.mockResolvedValueOnce({
      token: 'profile-session-jwt',
      me: {
        id: 'user-1',
        email: '[email protected]',
        name: null,
        picture: null,
        teamId: null,
        teamName: null,
        role: 'member',
      },
    })

    const res = await request(buildApp())
      .post('/api/v1/auth/password-login')
      .set('origin', 'http://localhost:3001')
      .set('x-forwarded-proto', 'https')
      .send({ email: '[email protected]', password: 'correct-password' })
      .expect(200)

    expect(res.body.token).toBeUndefined()
    expect(res.body.me.email).toBe('[email protected]')
    expect(String(res.headers['set-cookie'])).toContain('profile_session=profile-session-jwt')
    expect(String(res.headers['set-cookie'])).toContain('HttpOnly')
    expect(String(res.headers['set-cookie'])).toContain('Max-Age=43200')
    expect(String(res.headers['set-cookie'])).toContain('Secure')
    expect(String(res.headers['set-cookie'])).toContain('SameSite=Lax')
  })

  it('returns the bearer token body for non-browser Desktop App login', async () => {
    authServiceMock.loginWithPassword.mockResolvedValueOnce({
      token: 'desktop-session-jwt',
      me: {
        id: 'user-1',
        email: '[email protected]',
        name: null,
        picture: null,
        teamId: null,
        teamName: null,
        role: 'member',
      },
    })

    const res = await request(buildApp())
      .post('/api/v1/auth/password-login')
      .send({ email: '[email protected]', password: 'correct-password' })
      .expect(200)

    expect(res.body.token).toBe('desktop-session-jwt')
    expect(res.body.me.email).toBe('[email protected]')
    expect(String(res.headers['set-cookie'])).toContain('profile_session=desktop-session-jwt')
  })

  it('documents the password-reset limiter headers and client-IP contract', async () => {
    authServiceMock.requestPasswordReset.mockResolvedValue({ requested: true })
    const app = buildApp()
    const email = '[email protected]'

    for (let attempt = 0; attempt < 5; attempt += 1) {
      const res = await request(app)
        .post('/api/v1/auth/password-reset/request')
        .set('x-forwarded-for', '198.51.100.44')
        .send({ email })
        .expect(200)
      expect(res.headers['x-ratelimit-limit']).toBe('5')
      expect(res.headers['x-ratelimit-remaining']).toBe(String(4 - attempt))
    }

    const limited = await request(app)
      .post('/api/v1/auth/password-reset/request')
      .set('x-forwarded-for', '198.51.100.44')
      .send({ email })
      .expect(429)
    expect(limited.headers['retry-after']).toMatch(/^\d+$/)
    expect(limited.headers['x-ratelimit-limit']).toBe('5')
    expect(limited.headers['x-ratelimit-remaining']).toBe('0')
    expect(limited.body).toMatchObject({ retryAfterSeconds: expect.any(Number) })
  })
})