forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-account-request-test.js
More file actions
181 lines (145 loc) · 5.99 KB
/
Copy pathdelete-account-request-test.js
File metadata and controls
181 lines (145 loc) · 5.99 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
'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 DeleteAccountRequest = require('../../lib/requests/delete-account-request')
const AccountManager = require('../../lib/models/account-manager')
const SolidHost = require('../../lib/models/solid-host')
describe('DeleteAccountRequest', () => {
describe('constructor()', () => {
it('should initialize a request instance from options', () => {
const res = HttpMocks.createResponse()
const options = {
response: res,
username: 'alice'
}
const request = new DeleteAccountRequest(options)
expect(request.response).to.equal(res)
expect(request.username).to.equal(options.username)
})
})
describe('fromParams()', () => {
it('should return a request instance from options', () => {
const username = 'alice'
const accountManager = {}
const req = {
app: { locals: { accountManager } },
body: { username }
}
const res = HttpMocks.createResponse()
const request = DeleteAccountRequest.fromParams(req, res)
expect(request.accountManager).to.equal(accountManager)
expect(request.username).to.equal(username)
expect(request.response).to.equal(res)
})
})
describe('get()', () => {
it('should create an instance and render a delete account form', () => {
const username = 'alice'
const accountManager = { multiuser: true }
const req = {
app: { locals: { accountManager } },
body: { username }
}
const res = HttpMocks.createResponse()
res.render = sinon.stub()
DeleteAccountRequest.get(req, res)
expect(res.render).to.have.been.calledWith('account/delete',
{ error: undefined, multiuser: true })
})
})
describe('post()', () => {
it('creates a request instance and invokes handlePost()', () => {
sinon.spy(DeleteAccountRequest, 'handlePost')
const username = 'alice'
const host = SolidHost.from({ serverUri: 'https://example.com' })
const store = {
suffixAcl: '.acl'
}
const accountManager = AccountManager.from({ host, multiuser: true, store })
accountManager.accountExists = sinon.stub().resolves(true)
accountManager.loadAccountRecoveryEmail = sinon.stub().resolves('alice@example.com')
accountManager.sendDeleteLink = sinon.stub().resolves()
const req = {
app: { locals: { accountManager } },
body: { username }
}
const res = HttpMocks.createResponse()
DeleteAccountRequest.post(req, res)
.then(() => {
expect(DeleteAccountRequest.handlePost).to.have.been.called()
})
})
})
describe('validate()', () => {
it('should throw an error if username is missing in multi-user mode', () => {
const host = SolidHost.from({ serverUri: 'https://example.com' })
const accountManager = AccountManager.from({ host, multiuser: true })
const request = new DeleteAccountRequest({ accountManager })
expect(() => request.validate()).to.throw(/Username required/)
})
it('should not throw an error if username is missing in single user mode', () => {
const host = SolidHost.from({ serverUri: 'https://example.com' })
const accountManager = AccountManager.from({ host, multiuser: false })
const request = new DeleteAccountRequest({ accountManager })
expect(() => request.validate()).to.not.throw()
})
})
describe('handlePost()', () => {
it('should handle the post request', () => {
const host = SolidHost.from({ serverUri: 'https://example.com' })
const store = { suffixAcl: '.acl' }
const accountManager = AccountManager.from({ host, multiuser: true, store })
accountManager.loadAccountRecoveryEmail = sinon.stub().resolves('alice@example.com')
accountManager.sendDeleteAccountEmail = sinon.stub().resolves()
accountManager.accountExists = sinon.stub().resolves(true)
const username = 'alice'
const response = HttpMocks.createResponse()
response.render = sinon.stub()
const options = { accountManager, username, response }
const request = new DeleteAccountRequest(options)
sinon.spy(request, 'error')
return DeleteAccountRequest.handlePost(request)
.then(() => {
expect(accountManager.loadAccountRecoveryEmail).to.have.been.called()
expect(response.render).to.have.been.calledWith('account/delete-link-sent')
expect(request.error).to.not.have.been.called()
})
})
})
describe('loadUser()', () => {
it('should return a UserAccount instance based on username', () => {
const host = SolidHost.from({ serverUri: 'https://example.com' })
const store = { suffixAcl: '.acl' }
const accountManager = AccountManager.from({ host, multiuser: true, store })
accountManager.accountExists = sinon.stub().resolves(true)
const username = 'alice'
const options = { accountManager, username }
const request = new DeleteAccountRequest(options)
return request.loadUser()
.then(account => {
expect(account.webId).to.equal('https://alice.example.com/profile/card#me')
})
})
it('should throw an error if the user does not exist', done => {
const host = SolidHost.from({ serverUri: 'https://example.com' })
const store = { suffixAcl: '.acl' }
const accountManager = AccountManager.from({ host, multiuser: true, store })
accountManager.accountExists = sinon.stub().resolves(false)
const username = 'alice'
const options = { accountManager, username }
const request = new DeleteAccountRequest(options)
request.loadUser()
.catch(error => {
expect(error.message).to.equal('Account not found for that username')
done()
})
})
})
})