forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword-authenticator-test.js
More file actions
228 lines (186 loc) · 7.02 KB
/
Copy pathpassword-authenticator-test.js
File metadata and controls
228 lines (186 loc) · 7.02 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'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.should()
const { PasswordAuthenticator } = require('../../lib/models/authenticator')
const SolidHost = require('../../lib/models/solid-host')
const AccountManager = require('../../lib/models/account-manager')
const mockUserStore = {
findUser: () => { return Promise.resolve(true) },
matchPassword: (user, password) => { return Promise.resolve(user) }
}
const host = SolidHost.from({ serverUri: 'https://localhost:8443' })
const accountManager = AccountManager.from({ host })
describe('PasswordAuthenticator', () => {
describe('fromParams()', () => {
let req = {
body: { username: 'alice', password: '12345' }
}
let options = { userStore: mockUserStore, accountManager }
it('should return a PasswordAuthenticator instance', () => {
let pwAuth = PasswordAuthenticator.fromParams(req, options)
expect(pwAuth.userStore).to.equal(mockUserStore)
expect(pwAuth.accountManager).to.equal(accountManager)
expect(pwAuth.username).to.equal('alice')
expect(pwAuth.password).to.equal('12345')
})
it('should init with undefined username and password if no body is provided', () => {
let req = {}
let pwAuth = PasswordAuthenticator.fromParams(req, {})
expect(pwAuth.username).to.be.undefined()
expect(pwAuth.password).to.be.undefined()
})
})
describe('validate()', () => {
it('should throw a 400 error if no username was provided', done => {
let options = { username: null, password: '12345' }
let pwAuth = new PasswordAuthenticator(options)
try {
pwAuth.validate()
} catch (error) {
expect(error.statusCode).to.equal(400)
expect(error.message).to.equal('Username required')
done()
}
})
it('should throw a 400 error if no password was provided', done => {
let options = { username: 'alice', password: null }
let pwAuth = new PasswordAuthenticator(options)
try {
pwAuth.validate()
} catch (error) {
expect(error.statusCode).to.equal(400)
expect(error.message).to.equal('Password required')
done()
}
})
})
describe('findValidUser()', () => {
it('should throw a 400 if no valid user is found in the user store', done => {
let options = {
username: 'alice',
password: '1234',
accountManager
}
let pwAuth = new PasswordAuthenticator(options)
pwAuth.userStore = {
findUser: () => { return Promise.resolve(false) }
}
pwAuth.findValidUser()
.catch(error => {
expect(error.statusCode).to.equal(400)
expect(error.message).to.equal('No user found for that username')
done()
})
})
it('should throw a 400 if user is found but password does not match', done => {
let options = {
username: 'alice',
password: '1234',
accountManager
}
let pwAuth = new PasswordAuthenticator(options)
pwAuth.userStore = {
findUser: () => { return Promise.resolve(true) },
matchPassword: () => { return Promise.resolve(false) }
}
pwAuth.findValidUser()
.catch(error => {
expect(error.statusCode).to.equal(400)
expect(error.message).to.equal('User found but no password match')
done()
})
})
it('should return a valid user if one is found and password matches', () => {
let webId = 'https://alice.example.com/#me'
let validUser = { username: 'alice', webId }
let options = {
username: 'alice',
password: '1234',
accountManager
}
let pwAuth = new PasswordAuthenticator(options)
pwAuth.userStore = {
findUser: () => { return Promise.resolve(validUser) },
matchPassword: (user, password) => { return Promise.resolve(user) }
}
return pwAuth.findValidUser()
.then(foundUser => {
expect(foundUser.webId).to.equal(webId)
})
})
describe('in Multi User mode', () => {
let multiuser = true
let serverUri = 'https://example.com'
let host = SolidHost.from({ serverUri })
let accountManager = AccountManager.from({ multiuser, host })
let aliceRecord = { webId: 'https://alice.example.com/profile/card#me' }
let mockUserStore = {
findUser: sinon.stub().resolves(aliceRecord),
matchPassword: (user, password) => { return Promise.resolve(user) }
}
it('should load user from store if provided with username', () => {
let options = {
username: 'alice',
password: '1234',
userStore: mockUserStore,
accountManager
}
let pwAuth = new PasswordAuthenticator(options)
let userStoreKey = 'alice.example.com/profile/card#me'
return pwAuth.findValidUser()
.then(() => {
expect(mockUserStore.findUser).to.be.calledWith(userStoreKey)
})
})
it('should load user from store if provided with WebID', () => {
let webId = 'https://alice.example.com/profile/card#me'
let options = {
username: webId,
password: '1234',
userStore: mockUserStore,
accountManager
}
let pwAuth = new PasswordAuthenticator(options)
let userStoreKey = 'alice.example.com/profile/card#me'
return pwAuth.findValidUser()
.then(() => {
expect(mockUserStore.findUser).to.be.calledWith(userStoreKey)
})
})
})
describe('in Single User mode', () => {
let multiuser = false
let serverUri = 'https://localhost:8443'
let host = SolidHost.from({ serverUri })
let accountManager = AccountManager.from({ multiuser, host })
let aliceRecord = { webId: 'https://localhost:8443/profile/card#me' }
let mockUserStore = {
findUser: sinon.stub().resolves(aliceRecord),
matchPassword: (user, password) => { return Promise.resolve(user) }
}
it('should load user from store if provided with username', () => {
let options = { username: 'admin', password: '1234', userStore: mockUserStore, accountManager }
let pwAuth = new PasswordAuthenticator(options)
let userStoreKey = 'localhost:8443/profile/card#me'
return pwAuth.findValidUser()
.then(() => {
expect(mockUserStore.findUser).to.be.calledWith(userStoreKey)
})
})
it('should load user from store if provided with WebID', () => {
let webId = 'https://localhost:8443/profile/card#me'
let options = { username: webId, password: '1234', userStore: mockUserStore, accountManager }
let pwAuth = new PasswordAuthenticator(options)
let userStoreKey = 'localhost:8443/profile/card#me'
return pwAuth.findValidUser()
.then(() => {
expect(mockUserStore.findUser).to.be.calledWith(userStoreKey)
})
})
})
})
})