lease-broker.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { mkdtemp, readFile, stat } from 'node:fs/promises';
  2. import path from 'node:path';
  3. import { tmpdir } from 'node:os';
  4. import { createNodeAuthLeaseClient } from '@mavis/oauth-lease-protocol';
  5. import { afterEach, describe, expect, it, vi } from 'vitest';
  6. import { startMcodeToolsAuthLeaseBroker } from '../../src/lease-broker.js';
  7. const cleanups: Array<() => Promise<void>> = [];
  8. afterEach(async () => {
  9. await Promise.allSettled(cleanups.splice(0).map((cleanup) => cleanup()));
  10. });
  11. describe('mcode-tools auth lease broker', () => {
  12. it('serves fixed leases and removes the private capability on dispose', async () => {
  13. const dataDir = await mkdtemp(path.join(tmpdir(), 'mtb-'));
  14. const stopWatching = vi.fn();
  15. const broker = await startMcodeToolsAuthLeaseBroker({
  16. dataDir,
  17. session: {
  18. getStatus: async () => ({ status: 'authenticated', generation: 3 }),
  19. getAccessToken: async () => ({
  20. accessToken: 'at-3',
  21. expiresAtMs: Date.now() + 120_000,
  22. generation: 3,
  23. audience: 'agent-backend',
  24. scopes: ['agent.default'],
  25. }),
  26. handleUnauthorized: async () => 'retry',
  27. watch: () => stopWatching,
  28. },
  29. });
  30. cleanups.push(() => broker.dispose());
  31. const client = createNodeAuthLeaseClient({
  32. endpoint: broker.endpoint,
  33. capabilityFile: broker.capabilityFile,
  34. });
  35. cleanups.push(async () => client.close());
  36. await expect(client.getLease(30_000)).resolves.toMatchObject({
  37. accessToken: 'at-3',
  38. generation: 3,
  39. audience: 'agent-backend',
  40. scopes: ['agent.default'],
  41. });
  42. expect((await readFile(broker.capabilityFile, 'utf8')).trim()).toHaveLength(43);
  43. if (process.platform !== 'win32') {
  44. expect((await stat(broker.capabilityFile)).mode & 0o777).toBe(0o600);
  45. }
  46. await broker.dispose();
  47. await expect(stat(broker.capabilityFile)).rejects.toMatchObject({ code: 'ENOENT' });
  48. expect(stopWatching).toHaveBeenCalledOnce();
  49. });
  50. it('rejects a lease outside the fixed audience contract', async () => {
  51. const dataDir = await mkdtemp(path.join(tmpdir(), 'mtb-'));
  52. const broker = await startMcodeToolsAuthLeaseBroker({
  53. dataDir,
  54. session: {
  55. getStatus: async () => ({ status: 'authenticated', generation: 1 }),
  56. getAccessToken: async () => ({
  57. accessToken: 'wrong-at',
  58. expiresAtMs: Date.now() + 120_000,
  59. generation: 1,
  60. audience: 'wrong-audience' as 'agent-backend',
  61. scopes: ['agent.default'],
  62. }),
  63. handleUnauthorized: async () => 'logout',
  64. watch: () => () => undefined,
  65. },
  66. });
  67. cleanups.push(() => broker.dispose());
  68. const client = createNodeAuthLeaseClient({
  69. endpoint: broker.endpoint,
  70. capabilityFile: broker.capabilityFile,
  71. });
  72. cleanups.push(async () => client.close());
  73. await expect(client.getLease(30_000)).rejects.toMatchObject({ code: 'INTERNAL_ERROR' });
  74. });
  75. });