Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return otplease fn results #4629

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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