Skip to content

Commit f270419

Browse files
authored
Merge pull request #18277 from mozilla/fxa-sms-testing
fix(testing): Update mail helper to print sms codes
2 parents bc62e1f + 824d399 commit f270419

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

packages/fxa-auth-server/test/mail_helper.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,46 @@
77
'use strict';
88
const MailParser = require('mailparser').MailParser;
99
const simplesmtp = require('simplesmtp');
10-
10+
const Redis = require('ioredis');
1111
const config = require('../config').default.getProperties();
1212

1313
const TEMPLATES_WITH_NO_CODE = new Set(['passwordResetEmail']);
1414

15+
// SMS polling
16+
const redis = new Redis(config.redis);
17+
const usersLastSms = {};
18+
async function printMatchingKeys(startUp = false) {
19+
const redisKeyPattern = 'recovery-phone:sms-attempt:*:*';
20+
try {
21+
const keys = await redis.keys(redisKeyPattern);
22+
23+
if (keys.length > 0) {
24+
for (const key of keys) {
25+
const keyParts = key.split(':');
26+
const uid = keyParts[2];
27+
const code = keyParts[3];
28+
29+
if (!usersLastSms[uid]) {
30+
usersLastSms[uid] = {};
31+
}
32+
33+
// Check if this code was already printed for this user
34+
if (!usersLastSms[uid][code]) {
35+
if (!startUp) {
36+
console.log('\x1B[36mRecovery Phone Otp:', code, '\x1B[39m');
37+
}
38+
usersLastSms[uid][code] = true;
39+
}
40+
}
41+
}
42+
} catch (error) {
43+
console.error('Failed to retrieve keys:', error);
44+
} finally {
45+
// 1s delay seems reasonable
46+
setTimeout(printMatchingKeys, 1000);
47+
}
48+
}
49+
1550
// SMTP half
1651

1752
const users = {};
@@ -147,14 +182,15 @@ module.exports = (printLogs) => {
147182
},
148183
]);
149184

150-
api.start().then(() => {
185+
api.start().then(async () => {
151186
console.log('mail_helper started...');
152-
187+
printMatchingKeys(true);
153188
return resolve({
154189
close() {
155190
return new Promise((resolve, reject) => {
156191
let smtpClosed = false;
157192
let apiClosed = false;
193+
redis.quit();
158194
smtp.server.end(() => {
159195
smtpClosed = true;
160196
if (apiClosed) {

0 commit comments

Comments
 (0)