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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import {
  4. buildApiUsageExamples,
  5. buildInboundMessageApiUsageExamples,
  6. buildMailboxApiUsageExamples,
  7. canCopyFullApiToken,
  8. formatApiTokenPrefix,
  9. getCopyableApiToken,
  10. getCreatedApiTokenSecret
  11. } from '../src/frontend/api-token-model.js';
  12. test('only exposes full API token when the create response includes the secret', () => {
  13. const created = { tokenPrefix: 'mh_123456789', token: 'mh_123456789.full-secret' };
  14. const explicitlyUnrecoverable = { ...created, tokenRecoverable: false };
  15. const listed = { tokenPrefix: 'mh_987654321' };
  16. // Keep compatibility with older create-response fixtures that predate tokenRecoverable.
  17. assert.equal(canCopyFullApiToken(created), true);
  18. assert.equal(getCreatedApiTokenSecret(created), 'mh_123456789.full-secret');
  19. assert.equal(getCopyableApiToken(created), 'mh_123456789.full-secret');
  20. assert.equal(canCopyFullApiToken(explicitlyUnrecoverable), false);
  21. assert.equal(canCopyFullApiToken(listed), false);
  22. assert.equal(getCreatedApiTokenSecret(listed), '');
  23. assert.equal(getCopyableApiToken(listed), '');
  24. });
  25. test('formats token prefixes without pretending the full secret is available', () => {
  26. assert.equal(formatApiTokenPrefix({ tokenPrefix: 'mh_abcdef1234' }), 'mh_abcdef1234...');
  27. assert.equal(formatApiTokenPrefix({}), '-');
  28. });
  29. test('builds API usage examples with endpoint, bearer token, and message body', () => {
  30. const examples = buildApiUsageExamples({
  31. endpoint: 'https://mailhub.example.com/api/send',
  32. token: 'mh_example_token',
  33. from: 'noreply@example.com',
  34. to: 'user@example.com'
  35. });
  36. assert.match(examples.curl, /Authorization: Bearer mh_example_token/);
  37. assert.match(examples.curl, /https:\/\/mailhub\.example\.com\/api\/send/);
  38. assert.match(examples.nodeFetch, /fetch\('https:\/\/mailhub\.example\.com\/api\/send'/);
  39. assert.match(examples.nodeFetch, /from: 'noreply@example.com'/);
  40. assert.match(examples.requestBody, /"to": "user@example.com"/);
  41. assert.match(examples.successResponse, /"queued": true/);
  42. });
  43. test('builds persistent and temporary mailbox API examples', () => {
  44. const examples = buildMailboxApiUsageExamples({
  45. endpoint: 'https://mailhub.example.com/api/mailboxes',
  46. token: 'mh_mailbox_token',
  47. domain: 'example.com'
  48. });
  49. assert.match(examples.permanentCurl, /"mode": "permanent"/);
  50. assert.match(examples.temporaryCurl, /Authorization: Bearer mh_mailbox_token/);
  51. assert.match(examples.temporary, /"expiresInMinutes": 60/);
  52. assert.match(examples.successResponse, /"temporary": true/);
  53. });
  54. test('builds inbound message list detail and folder API examples', () => {
  55. const examples = buildInboundMessageApiUsageExamples({
  56. endpoint: 'https://mailhub.example.com/api/inbound-messages',
  57. mailboxEndpoint: 'https://mailhub.example.com/api/inbound-mailboxes',
  58. token: 'mh_messages_token',
  59. mailboxId: 42,
  60. messageId: 108
  61. });
  62. assert.match(examples.listCurl, /page=1&pageSize=20&mailboxId=42&folder=INBOX&read=false&q=invoice/);
  63. assert.match(examples.detailCurl, /api\/inbound-messages\/108/);
  64. assert.match(examples.foldersCurl, /api\/inbound-mailboxes\/42\/folders/);
  65. assert.match(examples.listCurl, /Authorization: Bearer mh_messages_token/);
  66. assert.match(examples.listResponse, /"total": 1/);
  67. assert.match(examples.detailResponse, /"textBody": "Invoice details"/);
  68. assert.equal(JSON.parse(examples.foldersResponse).folders[1].specialUse, '\\Sent');
  69. });