-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathwebauthn_test.js
76 lines (64 loc) · 2.54 KB
/
webauthn_test.js
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
let assert = require('assert')
let sinon = require('sinon')
let express = require('express')
let Webauthn = require('../src/Webauthn')
let MemoryAdapter = require('../src/MemoryAdapter')
let Dictionaries = require('../src/Dictionaries')
describe('Webauthn', () => {
afterEach(() => {
sinon.restore()
})
describe('constructor()', () => {
let validConfig
beforeEach(() => {
validConfig = {
origin: 'http://localhost:3000',
usernameField: 'name',
userFields: ['name', 'displayName'],
store: new MemoryAdapter(),
rpName: 'ACME Corporation',
credentialEndpoint: '/register',
assertionEndpoint: '/login',
challengeEndpoint: '/response',
logoutEndpoint: '/logout',
enableLogging: true,
attestation: Dictionaries.AttestationConveyancePreference.NONE,
}
})
it('rejects an invalid attestation', () => {
assert.throws(() => new Webauthn({...validConfig, ...{attestation: 'invalid'}}),
'Invalid attestation value invalid. Must be one of "none", "direct", "indirect"')
})
it('maps user fields when using arrays', () => {
let webauthn = new Webauthn({...validConfig, ...{userFields: ['one', 'two']}})
assert.deepEqual({one: 'one', two: 'two'}, webauthn.config.userFields)
})
it('maps user fields when using objects', () => {
let webauthn = new Webauthn({...validConfig, ...{userFields: {'one': 1, 'two' : 2}}})
assert.deepEqual({one: 1, two: 2}, webauthn.config.userFields)
})
})
describe('initialize()', () => {
it('sets up the endpoints and returns the router', () => {
let webauthn = new Webauthn({
credentialEndpoint: 'credentialEndpoint',
assertionEndpoint: 'assertionEndpoint',
challengeEndpoint: 'challengeEndpoint',
logoutEndpoint: 'logoutEndpoint',
})
let router = {
post: (endpoint, handler) => {},
get: (endpoint, handler) => {},
}
let routerMock = sinon.mock(router)
routerMock.expects('post').withArgs('assertionEndpoint', sinon.match.func)
routerMock.expects('post').withArgs('credentialEndpoint', sinon.match.func)
routerMock.expects('post').withArgs('challengeEndpoint', sinon.match.func)
routerMock.expects('post').withArgs('logoutEndpoint', sinon.match.func)
routerMock.expects('get').withArgs('logoutEndpoint', sinon.match.func)
sinon.stub(express, 'Router').returns(router)
assert.equal(router, webauthn.initialize())
routerMock.verify()
})
})
})