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
41async 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' || ! / o n e - t i m e p a s s / . 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
Original file line number Diff line number Diff line change 11const t = require ( 'tap' )
2+ const mockGlobals = require ( '../../fixtures/mock-globals' )
23
34const 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+
1133t . test ( 'prompts for otp for EOTP' , async ( t ) => {
1234 const stdinTTY = process . stdin . isTTY
1335 const stdoutTTY = process . stdout . isTTY
You can’t perform that action at this time.
0 commit comments