Skip to content

Commit

Permalink
test webauth in otplease
Browse files Browse the repository at this point in the history
  • Loading branch information
hfaulds committed Jul 13, 2022
1 parent 648cb04 commit a0fd462
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/lib/utils/otplease.js
@@ -1,12 +1,20 @@
const t = require('tap')

const { fake: mockNpm } = require('../../fixtures/mock-npm')
const mockGlobals = require('../../fixtures/mock-globals')

const readUserInfo = {
otp: async () => '1234',
}
const webAuth = async (opener) => {
opener()
return '1234'
}

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

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

t.test('returns function results on webauth success', async (t) => {
mockGlobals(t, {
'process.stdin': { isTTY: true },
'process.stdout': { isTTY: true },
})

const npm = mockNpm({ config: { browser: 'firefox' } })
const fn = ({ otp }) => {
if (otp) {
return 'success'
}
throw Object.assign(new Error('nope'), {
code: 'EOTP',
body: {
authUrl: 'https://www.example.com/auth',
doneUrl: 'https://www.example.com/done',
},
})
}

const result = await otplease(npm, {}, fn)
t.equal('success', result)
})

t.test('prompts for otp for 401', async (t) => {
const stdinTTY = process.stdin.isTTY
const stdoutTTY = process.stdout.isTTY
Expand Down

0 comments on commit a0fd462

Please sign in to comment.