verification-flow-polling.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('background/verification-flow.js', 'utf8');
  5. const globalScope = {};
  6. const api = new Function('self', `${source}; return self.MultiPageBackgroundVerificationFlow;`)(globalScope);
  7. test('verification flow extends 2925 polling window', () => {
  8. const helpers = api.createVerificationFlowHelpers({
  9. addLog: async () => {},
  10. chrome: { tabs: { update: async () => {} } },
  11. CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email',
  12. completeStepFromBackground: async () => {},
  13. confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }),
  14. getHotmailVerificationPollConfig: () => ({}),
  15. getHotmailVerificationRequestTimestamp: () => 0,
  16. getState: async () => ({}),
  17. getTabId: async () => 1,
  18. HOTMAIL_PROVIDER: 'hotmail-api',
  19. isStopError: () => false,
  20. LUCKMAIL_PROVIDER: 'luckmail-api',
  21. MAIL_2925_VERIFICATION_INTERVAL_MS: 15000,
  22. MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15,
  23. pollCloudflareTempEmailVerificationCode: async () => ({}),
  24. pollHotmailVerificationCode: async () => ({}),
  25. pollLuckmailVerificationCode: async () => ({}),
  26. sendToContentScript: async () => ({}),
  27. sendToMailContentScriptResilient: async () => ({}),
  28. setState: async () => {},
  29. setStepStatus: async () => {},
  30. sleepWithStop: async () => {},
  31. throwIfStopped: () => {},
  32. VERIFICATION_POLL_MAX_ROUNDS: 5,
  33. });
  34. const step4Payload = helpers.getVerificationPollPayload(4, { email: 'user@example.com', mailProvider: '2925' });
  35. const step7Payload = helpers.getVerificationPollPayload(7, { email: 'user@example.com', mailProvider: '2925' });
  36. assert.equal(step4Payload.maxAttempts, 15);
  37. assert.equal(step4Payload.intervalMs, 15000);
  38. assert.equal(step7Payload.maxAttempts, 15);
  39. assert.equal(step7Payload.intervalMs, 15000);
  40. });
  41. test('verification flow runs beforeSubmit hook before filling the code', async () => {
  42. const events = [];
  43. const helpers = api.createVerificationFlowHelpers({
  44. addLog: async () => {},
  45. chrome: {
  46. tabs: {
  47. update: async () => {},
  48. },
  49. },
  50. CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email',
  51. completeStepFromBackground: async (_step, payload) => {
  52. events.push(['complete', payload.code]);
  53. },
  54. confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }),
  55. getHotmailVerificationPollConfig: () => ({}),
  56. getHotmailVerificationRequestTimestamp: () => 0,
  57. getState: async () => ({}),
  58. getTabId: async () => 1,
  59. HOTMAIL_PROVIDER: 'hotmail-api',
  60. isStopError: () => false,
  61. LUCKMAIL_PROVIDER: 'luckmail-api',
  62. MAIL_2925_VERIFICATION_INTERVAL_MS: 15000,
  63. MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15,
  64. pollCloudflareTempEmailVerificationCode: async () => ({}),
  65. pollHotmailVerificationCode: async () => ({}),
  66. pollLuckmailVerificationCode: async () => ({}),
  67. sendToContentScript: async (_source, message) => {
  68. if (message.type === 'FILL_CODE') {
  69. events.push(['submit', message.payload.code]);
  70. return {};
  71. }
  72. return {};
  73. },
  74. sendToMailContentScriptResilient: async () => ({
  75. code: '654321',
  76. emailTimestamp: 123,
  77. }),
  78. setState: async (payload) => {
  79. events.push(['state', payload.lastLoginCode || payload.lastSignupCode]);
  80. },
  81. setStepStatus: async () => {},
  82. sleepWithStop: async () => {},
  83. throwIfStopped: () => {},
  84. VERIFICATION_POLL_MAX_ROUNDS: 5,
  85. });
  86. await helpers.resolveVerificationStep(
  87. 7,
  88. { email: 'user@example.com', lastLoginCode: null },
  89. { provider: 'qq', label: 'QQ 邮箱' },
  90. {
  91. beforeSubmit: async (result) => {
  92. events.push(['beforeSubmit', result.code]);
  93. },
  94. }
  95. );
  96. assert.deepStrictEqual(events, [
  97. ['beforeSubmit', '654321'],
  98. ['submit', '654321'],
  99. ['state', '654321'],
  100. ['complete', '654321'],
  101. ]);
  102. });