1const t = require('tap') 2const tmock = require('../../fixtures/tmock') 3 4let readOpts = null 5let readResult = null 6const read = async (opts) => { 7 readOpts = opts 8 return readResult 9} 10 11const npmUserValidate = { 12 username: (username) => { 13 if (username === 'invalid') { 14 return new Error('invalid username') 15 } 16 17 return null 18 }, 19 email: (email) => { 20 if (email.startsWith('invalid')) { 21 return new Error('invalid email') 22 } 23 24 return null 25 }, 26} 27 28let logMsg = null 29const readUserInfo = tmock(t, '{LIB}/utils/read-user-info.js', { 30 read, 31 npmlog: { 32 clearProgress: () => {}, 33 showProgress: () => {}, 34 }, 35 'proc-log': { 36 warn: (msg) => logMsg = msg, 37 }, 38 'npm-user-validate': npmUserValidate, 39}) 40 41t.beforeEach(() => { 42 logMsg = null 43}) 44 45t.test('otp', async (t) => { 46 readResult = '1234' 47 t.teardown(() => { 48 readResult = null 49 readOpts = null 50 }) 51 const result = await readUserInfo.otp() 52 t.equal(result, '1234', 'received the otp') 53}) 54 55t.test('password', async (t) => { 56 readResult = 'password' 57 t.teardown(() => { 58 readResult = null 59 readOpts = null 60 }) 61 const result = await readUserInfo.password() 62 t.equal(result, 'password', 'received the password') 63 t.match(readOpts, { 64 silent: true, 65 }, 'got the correct options') 66}) 67 68t.test('username', async (t) => { 69 readResult = 'username' 70 t.teardown(() => { 71 readResult = null 72 readOpts = null 73 }) 74 const result = await readUserInfo.username() 75 t.equal(result, 'username', 'received the username') 76}) 77 78t.test('username - invalid warns and retries', async (t) => { 79 readResult = 'invalid' 80 t.teardown(() => { 81 readResult = null 82 readOpts = null 83 }) 84 85 const pResult = readUserInfo.username(null, null) 86 // have to swap it to a valid username after execution starts 87 // or it will loop forever 88 readResult = 'valid' 89 const result = await pResult 90 t.equal(result, 'valid', 'received the username') 91 t.equal(logMsg, 'invalid username') 92}) 93 94t.test('email', async (t) => { 95 readResult = 'foo@bar.baz' 96 t.teardown(() => { 97 readResult = null 98 readOpts = null 99 }) 100 const result = await readUserInfo.email() 101 t.equal(result, 'foo@bar.baz', 'received the email') 102}) 103 104t.test('email - invalid warns and retries', async (t) => { 105 readResult = 'invalid@bar.baz' 106 t.teardown(() => { 107 readResult = null 108 readOpts = null 109 }) 110 111 const pResult = readUserInfo.email(null, null) 112 readResult = 'foo@bar.baz' 113 const result = await pResult 114 t.equal(result, 'foo@bar.baz', 'received the email') 115 t.equal(logMsg, 'invalid email') 116}) 117