Fix Playwright OTP login and UAT security tests
This commit is contained in:
+76
-14
@@ -91,25 +91,87 @@ async function fetchOtpOnceFromImap(config, sinceDate) {
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
let lock;
|
||||
|
||||
try {
|
||||
lock = await client.getMailboxLock(config.mailbox);
|
||||
const query = { since: sinceDate };
|
||||
if (config.searchFrom) query.from = config.searchFrom;
|
||||
const mailboxList = [];
|
||||
const primaryMailbox = config.mailbox || 'INBOX';
|
||||
|
||||
const uids = await client.search(query, { uid: true });
|
||||
const recentUids = (uids || []).slice(-config.maxMessages).reverse();
|
||||
if (!recentUids.length) return '';
|
||||
mailboxList.push(primaryMailbox);
|
||||
|
||||
for await (const message of client.fetch(recentUids.join(','), { envelope: true, source: true }, { uid: true })) {
|
||||
const envelopeText = JSON.stringify(message.envelope || {});
|
||||
const sourceText = message.source ? message.source.toString('utf8') : '';
|
||||
const otp = firstOtpMatch(`${envelopeText}\n${sourceText}`);
|
||||
if (otp) return otp;
|
||||
for (const extraMailbox of ['INBOX', 'Junk Mail', 'Deleted Items']) {
|
||||
if (!mailboxList.includes(extraMailbox)) {
|
||||
mailboxList.push(extraMailbox);
|
||||
}
|
||||
}
|
||||
|
||||
for (const mailbox of mailboxList) {
|
||||
let lock;
|
||||
|
||||
try {
|
||||
lock = await client.getMailboxLock(mailbox);
|
||||
|
||||
const query = { since: sinceDate };
|
||||
if (config.searchFrom) query.from = config.searchFrom;
|
||||
|
||||
let recentUids = [];
|
||||
|
||||
try {
|
||||
const uids = await client.search(query, { uid: true });
|
||||
recentUids = (uids || []).slice(-config.maxMessages).reverse();
|
||||
} catch (_) {
|
||||
recentUids = [];
|
||||
}
|
||||
|
||||
if (recentUids.length) {
|
||||
for await (const message of client.fetch(
|
||||
recentUids.join(','),
|
||||
{ envelope: true, source: true },
|
||||
{ uid: true }
|
||||
)) {
|
||||
const envelopeText = JSON.stringify(message.envelope || {});
|
||||
const sourceText = message.source ? message.source.toString('utf8') : '';
|
||||
const otp = firstOtpMatch(`${envelopeText}\n${sourceText}`);
|
||||
if (otp) return otp;
|
||||
}
|
||||
}
|
||||
|
||||
const allMessages = [];
|
||||
|
||||
for await (const message of client.fetch(
|
||||
'1:*',
|
||||
{ envelope: true, source: true, uid: true },
|
||||
{ uid: false }
|
||||
)) {
|
||||
allMessages.push(message);
|
||||
}
|
||||
|
||||
allMessages.sort((a, b) => {
|
||||
const ad = a.envelope?.date ? new Date(a.envelope.date).getTime() : 0;
|
||||
const bd = b.envelope?.date ? new Date(b.envelope.date).getTime() : 0;
|
||||
return bd - ad;
|
||||
});
|
||||
|
||||
for (const message of allMessages.slice(0, config.maxMessages || 20)) {
|
||||
const envelopeText = JSON.stringify(message.envelope || {});
|
||||
const sourceText = message.source ? message.source.toString('utf8') : '';
|
||||
const fullText = `${envelopeText}\n${sourceText}`;
|
||||
|
||||
if (config.searchFrom && !fullText.toLowerCase().includes(String(config.searchFrom).toLowerCase())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const otp = firstOtpMatch(fullText);
|
||||
if (otp) return otp;
|
||||
}
|
||||
} catch (_) {
|
||||
// Try next mailbox.
|
||||
} finally {
|
||||
if (lock) lock.release();
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
} finally {
|
||||
if (lock) lock.release();
|
||||
try { await client.logout(); } catch (_) {}
|
||||
}
|
||||
}
|
||||
@@ -184,4 +246,4 @@ async function logout(page) {
|
||||
try { await page.goto('/logout'); await page.waitForLoadState('domcontentloaded'); } catch (_) {}
|
||||
}
|
||||
|
||||
module.exports = { login, logout, roleCredentials, roleKey, getOtp, fillFirst, clickFirst };
|
||||
module.exports = { login, logout, roleCredentials, roleKey, getOtp, fillFirst, clickFirst };
|
||||
Reference in New Issue
Block a user