Skip to content

Commit 53443a4

Browse files
committed
add tests for web-auth
1 parent 9241514 commit 53443a4

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/utils/web-auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async function webAuth (opener, initialUrl, doneUrl, opts) {
1010
// cancel open prompt if it's present
1111
doneEmitter.emit('abort')
1212

13-
return authResult
13+
return authResult.token
1414
})
1515

1616
await openPromise

test/lib/utils/web-auth.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const t = require('tap')
2+
3+
const webAuthCheckLogin = async () => {
4+
return { token: 'otp-token' }
5+
}
6+
7+
const webauth = t.mock('../../../lib/utils/web-auth.js', {
8+
'npm-profile': { webAuthCheckLogin },
9+
})
10+
11+
const initialUrl = 'https://example.com/auth'
12+
const doneUrl = 'https://example.com/done'
13+
const opts = {}
14+
15+
t.test('returns token on success', async (t) => {
16+
const opener = async () => {}
17+
const result = await webauth(opener, initialUrl, doneUrl, opts)
18+
t.equal(result, 'otp-token')
19+
})
20+
21+
t.test('closes opener when auth check finishes', async (t) => {
22+
const opener = (_url, emitter) => {
23+
return new Promise((resolve, reject) => {
24+
// the only way to finish this promise is to emit aboter on the emitter
25+
emitter.addListener('abort', () => {
26+
resolve()
27+
})
28+
})
29+
}
30+
const result = await webauth(opener, initialUrl, doneUrl, opts)
31+
t.equal(result, 'otp-token')
32+
})

0 commit comments

Comments
 (0)