|
| 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 DeleteAccountRequest = require('../../lib/requests/delete-account-request') |
| 15 | +const AccountManager = require('../../lib/models/account-manager') |
| 16 | +const SolidHost = require('../../lib/models/solid-host') |
| 17 | + |
| 18 | +describe('DeleteAccountRequest', () => { |
| 19 | + describe('constructor()', () => { |
| 20 | + it('should initialize a request instance from options', () => { |
| 21 | + let res = HttpMocks.createResponse() |
| 22 | + |
| 23 | + let options = { |
| 24 | + response: res, |
| 25 | + username: 'alice' |
| 26 | + } |
| 27 | + |
| 28 | + let request = new DeleteAccountRequest(options) |
| 29 | + |
| 30 | + expect(request.response).to.equal(res) |
| 31 | + expect(request.username).to.equal(options.username) |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe('fromParams()', () => { |
| 36 | + it('should return a request instance from options', () => { |
| 37 | + let username = 'alice' |
| 38 | + let accountManager = {} |
| 39 | + |
| 40 | + let req = { |
| 41 | + app: { locals: { accountManager } }, |
| 42 | + body: { username } |
| 43 | + } |
| 44 | + let res = HttpMocks.createResponse() |
| 45 | + |
| 46 | + let request = DeleteAccountRequest.fromParams(req, res) |
| 47 | + |
| 48 | + expect(request.accountManager).to.equal(accountManager) |
| 49 | + expect(request.username).to.equal(username) |
| 50 | + expect(request.response).to.equal(res) |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('get()', () => { |
| 55 | + it('should create an instance and render a delete account form', () => { |
| 56 | + let username = 'alice' |
| 57 | + let accountManager = { multiuser: true } |
| 58 | + |
| 59 | + let req = { |
| 60 | + app: { locals: { accountManager } }, |
| 61 | + body: { username } |
| 62 | + } |
| 63 | + let res = HttpMocks.createResponse() |
| 64 | + res.render = sinon.stub() |
| 65 | + |
| 66 | + DeleteAccountRequest.get(req, res) |
| 67 | + |
| 68 | + expect(res.render).to.have.been.calledWith('account/delete', |
| 69 | + { error: undefined, multiuser: true }) |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe('post()', () => { |
| 74 | + it('creates a request instance and invokes handlePost()', () => { |
| 75 | + sinon.spy(DeleteAccountRequest, 'handlePost') |
| 76 | + |
| 77 | + let username = 'alice' |
| 78 | + let host = SolidHost.from({ serverUri: 'https://example.com' }) |
| 79 | + let store = { |
| 80 | + suffixAcl: '.acl' |
| 81 | + } |
| 82 | + let accountManager = AccountManager.from({ host, multiuser: true, store }) |
| 83 | + accountManager.accountExists = sinon.stub().resolves(true) |
| 84 | + accountManager.loadAccountRecoveryEmail = sinon.stub().resolves('alice@example.com') |
| 85 | + accountManager.sendDeleteLink = sinon.stub().resolves() |
| 86 | + |
| 87 | + let req = { |
| 88 | + app: { locals: { accountManager } }, |
| 89 | + body: { username } |
| 90 | + } |
| 91 | + let res = HttpMocks.createResponse() |
| 92 | + |
| 93 | + DeleteAccountRequest.post(req, res) |
| 94 | + .then(() => { |
| 95 | + expect(DeleteAccountRequest.handlePost).to.have.been.called() |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe('validate()', () => { |
| 101 | + it('should throw an error if username is missing in multi-user mode', () => { |
| 102 | + let host = SolidHost.from({ serverUri: 'https://example.com' }) |
| 103 | + let accountManager = AccountManager.from({ host, multiuser: true }) |
| 104 | + |
| 105 | + let request = new DeleteAccountRequest({ accountManager }) |
| 106 | + |
| 107 | + expect(() => request.validate()).to.throw(/Username required/) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should not throw an error if username is missing in single user mode', () => { |
| 111 | + let host = SolidHost.from({ serverUri: 'https://example.com' }) |
| 112 | + let accountManager = AccountManager.from({ host, multiuser: false }) |
| 113 | + |
| 114 | + let request = new DeleteAccountRequest({ accountManager }) |
| 115 | + |
| 116 | + expect(() => request.validate()).to.not.throw() |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe('handlePost()', () => { |
| 121 | + it('should handle the post request', () => { |
| 122 | + let host = SolidHost.from({ serverUri: 'https://example.com' }) |
| 123 | + let store = { suffixAcl: '.acl' } |
| 124 | + let accountManager = AccountManager.from({ host, multiuser: true, store }) |
| 125 | + accountManager.loadAccountRecoveryEmail = sinon.stub().resolves('alice@example.com') |
| 126 | + accountManager.sendDeleteAccountEmail = sinon.stub().resolves() |
| 127 | + accountManager.accountExists = sinon.stub().resolves(true) |
| 128 | + |
| 129 | + let username = 'alice' |
| 130 | + let response = HttpMocks.createResponse() |
| 131 | + response.render = sinon.stub() |
| 132 | + |
| 133 | + let options = { accountManager, username, response } |
| 134 | + let request = new DeleteAccountRequest(options) |
| 135 | + |
| 136 | + sinon.spy(request, 'error') |
| 137 | + |
| 138 | + return DeleteAccountRequest.handlePost(request) |
| 139 | + .then(() => { |
| 140 | + expect(accountManager.loadAccountRecoveryEmail).to.have.been.called() |
| 141 | + expect(response.render).to.have.been.calledWith('account/delete-link-sent') |
| 142 | + expect(request.error).to.not.have.been.called() |
| 143 | + }) |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + describe('loadUser()', () => { |
| 148 | + it('should return a UserAccount instance based on username', () => { |
| 149 | + let host = SolidHost.from({ serverUri: 'https://example.com' }) |
| 150 | + let store = { suffixAcl: '.acl' } |
| 151 | + let accountManager = AccountManager.from({ host, multiuser: true, store }) |
| 152 | + accountManager.accountExists = sinon.stub().resolves(true) |
| 153 | + let username = 'alice' |
| 154 | + |
| 155 | + let options = { accountManager, username } |
| 156 | + let request = new DeleteAccountRequest(options) |
| 157 | + |
| 158 | + return request.loadUser() |
| 159 | + .then(account => { |
| 160 | + expect(account.webId).to.equal('https://alice.example.com/profile/card#me') |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + it('should throw an error if the user does not exist', done => { |
| 165 | + let host = SolidHost.from({ serverUri: 'https://example.com' }) |
| 166 | + let store = { suffixAcl: '.acl' } |
| 167 | + let accountManager = AccountManager.from({ host, multiuser: true, store }) |
| 168 | + accountManager.accountExists = sinon.stub().resolves(false) |
| 169 | + let username = 'alice' |
| 170 | + |
| 171 | + let options = { accountManager, username } |
| 172 | + let request = new DeleteAccountRequest(options) |
| 173 | + |
| 174 | + request.loadUser() |
| 175 | + .catch(error => { |
| 176 | + expect(error.message).to.equal('Account not found for that username') |
| 177 | + done() |
| 178 | + }) |
| 179 | + }) |
| 180 | + }) |
| 181 | +}) |
0 commit comments