metadata-tests.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const sinon = require('sinon');
  2. const proxyquire = require('proxyquire').noCallThru();
  3. const storage = {
  4. ttl: sinon.stub(),
  5. length: sinon.stub()
  6. };
  7. function request(id, meta) {
  8. return {
  9. params: { id },
  10. meta
  11. };
  12. }
  13. function response() {
  14. return {
  15. sendStatus: sinon.stub(),
  16. send: sinon.stub()
  17. };
  18. }
  19. const metadataRoute = proxyquire('../../server/routes/metadata', {
  20. '../storage': storage
  21. });
  22. describe('/api/metadata', function() {
  23. afterEach(function() {
  24. storage.ttl.reset();
  25. storage.length.reset();
  26. });
  27. it('calls storage.ttl with the id parameter', async function() {
  28. const req = request('x');
  29. const res = response();
  30. await metadataRoute(req, res);
  31. sinon.assert.calledWith(storage.ttl, 'x');
  32. });
  33. it('sends a 404 on failure', async function() {
  34. storage.ttl.returns(Promise.reject(new Error()));
  35. const res = response();
  36. await metadataRoute(request('x'), res);
  37. sinon.assert.calledWith(res.sendStatus, 404);
  38. });
  39. it('returns a json object', async function() {
  40. storage.ttl.returns(Promise.resolve(123));
  41. const meta = {
  42. dlimit: 1,
  43. dl: 0,
  44. metadata: 'foo'
  45. };
  46. const res = response();
  47. await metadataRoute(request('x', meta), res);
  48. sinon.assert.calledWithMatch(res.send, {
  49. metadata: 'foo',
  50. finalDownload: true,
  51. ttl: 123
  52. });
  53. });
  54. });