2018-02-06 23:31:18 +01:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const proxyquire = require('proxyquire').noCallThru();
|
|
|
|
|
|
|
|
const storage = {
|
|
|
|
setField: sinon.stub()
|
|
|
|
};
|
|
|
|
|
|
|
|
function request(id, body) {
|
|
|
|
return {
|
|
|
|
params: { id },
|
|
|
|
body
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function response() {
|
|
|
|
return {
|
|
|
|
sendStatus: sinon.stub()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const passwordRoute = proxyquire('../../server/routes/password', {
|
|
|
|
'../storage': storage
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('/api/password', function() {
|
|
|
|
afterEach(function() {
|
|
|
|
storage.setField.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls storage.setField with the correct parameter', function() {
|
|
|
|
const req = request('x', { auth: 'z' });
|
|
|
|
const res = response();
|
|
|
|
passwordRoute(req, res);
|
|
|
|
sinon.assert.calledWith(storage.setField, 'x', 'auth', 'z');
|
2020-07-27 20:18:52 +02:00
|
|
|
sinon.assert.calledWith(storage.setField, 'x', 'pwd', 1);
|
2018-02-06 23:31:18 +01:00
|
|
|
sinon.assert.calledWith(res.sendStatus, 200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends a 400 if auth is missing', function() {
|
|
|
|
const req = request('x', {});
|
|
|
|
const res = response();
|
|
|
|
passwordRoute(req, res);
|
|
|
|
sinon.assert.calledWith(res.sendStatus, 400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends a 404 on failure', function() {
|
|
|
|
storage.setField.throws(new Error());
|
|
|
|
const req = request('x', { auth: 'z' });
|
|
|
|
const res = response();
|
|
|
|
passwordRoute(req, res);
|
|
|
|
sinon.assert.calledWith(res.sendStatus, 404);
|
|
|
|
});
|
|
|
|
});
|