password-tests.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const sinon = require('sinon');
  2. const proxyquire = require('proxyquire').noCallThru();
  3. const storage = {
  4. setField: sinon.stub()
  5. };
  6. function request(id, body) {
  7. return {
  8. params: { id },
  9. body
  10. };
  11. }
  12. function response() {
  13. return {
  14. sendStatus: sinon.stub()
  15. };
  16. }
  17. const passwordRoute = proxyquire('../../server/routes/password', {
  18. '../storage': storage
  19. });
  20. describe('/api/password', function() {
  21. afterEach(function() {
  22. storage.setField.reset();
  23. });
  24. it('calls storage.setField with the correct parameter', function() {
  25. const req = request('x', { auth: 'z' });
  26. const res = response();
  27. passwordRoute(req, res);
  28. sinon.assert.calledWith(storage.setField, 'x', 'auth', 'z');
  29. sinon.assert.calledWith(storage.setField, 'x', 'pwd', 1);
  30. sinon.assert.calledWith(res.sendStatus, 200);
  31. });
  32. it('sends a 400 if auth is missing', function() {
  33. const req = request('x', {});
  34. const res = response();
  35. passwordRoute(req, res);
  36. sinon.assert.calledWith(res.sendStatus, 400);
  37. });
  38. it('sends a 404 on failure', function() {
  39. storage.setField.throws(new Error());
  40. const req = request('x', { auth: 'z' });
  41. const res = response();
  42. passwordRoute(req, res);
  43. sinon.assert.calledWith(res.sendStatus, 404);
  44. });
  45. });