forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword-change-request-test.js
More file actions
260 lines (218 loc) · 8 KB
/
Copy pathpassword-change-request-test.js
File metadata and controls
260 lines (218 loc) · 8 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use strict'
const chai = require('chai')
const sinon = require('sinon')
const expect = chai.expect
const dirtyChai = require('dirty-chai')
chai.use(dirtyChai)
const sinonChai = require('sinon-chai')
chai.use(sinonChai)
chai.should()
const HttpMocks = require('node-mocks-http')
const PasswordChangeRequest = require('../../lib/requests/password-change-request')
const SolidHost = require('../../lib/models/solid-host')
describe('PasswordChangeRequest', () => {
sinon.spy(PasswordChangeRequest.prototype, 'error')
describe('constructor()', () => {
it('should initialize a request instance from options', () => {
let res = HttpMocks.createResponse()
let accountManager = {}
let userStore = {}
let options = {
accountManager,
userStore,
returnToUrl: 'https://example.com/resource',
response: res,
token: '12345',
newPassword: 'swordfish'
}
let request = new PasswordChangeRequest(options)
expect(request.returnToUrl).to.equal(options.returnToUrl)
expect(request.response).to.equal(res)
expect(request.token).to.equal(options.token)
expect(request.newPassword).to.equal(options.newPassword)
expect(request.accountManager).to.equal(accountManager)
expect(request.userStore).to.equal(userStore)
})
})
describe('fromParams()', () => {
it('should return a request instance from options', () => {
let returnToUrl = 'https://example.com/resource'
let token = '12345'
let newPassword = 'swordfish'
let accountManager = {}
let userStore = {}
let req = {
app: { locals: { accountManager, oidc: { users: userStore } } },
query: { returnToUrl, token },
body: { newPassword }
}
let res = HttpMocks.createResponse()
let request = PasswordChangeRequest.fromParams(req, res)
expect(request.returnToUrl).to.equal(returnToUrl)
expect(request.response).to.equal(res)
expect(request.token).to.equal(token)
expect(request.newPassword).to.equal(newPassword)
expect(request.accountManager).to.equal(accountManager)
expect(request.userStore).to.equal(userStore)
})
})
describe('get()', () => {
let returnToUrl = 'https://example.com/resource'
let token = '12345'
let userStore = {}
let res = HttpMocks.createResponse()
sinon.spy(res, 'render')
it('should create an instance and render a change password form', () => {
let accountManager = {
validateResetToken: sinon.stub().resolves(true)
}
let req = {
app: { locals: { accountManager, oidc: { users: userStore } } },
query: { returnToUrl, token }
}
return PasswordChangeRequest.get(req, res)
.then(() => {
expect(accountManager.validateResetToken)
.to.have.been.called()
expect(res.render).to.have.been.calledWith('auth/change-password',
{ returnToUrl, token, validToken: true })
})
})
it('should display an error message on an invalid token', () => {
let accountManager = {
validateResetToken: sinon.stub().throws()
}
let req = {
app: { locals: { accountManager, oidc: { users: userStore } } },
query: { returnToUrl, token }
}
return PasswordChangeRequest.get(req, res)
.then(() => {
expect(PasswordChangeRequest.prototype.error)
.to.have.been.called()
})
})
})
describe('post()', () => {
it('creates a request instance and invokes handlePost()', () => {
sinon.spy(PasswordChangeRequest, 'handlePost')
let returnToUrl = 'https://example.com/resource'
let token = '12345'
let newPassword = 'swordfish'
let host = SolidHost.from({ serverUri: 'https://example.com' })
let alice = {
webId: 'https://alice.example.com/#me'
}
let storedToken = { webId: alice.webId }
let store = {
findUser: sinon.stub().resolves(alice),
updatePassword: sinon.stub()
}
let accountManager = {
host,
store,
userAccountFrom: sinon.stub().resolves(alice),
validateResetToken: sinon.stub().resolves(storedToken)
}
accountManager.accountExists = sinon.stub().resolves(true)
accountManager.loadAccountRecoveryEmail = sinon.stub().resolves('alice@example.com')
accountManager.sendPasswordResetEmail = sinon.stub().resolves()
let req = {
app: { locals: { accountManager, oidc: { users: store } } },
query: { returnToUrl },
body: { token, newPassword }
}
let res = HttpMocks.createResponse()
return PasswordChangeRequest.post(req, res)
.then(() => {
expect(PasswordChangeRequest.handlePost).to.have.been.called()
})
})
})
describe('handlePost()', () => {
it('should display error message if validation error encountered', () => {
let returnToUrl = 'https://example.com/resource'
let token = '12345'
let userStore = {}
let res = HttpMocks.createResponse()
let accountManager = {
validateResetToken: sinon.stub().throws()
}
let req = {
app: { locals: { accountManager, oidc: { users: userStore } } },
query: { returnToUrl, token }
}
let request = PasswordChangeRequest.fromParams(req, res)
return PasswordChangeRequest.handlePost(request)
.then(() => {
expect(PasswordChangeRequest.prototype.error)
.to.have.been.called()
})
})
})
describe('validateToken()', () => {
it('should return false if no token is present', () => {
let accountManager = {
validateResetToken: sinon.stub()
}
let request = new PasswordChangeRequest({ accountManager, token: null })
return request.validateToken()
.then(result => {
expect(result).to.be.false()
expect(accountManager.validateResetToken).to.not.have.been.called()
})
})
})
describe('validatePost()', () => {
it('should throw an error if no new password was entered', () => {
let request = new PasswordChangeRequest({ newPassword: null })
expect(() => request.validatePost()).to.throw('Please enter a new password')
})
})
describe('error()', () => {
it('should invoke renderForm() with the error', () => {
let request = new PasswordChangeRequest({})
request.renderForm = sinon.stub()
let error = new Error('error message')
request.error(error)
expect(request.renderForm).to.have.been.calledWith(error)
})
})
describe('changePassword()', () => {
it('should create a new user store entry if none exists', () => {
// this would be the case for legacy pre-user-store accounts
let webId = 'https://alice.example.com/#me'
let user = { webId, id: webId }
let accountManager = {
userAccountFrom: sinon.stub().returns(user)
}
let userStore = {
findUser: sinon.stub().resolves(null), // no user found
createUser: sinon.stub().resolves(),
updatePassword: sinon.stub().resolves()
}
let options = {
accountManager, userStore, newPassword: 'swordfish'
}
let request = new PasswordChangeRequest(options)
return request.changePassword(user)
.then(() => {
expect(userStore.createUser).to.have.been.calledWith(user, options.newPassword)
})
})
})
describe('renderForm()', () => {
it('should set response status to error status, if error exists', () => {
let returnToUrl = 'https://example.com/resource'
let token = '12345'
let response = HttpMocks.createResponse()
sinon.spy(response, 'render')
let options = { returnToUrl, token, response }
let request = new PasswordChangeRequest(options)
let error = new Error('error message')
request.renderForm(error)
expect(response.render).to.have.been.calledWith('auth/change-password',
{ validToken: false, token, returnToUrl, error: 'error message' })
})
})
})