frontend-api-token-model.test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import {
  4. buildApiUsageExamples,
  5. canCopyFullApiToken,
  6. formatApiTokenPrefix,
  7. getCopyableApiToken,
  8. getCreatedApiTokenSecret
  9. } from '../src/frontend/api-token-model.js';
  10. test('only exposes full API token when the create response includes the secret', () => {
  11. const created = { tokenPrefix: 'mh_123456789', token: 'mh_123456789.full-secret' };
  12. const listed = { tokenPrefix: 'mh_987654321' };
  13. assert.equal(canCopyFullApiToken(created), true);
  14. assert.equal(getCreatedApiTokenSecret(created), 'mh_123456789.full-secret');
  15. assert.equal(getCopyableApiToken(created), 'mh_123456789.full-secret');
  16. assert.equal(canCopyFullApiToken(listed), false);
  17. assert.equal(getCreatedApiTokenSecret(listed), '');
  18. assert.equal(getCopyableApiToken(listed), '');
  19. });
  20. test('formats token prefixes without pretending the full secret is available', () => {
  21. assert.equal(formatApiTokenPrefix({ tokenPrefix: 'mh_abcdef1234' }), 'mh_abcdef1234...');
  22. assert.equal(formatApiTokenPrefix({}), '-');
  23. });
  24. test('builds API usage examples with endpoint, bearer token, and message body', () => {
  25. const examples = buildApiUsageExamples({
  26. endpoint: 'https://mail-send.ss5.xyz/api/send',
  27. token: 'mh_example_token',
  28. from: 'noreply@example.com',
  29. to: 'user@example.com'
  30. });
  31. assert.match(examples.curl, /Authorization: Bearer mh_example_token/);
  32. assert.match(examples.curl, /https:\/\/mail-send\.ss5\.xyz\/api\/send/);
  33. assert.match(examples.nodeFetch, /fetch\('https:\/\/mail-send\.ss5\.xyz\/api\/send'/);
  34. assert.match(examples.nodeFetch, /from: 'noreply@example.com'/);
  35. assert.match(examples.requestBody, /"to": "user@example.com"/);
  36. assert.match(examples.successResponse, /"queued": true/);
  37. });