params-tests.js 1.3 KB

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