Skip to content

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

4.1 KB
Raw Blame
/*
 * Copyright 2025 Daytona Platforms Inc.
 * Modified by BoxLite AI, 2025-2026
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Test } from '@nestjs/testing'
import { ApiKey } from '../api-key/api-key.entity'
import { AuthContext } from '../common/interfaces/auth-context.interface'
import { OrganizationMemberRole } from '../organization/enums/organization-member-role.enum'
import { OrganizationResourcePermission as Permission } from '../organization/enums/organization-resource-permission.enum'
import { OrganizationService } from '../organization/services/organization.service'
import { OrganizationUserService } from '../organization/services/organization-user.service'
import { SystemRole } from '../user/enums/system-role.enum'
import { BoxliteMeController } from './boxlite-me.controller'

describe('BoxliteMeController', () => {
  const user: AuthContext = { userId: 'user-2', email: '[email protected]', role: SystemRole.USER }

  async function makeController(
    findByUserWithDefaultFlag = jest.fn().mockResolvedValue([]),
    findOneCached = jest.fn().mockResolvedValue(null),
  ): Promise<BoxliteMeController> {
    const module = await Test.createTestingModule({
      controllers: [BoxliteMeController],
      providers: [
        { provide: OrganizationService, useValue: { findByUserWithDefaultFlag } },
        { provide: OrganizationUserService, useValue: { findOneCached } },
      ],
    }).compile()
    return module.get(BoxliteMeController)
  }

  function apiKeyContext(expiresAt?: Date): AuthContext {
    const apiKey = {
      organizationId: '11111111-1111-1111-1111-111111111111',
      userId: 'user-1',
      name: 'key-531',
      expiresAt,
    } as ApiKey
    return {
      userId: 'user-1',
      email: '[email protected]',
      role: SystemRole.USER,
      apiKey,
      organizationId: apiKey.organizationId,
    }
  }

  describe('GET /v1/me — expires_at is sourced from ApiKey.expiresAt', () => {
    it('returns the API key expiry as an ISO string (same field the dashboard renders)', async () => {
      const expiresAt = new Date('2026-06-07T12:00:00.000Z')
      const controller = await makeController()
      const result = await controller.getMe(apiKeyContext(expiresAt))

      expect(result.principal_type).toBe('service_account')
      expect(result.expires_at).toBe(expiresAt.toISOString())
    })

    it('returns null for a non-expiring API key (expiresAt unset)', async () => {
      const controller = await makeController()
      const result = await controller.getMe(apiKeyContext(undefined))

      expect(result.expires_at).toBeNull()
    })

    it('returns null for an interactive user session (no API key)', async () => {
      const controller = await makeController()
      const result = await controller.getMe(user)

      expect(result.principal_type).toBe('user')
      expect(result.expires_at).toBeNull()
    })
  })

  it.each([
    [true, 'marked-default', ['box:read', 'image:read', 'me:read']],
    [false, 'first-membership', ['box:read', 'box:delete', 'me:read']],
  ])(
    'reports the interactive user scopes for the resolved prefix (default marked: %s)',
    async (markDefault, prefix, scopes) => {
      const findOrganizations = jest.fn().mockResolvedValue([
        { organization: { id: 'first-membership' }, isDefaultForAuthenticatedUser: false },
        { organization: { id: 'marked-default' }, isDefaultForAuthenticatedUser: markDefault },
      ])
      const findMembership = jest.fn(async (organizationId: string) => ({
        role: OrganizationMemberRole.MEMBER,
        assignedRoles: [
          { permissions: [organizationId === 'marked-default' ? Permission.READ_IMAGES : Permission.DELETE_BOXES] },
        ],
      }))
      const controller = await makeController(findOrganizations, findMembership)

      expect(await controller.getMe(user)).toMatchObject({ path_prefix: prefix, scopes })
    },
  )

  it('reports identity access alone for an interactive user with no organization', async () => {
    const controller = await makeController()

    expect(await controller.getMe(user)).toMatchObject({ path_prefix: null, scopes: ['me:read'] })
  })
})