Skip to content

Commit a0fd462

Browse files
committed
test webauth in otplease
1 parent 648cb04 commit a0fd462

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/lib/utils/otplease.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
const t = require('tap')
2+
3+
const { fake: mockNpm } = require('../../fixtures/mock-npm')
24
const mockGlobals = require('../../fixtures/mock-globals')
35

46
const readUserInfo = {
57
otp: async () => '1234',
68
}
9+
const webAuth = async (opener) => {
10+
opener()
11+
return '1234'
12+
}
713

814
const otplease = t.mock('../../../lib/utils/otplease.js', {
915
'../../../lib/utils/read-user-info.js': readUserInfo,
16+
'../../../lib/utils/open-url-prompt.js': () => {},
17+
'../../../lib/utils/web-auth': webAuth,
1018
})
1119

1220
t.test('returns function results on success', async (t) => {
@@ -54,6 +62,30 @@ t.test('prompts for otp for EOTP', async (t) => {
5462
await otplease(null, { some: 'prop' }, fn)
5563
})
5664

65+
t.test('returns function results on webauth success', async (t) => {
66+
mockGlobals(t, {
67+
'process.stdin': { isTTY: true },
68+
'process.stdout': { isTTY: true },
69+
})
70+
71+
const npm = mockNpm({ config: { browser: 'firefox' } })
72+
const fn = ({ otp }) => {
73+
if (otp) {
74+
return 'success'
75+
}
76+
throw Object.assign(new Error('nope'), {
77+
code: 'EOTP',
78+
body: {
79+
authUrl: 'https://www.example.com/auth',
80+
doneUrl: 'https://www.example.com/done',
81+
},
82+
})
83+
}
84+
85+
const result = await otplease(npm, {}, fn)
86+
t.equal('success', result)
87+
})
88+
5789
t.test('prompts for otp for 401', async (t) => {
5890
const stdinTTY = process.stdin.isTTY
5991
const stdoutTTY = process.stdout.isTTY

0 commit comments

Comments
 (0)