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 committed Mar 29, 2022
1 parent b48a2bf commit 86e80be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/utils/otplease.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const readUserInfo = require('./read-user-info.js')

module.exports = otplease
async function otplease (opts, fn) {
let result
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
23 changes: 23 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,28 @@ 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 }
})
let ran = false
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 86e80be

Please sign in to comment.