Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

8.1 KB
Raw Blame
/*
 * Copyright 2025 BoxLite AI
 * SPDX-License-Identifier: AGPL-3.0
 */

import { ConflictException, ExecutionContext, NotFoundException } from '@nestjs/common'
import { OrganizationAuthContext } from '../../common/interfaces/auth-context.interface'
import { SystemRole } from '../../user/enums/system-role.enum'
import { Organization } from '../entities/organization.entity'
import { OrganizationAccessGuard } from '../guards/organization-access.guard'
import { OrganizationService } from './organization.service'

const organization = (overrides: Partial<Organization> = {}): Organization =>
  Object.assign(new Organization(), {
    id: 'org-1',
    suspended: false,
    suspensionReason: null,
    suspendedUntil: null,
    suspendedAt: null,
    ...overrides,
  })

// unsuspend() only ever touches organizationRepository; the rest of the
// constructor's dependencies are unused stubs to satisfy DI.
const makeService = (found: Organization | null) => {
  const organizationRepository = {
    findOne: jest.fn().mockResolvedValue(found),
    save: jest.fn().mockImplementation((org: Organization) => Promise.resolve(org)),
  }
  const configService = { getOrThrow: jest.fn().mockReturnValue(false), get: jest.fn() }

  const service = new OrganizationService(
    organizationRepository as any,
    {} as any,
    {} as any,
    configService as any,
    {} as any,
    {} as any,
    {} as any,
  )

  return { service, organizationRepository }
}

describe('OrganizationService.unsuspend', () => {
  it('unsuspends unconditionally when no ifReason is given', async () => {
    const { service, organizationRepository } = makeService(
      organization({ suspended: true, suspensionReason: 'abuse', suspendedAt: new Date() }),
    )

    await service.unsuspend('org-1')

    expect(organizationRepository.save).toHaveBeenCalledWith(
      expect.objectContaining({ suspended: false, suspensionReason: null, suspendedAt: null }),
    )
  })

  it('unsuspends when ifReason matches the current suspension reason exactly', async () => {
    const { service, organizationRepository } = makeService(
      organization({ suspended: true, suspensionReason: 'abuse' }),
    )

    await service.unsuspend('org-1', 'abuse')

    expect(organizationRepository.save).toHaveBeenCalledWith(expect.objectContaining({ suspended: false }))
  })

  // An admin may have re-suspended for a different reason after a credit hold
  // was placed; releasing the hold must not clear that unrelated suspension.
  it('leaves the organization untouched and throws 409 when ifReason does not match', async () => {
    const { service, organizationRepository } = makeService(
      organization({ suspended: true, suspensionReason: 'manually suspended by an admin' }),
    )

    await expect(service.unsuspend('org-1', 'credits depleted')).rejects.toThrow(ConflictException)
    expect(organizationRepository.save).not.toHaveBeenCalled()
  })

  // suspensionReason is free text an admin or support agent wrote — a caller probing with
  // guessed ifReason values must not be able to read it back out of the error.
  it('does not leak the stored suspension reason into the 409 message', async () => {
    const { service } = makeService(
      organization({ suspended: true, suspensionReason: 'flagged for suspected abuse by support' }),
    )

    await expect(service.unsuspend('org-1', 'credits depleted')).rejects.toMatchObject({
      message: expect.not.stringContaining('flagged for suspected abuse by support'),
    })
  })

  it('leaves the organization untouched and throws 409 when ifReason is given but the org is not suspended', async () => {
    const { service, organizationRepository } = makeService(organization())

    await expect(service.unsuspend('org-1', 'credits depleted')).rejects.toThrow(ConflictException)
    expect(organizationRepository.save).not.toHaveBeenCalled()
  })

  it('throws 404 for an unknown organization', async () => {
    const { service } = makeService(null)

    await expect(service.unsuspend('missing')).rejects.toThrow(NotFoundException)
  })
})

describe('OrganizationService.handleUserEmailVerifiedEvent', () => {
  const verificationReason = 'Please verify your email address'

  const eventContext = (found: Organization) => {
    const entityManager = {
      findOne: jest.fn().mockResolvedValue({ organization: found }),
      save: jest.fn().mockImplementation((org: Organization) => Promise.resolve(org)),
    }
    const { service } = makeService(null)
    return { service, entityManager, payload: { entityManager, userId: 'user-1' } as any }
  }

  it('clears the default organization suspension created by email verification', async () => {
    const suspendedAt = new Date()
    const { service, entityManager, payload } = eventContext(
      organization({ suspended: true, suspensionReason: verificationReason, suspendedAt }),
    )

    await service.handleUserEmailVerifiedEvent(payload)

    expect(entityManager.save).toHaveBeenCalledWith(
      Organization,
      expect.objectContaining({ suspended: false, suspensionReason: null, suspendedAt: null }),
    )
  })

  it('preserves an unrelated administrator or billing suspension', async () => {
    const suspended = organization({ suspended: true, suspensionReason: 'Payment method required' })
    const { service, entityManager, payload } = eventContext(suspended)

    await service.handleUserEmailVerifiedEvent(payload)

    expect(entityManager.save).not.toHaveBeenCalled()
    expect(suspended).toMatchObject({ suspended: true, suspensionReason: 'Payment method required' })
  })

  it('does nothing when the organization is already active', async () => {
    const { service, entityManager, payload } = eventContext(organization())

    await service.handleUserEmailVerifiedEvent(payload)

    expect(entityManager.save).not.toHaveBeenCalled()
  })
})

describe('OrganizationService.assertOrganizationIsNotSuspended', () => {
  it('does not throw for an active organization', () => {
    const { service } = makeService(organization())

    expect(() => service.assertOrganizationIsNotSuspended(organization())).not.toThrow()
  })

  // suspensionReason is free text an admin or support agent wrote and may describe a case
  // (e.g. a fraud investigation) in more detail than "you are suspended" should disclose.
  it.each(['temporary', 'indefinite'])(
    'rejects a %s suspension without leaking its reason on cache hits',
    async (kind) => {
      const suspendedUntil = kind === 'temporary' ? new Date(Date.now() + 60_000) : null
      const { service, organizationRepository } = makeService(
        organization({ suspended: true, suspendedUntil, suspensionReason: 'flagged for suspected abuse by support' }),
      )
      const guard = new OrganizationAccessGuard(service, {
        findOneCached: jest.fn().mockResolvedValue({ organizationId: 'org-1', userId: 'user-1' }),
      } as any)
      const cache = new Map<string, string>()
      ;(guard as any).redis = {
        get: async (key: string) => cache.get(key) ?? null,
        set: async (key: string, value: string) => cache.set(key, value),
      }

      for (let read = 0; read < 2; read++) {
        const request = {
          params: { organizationId: 'org-1' },
          user: { userId: 'user-1', role: SystemRole.USER },
        }
        const context = { switchToHttp: () => ({ getRequest: () => request }) } as ExecutionContext
        await expect(guard.canActivate(context)).resolves.toBe(true)
        const found = (request.user as OrganizationAuthContext).organization

        expect(() => service.assertOrganizationIsNotSuspended(found)).toThrow(
          expect.objectContaining({
            status: 403,
            message: expect.not.stringContaining('flagged for suspected abuse by support'),
          }),
        )
        expect(found.suspendedUntil).toEqual(suspendedUntil)
      }
      expect(organizationRepository.findOne).toHaveBeenCalledTimes(1)
    },
  )

  it('does not throw once a temporary suspension has expired', () => {
    const { service } = makeService(organization())
    const expired = organization({ suspended: true, suspendedUntil: new Date(Date.now() - 1000) })

    expect(() => service.assertOrganizationIsNotSuspended(expired)).not.toThrow()
  })
})