|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const chai = require('chai') |
| 4 | +const sinon = require('sinon') |
| 5 | +const expect = chai.expect |
| 6 | +const dirtyChai = require('dirty-chai') |
| 7 | +chai.use(dirtyChai) |
| 8 | +const sinonChai = require('sinon-chai') |
| 9 | +chai.use(sinonChai) |
| 10 | +chai.should() |
| 11 | + |
| 12 | +const HttpMocks = require('node-mocks-http') |
| 13 | + |
| 14 | +const DeleteAccountConfirmRequest = require('../../lib/requests/delete-account-confirm-request') |
| 15 | +const SolidHost = require('../../lib/models/solid-host') |
| 16 | + |
| 17 | +describe('DeleteAccountConfirmRequest', () => { |
| 18 | + sinon.spy(DeleteAccountConfirmRequest.prototype, 'error') |
| 19 | + |
| 20 | + describe('constructor()', () => { |
| 21 | + it('should initialize a request instance from options', () => { |
| 22 | + let res = HttpMocks.createResponse() |
| 23 | + |
| 24 | + let accountManager = {} |
| 25 | + let userStore = {} |
| 26 | + |
| 27 | + let options = { |
| 28 | + accountManager, |
| 29 | + userStore, |
| 30 | + response: res, |
| 31 | + token: '12345' |
| 32 | + } |
| 33 | + |
| 34 | + let request = new DeleteAccountConfirmRequest(options) |
| 35 | + |
| 36 | + expect(request.response).to.equal(res) |
| 37 | + expect(request.token).to.equal(options.token) |
| 38 | + expect(request.accountManager).to.equal(accountManager) |
| 39 | + expect(request.userStore).to.equal(userStore) |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + describe('fromParams()', () => { |
| 44 | + it('should return a request instance from options', () => { |
| 45 | + let token = '12345' |
| 46 | + let accountManager = {} |
| 47 | + let userStore = {} |
| 48 | + |
| 49 | + let req = { |
| 50 | + app: { locals: { accountManager, oidc: { users: userStore } } }, |
| 51 | + query: { token } |
| 52 | + } |
| 53 | + let res = HttpMocks.createResponse() |
| 54 | + |
| 55 | + let request = DeleteAccountConfirmRequest.fromParams(req, res) |
| 56 | + |
| 57 | + expect(request.response).to.equal(res) |
| 58 | + expect(request.token).to.equal(token) |
| 59 | + expect(request.accountManager).to.equal(accountManager) |
| 60 | + expect(request.userStore).to.equal(userStore) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('get()', () => { |
| 65 | + let token = '12345' |
| 66 | + let userStore = {} |
| 67 | + let res = HttpMocks.createResponse() |
| 68 | + sinon.spy(res, 'render') |
| 69 | + |
| 70 | + it('should create an instance and render a delete account form', () => { |
| 71 | + let accountManager = { |
| 72 | + validateDeleteToken: sinon.stub().resolves(true) |
| 73 | + } |
| 74 | + let req = { |
| 75 | + app: { locals: { accountManager, oidc: { users: userStore } } }, |
| 76 | + query: { token } |
| 77 | + } |
| 78 | + |
| 79 | + return DeleteAccountConfirmRequest.get(req, res) |
| 80 | + .then(() => { |
| 81 | + expect(accountManager.validateDeleteToken) |
| 82 | + .to.have.been.called() |
| 83 | + expect(res.render).to.have.been.calledWith('account/delete-confirm', |
| 84 | + { token, validToken: true }) |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should display an error message on an invalid token', () => { |
| 89 | + let accountManager = { |
| 90 | + validateDeleteToken: sinon.stub().throws() |
| 91 | + } |
| 92 | + let req = { |
| 93 | + app: { locals: { accountManager, oidc: { users: userStore } } }, |
| 94 | + query: { token } |
| 95 | + } |
| 96 | + |
| 97 | + return DeleteAccountConfirmRequest.get(req, res) |
| 98 | + .then(() => { |
| 99 | + expect(DeleteAccountConfirmRequest.prototype.error) |
| 100 | + .to.have.been.called() |
| 101 | + }) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe('post()', () => { |
| 106 | + it('creates a request instance and invokes handlePost()', () => { |
| 107 | + sinon.spy(DeleteAccountConfirmRequest, 'handlePost') |
| 108 | + |
| 109 | + let token = '12345' |
| 110 | + let host = SolidHost.from({ serverUri: 'https://example.com' }) |
| 111 | + let alice = { |
| 112 | + webId: 'https://alice.example.com/#me' |
| 113 | + } |
| 114 | + let storedToken = { webId: alice.webId } |
| 115 | + let store = { |
| 116 | + findUser: sinon.stub().resolves(alice), |
| 117 | + updatePassword: sinon.stub() |
| 118 | + } |
| 119 | + let accountManager = { |
| 120 | + host, |
| 121 | + store, |
| 122 | + userAccountFrom: sinon.stub().resolves(alice), |
| 123 | + validateDeleteToken: sinon.stub().resolves(storedToken) |
| 124 | + } |
| 125 | + |
| 126 | + accountManager.accountExists = sinon.stub().resolves(true) |
| 127 | + accountManager.loadAccountRecoveryEmail = sinon.stub().resolves('[email protected]') |
| 128 | + |
| 129 | + // TODO: @kjetilk write in your stuff here - probably a stub |
| 130 | + |
| 131 | + let req = { |
| 132 | + app: { locals: { accountManager, oidc: { users: store } } }, |
| 133 | + body: { token } |
| 134 | + } |
| 135 | + let res = HttpMocks.createResponse() |
| 136 | + |
| 137 | + return DeleteAccountConfirmRequest.post(req, res) |
| 138 | + .then(() => { |
| 139 | + expect(DeleteAccountConfirmRequest.handlePost).to.have.been.called() |
| 140 | + }) |
| 141 | + }) |
| 142 | + }) |
| 143 | + |
| 144 | + describe('handlePost()', () => { |
| 145 | + it('should display error message if validation error encountered', () => { |
| 146 | + let token = '12345' |
| 147 | + let userStore = {} |
| 148 | + let res = HttpMocks.createResponse() |
| 149 | + let accountManager = { |
| 150 | + validateResetToken: sinon.stub().throws() |
| 151 | + } |
| 152 | + let req = { |
| 153 | + app: { locals: { accountManager, oidc: { users: userStore } } }, |
| 154 | + query: { token } |
| 155 | + } |
| 156 | + |
| 157 | + let request = DeleteAccountConfirmRequest.fromParams(req, res) |
| 158 | + |
| 159 | + return DeleteAccountConfirmRequest.handlePost(request) |
| 160 | + .then(() => { |
| 161 | + expect(DeleteAccountConfirmRequest.prototype.error) |
| 162 | + .to.have.been.called() |
| 163 | + }) |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + describe('validateToken()', () => { |
| 168 | + it('should return false if no token is present', () => { |
| 169 | + let accountManager = { |
| 170 | + validateDeleteToken: sinon.stub() |
| 171 | + } |
| 172 | + let request = new DeleteAccountConfirmRequest({ accountManager, token: null }) |
| 173 | + |
| 174 | + return request.validateToken() |
| 175 | + .then(result => { |
| 176 | + expect(result).to.be.false() |
| 177 | + expect(accountManager.validateDeleteToken).to.not.have.been.called() |
| 178 | + }) |
| 179 | + }) |
| 180 | + }) |
| 181 | + |
| 182 | + describe('error()', () => { |
| 183 | + it('should invoke renderForm() with the error', () => { |
| 184 | + let request = new DeleteAccountConfirmRequest({}) |
| 185 | + request.renderForm = sinon.stub() |
| 186 | + let error = new Error('error message') |
| 187 | + |
| 188 | + request.error(error) |
| 189 | + |
| 190 | + expect(request.renderForm).to.have.been.calledWith(error) |
| 191 | + }) |
| 192 | + }) |
| 193 | + |
| 194 | + describe('deleteAccount()', () => { |
| 195 | + // TODO: @kjetilk Write test when more is in place |
| 196 | + }) |
| 197 | + |
| 198 | + describe('renderForm()', () => { |
| 199 | + it('should set response status to error status, if error exists', () => { |
| 200 | + let token = '12345' |
| 201 | + let response = HttpMocks.createResponse() |
| 202 | + sinon.spy(response, 'render') |
| 203 | + |
| 204 | + let options = { token, response } |
| 205 | + |
| 206 | + let request = new DeleteAccountConfirmRequest(options) |
| 207 | + |
| 208 | + let error = new Error('error message') |
| 209 | + |
| 210 | + request.renderForm(error) |
| 211 | + |
| 212 | + expect(response.render).to.have.been.calledWith('account/delete-confirm', |
| 213 | + { validToken: false, token, error: 'error message' }) |
| 214 | + }) |
| 215 | + }) |
| 216 | +}) |
0 commit comments