leases.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. localUserID: 'local-user-a',
  13. })
  14. expect(() => store.consume(
  15. lease.id,
  16. 'groups',
  17. 'http://127.0.0.1:8800',
  18. 'account-a',
  19. )).toThrow(/invalid or expired/)
  20. const consumed = store.consume(
  21. lease.id,
  22. 'chat',
  23. 'http://127.0.0.1:8800',
  24. 'account-a',
  25. )
  26. expect(consumed.credential.value).toBe('upstream-secret')
  27. expect(consumed.localUserID).toBe('local-user-a')
  28. expect(() => store.consume(
  29. lease.id,
  30. 'chat',
  31. 'http://127.0.0.1:8800',
  32. 'account-a',
  33. )).toThrow(/invalid or expired/)
  34. const expiring = store.issue({
  35. channel: 'chat',
  36. credential: { name: 'ticket', value: 'another-secret' },
  37. origin: 'http://127.0.0.1:8800',
  38. accountKeys: ['account-a'],
  39. })
  40. now += 20_001
  41. expect(() => store.consume(
  42. expiring.id,
  43. 'chat',
  44. 'http://127.0.0.1:8800',
  45. 'account-a',
  46. )).toThrow(/invalid or expired/)
  47. })
  48. })