-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-user-info.js
More file actions
152 lines (133 loc) · 3.65 KB
/
Copy pathread-user-info.js
File metadata and controls
152 lines (133 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const t = require('tap')
const procLog = require('proc-log')
const tmock = require('../../fixtures/tmock')
let readOpts = null
let readResult = null
let logMsg = null
const readUserInfo = tmock(t, '{LIB}/utils/read-user-info.js', {
read: {
read: async (opts) => {
readOpts = opts
return readResult
},
},
'proc-log': {
...procLog,
log: {
...procLog.log,
warn: (msg) => logMsg = msg,
},
input: {
...procLog.input,
read: (fn) => fn(),
},
},
'npm-user-validate': {
username: (username) => {
if (username === 'invalid') {
return new Error('invalid username')
}
return null
},
email: (email) => {
if (email.startsWith('invalid')) {
return new Error('invalid email')
}
return null
},
},
})
t.beforeEach(() => {
logMsg = null
})
t.test('otp', async (t) => {
readResult = '1234'
t.teardown(() => {
readResult = null
readOpts = null
})
const result = await readUserInfo.otp()
t.equal(result, '1234', 'received the otp')
})
t.test('password', async (t) => {
readResult = 'password'
t.teardown(() => {
readResult = null
readOpts = null
})
const result = await readUserInfo.password()
t.equal(result, 'password', 'received the password')
t.match(readOpts, {
silent: true,
}, 'got the correct options')
})
t.test('username', async (t) => {
readResult = 'username'
t.teardown(() => {
readResult = null
readOpts = null
})
const result = await readUserInfo.username()
t.equal(result, 'username', 'received the username')
})
t.test('username - invalid warns and retries', async (t) => {
readResult = 'invalid'
t.teardown(() => {
readResult = null
readOpts = null
})
const pResult = readUserInfo.username(null, null)
// have to swap it to a valid username after execution starts
// or it will loop forever
readResult = 'valid'
const result = await pResult
t.equal(result, 'valid', 'received the username')
t.equal(logMsg, 'invalid username')
})
t.test('email', async (t) => {
readResult = 'foo@bar.baz'
t.teardown(() => {
readResult = null
readOpts = null
})
const result = await readUserInfo.email()
t.equal(result, 'foo@bar.baz', 'received the email')
})
t.test('email - invalid warns and retries', async (t) => {
readResult = 'invalid@bar.baz'
t.teardown(() => {
readResult = null
readOpts = null
})
const pResult = readUserInfo.email(null, null)
readResult = 'foo@bar.baz'
const result = await pResult
t.equal(result, 'foo@bar.baz', 'received the email')
t.equal(logMsg, 'invalid email')
})
t.test('read-user-info integration works', async (t) => {
t.teardown(() => {
readResult = null
readOpts = null
})
readResult = 'regular-input'
const username = await readUserInfo.username('Username: ')
t.equal(username, 'regular-input', 'should return username from regular prompt')
t.notOk(readOpts.silent, 'username prompt should not set silent')
readResult = 'secret-password'
const password = await readUserInfo.password('Password: ')
t.equal(password, 'secret-password', 'should return password from silent prompt')
t.match(readOpts, { silent: true }, 'password prompt should set silent: true')
})
t.test('silent metadata is passed correctly by read-user-info', async (t) => {
t.teardown(() => {
readResult = null
readOpts = null
})
readResult = 'username'
await readUserInfo.username('Username: ')
t.notOk(readOpts?.silent, 'username prompt should not set silent')
readResult = 'password'
await readUserInfo.password('Password: ')
t.equal(readOpts?.silent, true, 'password prompt should set silent: true')
})