Skip to content

Commit

Permalink
fix: return otplease fn results
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored and lukekarrys committed Mar 29, 2022
1 parent aac01b8 commit 8b65bfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/utils/otplease.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const readUserInfo = require('./read-user-info.js')

module.exports = otplease
async function otplease (opts, fn) {
try {
await fn(opts)
return await fn(opts)
} catch (err) {
const readUserInfo = require('./read-user-info.js')
if (err.code !== 'EOTP' && (err.code !== 'E401' || !/one-time pass/.test(err.body))) {
throw err
} else if (!process.stdin.isTTY || !process.stdout.isTTY) {
throw err
} else {
const otp = await readUserInfo.otp('This operation requires a one-time password.\nEnter OTP:')
return fn({ ...opts, otp })
return await fn({ ...opts, otp })
}
}
}

module.exports = otplease
22 changes: 22 additions & 0 deletions test/lib/utils/otplease.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const t = require('tap')
const mockGlobals = require('../../fixtures/mock-globals')

const readUserInfo = {
otp: async () => '1234',
Expand All @@ -8,6 +9,27 @@ const otplease = t.mock('../../../lib/utils/otplease.js', {
'../../../lib/utils/read-user-info.js': readUserInfo,
})

t.test('returns function results on success', async (t) => {
const fn = () => 'test string'
const result = await otplease({}, fn)
t.equal('test string', result)
})

t.test('returns function results on otp success', async (t) => {
mockGlobals(t, {
'process.stdin': { isTTY: true },
'process.stdout': { isTTY: true },
})
const fn = ({ otp }) => {
if (otp) {
return 'success'
}
throw Object.assign(new Error('nope'), { code: 'EOTP' })
}
const result = await otplease({}, fn)
t.equal('success', result)
})

t.test('prompts for otp for EOTP', async (t) => {
const stdinTTY = process.stdin.isTTY
const stdoutTTY = process.stdout.isTTY
Expand Down

0 comments on commit 8b65bfd

Please sign in to comment.