apiKeyQr.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Tests for the API-key QR payload builder.
  3. */
  4. import { describe, it, expect } from 'vitest';
  5. import { buildApiKeyQrPayload, API_KEY_QR_VERSION } from '../../utils/apiKeyQr';
  6. describe('buildApiKeyQrPayload', () => {
  7. it('uses the bambuddy://config scheme with v first', () => {
  8. const payload = buildApiKeyQrPayload('https://printer.local', 'bb_abc123');
  9. expect(payload.startsWith(`bambuddy://config?v=${API_KEY_QR_VERSION}`)).toBe(true);
  10. });
  11. it('encodes the url and key parameters', () => {
  12. const payload = buildApiKeyQrPayload('https://printer.local', 'bb_abc123');
  13. expect(payload).toBe('bambuddy://config?v=1&url=https%3A%2F%2Fprinter.local&key=bb_abc123');
  14. });
  15. it('URL-encodes special characters in both values', () => {
  16. const baseUrl = 'http://host:5173/sub path';
  17. const key = 'bb_a+b/c=d&e';
  18. const payload = buildApiKeyQrPayload(baseUrl, key);
  19. expect(payload).toContain(`url=${encodeURIComponent(baseUrl)}`);
  20. expect(payload).toContain(`key=${encodeURIComponent(key)}`);
  21. // The raw, unencoded key must never leak into the payload.
  22. expect(payload).not.toContain(key);
  23. });
  24. it('round-trips the values back out of the query string', () => {
  25. const baseUrl = 'https://my.bambuddy.example:8443';
  26. const key = 'bb_ZZ/99+aa==';
  27. const payload = buildApiKeyQrPayload(baseUrl, key);
  28. const query = payload.slice(payload.indexOf('?') + 1);
  29. const params = new URLSearchParams(query);
  30. expect(params.get('v')).toBe(String(API_KEY_QR_VERSION));
  31. expect(params.get('url')).toBe(baseUrl);
  32. expect(params.get('key')).toBe(key);
  33. });
  34. });