Skip to content

Commit

Permalink
add tests for web-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
hfaulds committed Jul 13, 2022
1 parent 2a4c690 commit 38cef3a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/utils/web-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function webAuth (opener, initialUrl, doneUrl, opts) {
// cancel open prompt if it's present
doneEmitter.emit('abort')

return authResult
return authResult.token
})

await openPromise
Expand Down
32 changes: 32 additions & 0 deletions test/lib/utils/web-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const t = require('tap')

const webAuthCheckLogin = async () => {
return { token: 'otp-token' }
}

const webauth = t.mock('../../../lib/utils/web-auth.js', {
'npm-profile': { webAuthCheckLogin },
})

const initialUrl = 'https://example.com/auth'
const doneUrl = 'https://example.com/done'
const opts = {}

t.test('returns token on success', async (t) => {
const opener = async () => {}
const result = await webauth(opener, initialUrl, doneUrl, opts)
t.equal(result, 'otp-token')
})

t.test('closes opener when auth check finishes', async (t) => {
const opener = (_url, emitter) => {
return new Promise((resolve, reject) => {
// the only way to finish this promise is to emit aboter on the emitter
emitter.addListener('abort', () => {
resolve()
})
})
}
const result = await webauth(opener, initialUrl, doneUrl, opts)
t.equal(result, 'otp-token')
})

0 comments on commit 38cef3a

Please sign in to comment.