Skip to content

Commit 8b65bfd

Browse files
wraithgarlukekarrys
authored andcommitted
fix: return otplease fn results
1 parent aac01b8 commit 8b65bfd

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

lib/utils/otplease.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const readUserInfo = require('./read-user-info.js')
2-
3-
module.exports = otplease
41
async function otplease (opts, fn) {
52
try {
6-
await fn(opts)
3+
return await fn(opts)
74
} catch (err) {
5+
const readUserInfo = require('./read-user-info.js')
86
if (err.code !== 'EOTP' && (err.code !== 'E401' || !/one-time pass/.test(err.body))) {
97
throw err
108
} else if (!process.stdin.isTTY || !process.stdout.isTTY) {
119
throw err
1210
} else {
1311
const otp = await readUserInfo.otp('This operation requires a one-time password.\nEnter OTP:')
14-
return fn({ ...opts, otp })
12+
return await fn({ ...opts, otp })
1513
}
1614
}
1715
}
16+
17+
module.exports = otplease

test/lib/utils/otplease.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const t = require('tap')
2+
const mockGlobals = require('../../fixtures/mock-globals')
23

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

12+
t.test('returns function results on success', async (t) => {
13+
const fn = () => 'test string'
14+
const result = await otplease({}, fn)
15+
t.equal('test string', result)
16+
})
17+
18+
t.test('returns function results on otp success', async (t) => {
19+
mockGlobals(t, {
20+
'process.stdin': { isTTY: true },
21+
'process.stdout': { isTTY: true },
22+
})
23+
const fn = ({ otp }) => {
24+
if (otp) {
25+
return 'success'
26+
}
27+
throw Object.assign(new Error('nope'), { code: 'EOTP' })
28+
}
29+
const result = await otplease({}, fn)
30+
t.equal('success', result)
31+
})
32+
1133
t.test('prompts for otp for EOTP', async (t) => {
1234
const stdinTTY = process.stdin.isTTY
1335
const stdoutTTY = process.stdout.isTTY

0 commit comments

Comments
 (0)