forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtls-authenticator-test.js
More file actions
173 lines (127 loc) · 5.2 KB
/
Copy pathtls-authenticator-test.js
File metadata and controls
173 lines (127 loc) · 5.2 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict'
const chai = require('chai')
const expect = chai.expect
const sinon = require('sinon')
chai.use(require('sinon-chai'))
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
chai.should()
const { TlsAuthenticator } = require('../../lib/models/authenticator')
const SolidHost = require('../../lib/models/solid-host')
const AccountManager = require('../../lib/models/account-manager')
const host = SolidHost.from({ serverUri: 'https://example.com' })
const accountManager = AccountManager.from({ host, multiuser: true })
describe('TlsAuthenticator', () => {
describe('fromParams()', () => {
const req = {
connection: {}
}
const options = { accountManager }
it('should return a TlsAuthenticator instance', () => {
const tlsAuth = TlsAuthenticator.fromParams(req, options)
expect(tlsAuth.accountManager).to.equal(accountManager)
expect(tlsAuth.connection).to.equal(req.connection)
})
})
describe('findValidUser()', () => {
const webId = 'https://alice.example.com/#me'
const certificate = { uri: webId }
const connection = {
renegotiate: sinon.stub().yields(),
getPeerCertificate: sinon.stub().returns(certificate)
}
const options = { accountManager, connection }
const tlsAuth = new TlsAuthenticator(options)
tlsAuth.extractWebId = sinon.stub().resolves(webId)
sinon.spy(tlsAuth, 'renegotiateTls')
sinon.spy(tlsAuth, 'loadUser')
return tlsAuth.findValidUser()
.then(validUser => {
expect(tlsAuth.renegotiateTls).to.have.been.called()
expect(connection.getPeerCertificate).to.have.been.called()
expect(tlsAuth.extractWebId).to.have.been.calledWith(certificate)
expect(tlsAuth.loadUser).to.have.been.calledWith(webId)
expect(validUser.webId).to.equal(webId)
})
})
describe('renegotiateTls()', () => {
it('should reject if an error occurs while renegotiating', () => {
const connection = {
renegotiate: sinon.stub().yields(new Error('Error renegotiating'))
}
const tlsAuth = new TlsAuthenticator({ connection })
expect(tlsAuth.renegotiateTls()).to.be.rejectedWith(/Error renegotiating/)
})
it('should resolve if no error occurs', () => {
const connection = {
renegotiate: sinon.stub().yields(null)
}
const tlsAuth = new TlsAuthenticator({ connection })
expect(tlsAuth.renegotiateTls()).to.be.fulfilled()
})
})
describe('getCertificate()', () => {
it('should throw on a non-existent certificate', () => {
const connection = {
getPeerCertificate: sinon.stub().returns(null)
}
const tlsAuth = new TlsAuthenticator({ connection })
expect(() => tlsAuth.getCertificate()).to.throw(/No client certificate detected/)
})
it('should throw on an empty certificate', () => {
const connection = {
getPeerCertificate: sinon.stub().returns({})
}
const tlsAuth = new TlsAuthenticator({ connection })
expect(() => tlsAuth.getCertificate()).to.throw(/No client certificate detected/)
})
it('should return a certificate if no error occurs', () => {
const certificate = { uri: 'https://alice.example.com/#me' }
const connection = {
getPeerCertificate: sinon.stub().returns(certificate)
}
const tlsAuth = new TlsAuthenticator({ connection })
expect(tlsAuth.getCertificate()).to.equal(certificate)
})
})
describe('extractWebId()', () => {
it('should reject if an error occurs verifying certificate', () => {
const tlsAuth = new TlsAuthenticator({})
tlsAuth.verifyWebId = sinon.stub().yields(new Error('Error processing certificate'))
expect(tlsAuth.extractWebId()).to.be.rejectedWith(/Error processing certificate/)
})
it('should resolve with a verified web id', () => {
const tlsAuth = new TlsAuthenticator({})
const webId = 'https://alice.example.com/#me'
tlsAuth.verifyWebId = sinon.stub().yields(null, webId)
const certificate = { uri: webId }
expect(tlsAuth.extractWebId(certificate)).to.become(webId)
})
})
describe('loadUser()', () => {
it('should return a user instance if the webid is local', () => {
const tlsAuth = new TlsAuthenticator({ accountManager })
const webId = 'https://alice.example.com/#me'
const user = tlsAuth.loadUser(webId)
expect(user.username).to.equal('alice')
expect(user.webId).to.equal(webId)
})
it('should return a user instance if external user and this server is authorized provider', () => {
const tlsAuth = new TlsAuthenticator({ accountManager })
const externalWebId = 'https://alice.someothersite.com#me'
tlsAuth.discoverProviderFor = sinon.stub().resolves('https://example.com')
const user = tlsAuth.loadUser(externalWebId)
expect(user.username).to.equal(externalWebId)
expect(user.webId).to.equal(externalWebId)
})
})
describe('verifyWebId()', () => {
it('should yield an error if no cert is given', done => {
const tlsAuth = new TlsAuthenticator({})
tlsAuth.verifyWebId(null, (error) => {
expect(error.message).to.equal('No certificate given')
done()
})
})
})
})