leases.test.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { describe, expect, it } from 'vitest'
  2. import { RealtimeLeaseStore } from '../../src/server/leases.js'
  3. describe('RealtimeLeaseStore', () => {
  4. it('binds a lease to channel, origin, account and one use', () => {
  5. let now = 1_000
  6. const store = new RealtimeLeaseStore(20_000, () => now)
  7. const lease = store.issue({
  8. channel: 'chat',
  9. credential: { name: 'ticket', value: 'upstream-secret' },
  10. origin: 'http://127.0.0.1:8800',
  11. accountKeys: ['account-a'],
  12. })
  13. expect(() => store.consume(
  14. lease.id,
  15. 'groups',
  16. 'http://127.0.0.1:8800',
  17. 'account-a',
  18. )).toThrow(/invalid or expired/)
  19. expect(store.consume(
  20. lease.id,
  21. 'chat',
  22. 'http://127.0.0.1:8800',
  23. 'account-a',
  24. ).credential.value).toBe('upstream-secret')
  25. expect(() => store.consume(
  26. lease.id,
  27. 'chat',
  28. 'http://127.0.0.1:8800',
  29. 'account-a',
  30. )).toThrow(/invalid or expired/)
  31. const expiring = store.issue({
  32. channel: 'chat',
  33. credential: { name: 'ticket', value: 'another-secret' },
  34. origin: 'http://127.0.0.1:8800',
  35. accountKeys: ['account-a'],
  36. })
  37. now += 20_001
  38. expect(() => store.consume(
  39. expiring.id,
  40. 'chat',
  41. 'http://127.0.0.1:8800',
  42. 'account-a',
  43. )).toThrow(/invalid or expired/)
  44. })
  45. })