File tree Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change 1
- const readUserInfo = require ( './read-user-info.js' )
2
-
3
- module . exports = otplease
4
1
async function otplease ( opts , fn ) {
5
2
try {
6
- await fn ( opts )
3
+ return await fn ( opts )
7
4
} catch ( err ) {
5
+ const readUserInfo = require ( './read-user-info.js' )
8
6
if ( err . code !== 'EOTP' && ( err . code !== 'E401' || ! / o n e - t i m e p a s s / . test ( err . body ) ) ) {
9
7
throw err
10
8
} else if ( ! process . stdin . isTTY || ! process . stdout . isTTY ) {
11
9
throw err
12
10
} else {
13
11
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 } )
15
13
}
16
14
}
17
15
}
16
+
17
+ module . exports = otplease
Original file line number Diff line number Diff line change 1
1
const t = require ( 'tap' )
2
+ const mockGlobals = require ( '../../fixtures/mock-globals' )
2
3
3
4
const readUserInfo = {
4
5
otp : async ( ) => '1234' ,
@@ -8,6 +9,27 @@ const otplease = t.mock('../../../lib/utils/otplease.js', {
8
9
'../../../lib/utils/read-user-info.js' : readUserInfo ,
9
10
} )
10
11
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
+
11
33
t . test ( 'prompts for otp for EOTP' , async ( t ) => {
12
34
const stdinTTY = process . stdin . isTTY
13
35
const stdoutTTY = process . stdout . isTTY
You can’t perform that action at this time.
0 commit comments