auth-tests.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const assert = require('assert');
  2. const sinon = require('sinon');
  3. const proxyquire = require('proxyquire').noCallThru();
  4. const storage = {
  5. metadata: sinon.stub(),
  6. setField: sinon.stub()
  7. };
  8. function request(id, auth) {
  9. return {
  10. params: { id },
  11. header: sinon.stub().returns(auth)
  12. };
  13. }
  14. function response() {
  15. return {
  16. sendStatus: sinon.stub(),
  17. set: sinon.stub()
  18. };
  19. }
  20. const next = sinon.stub();
  21. const storedMeta = {
  22. auth:
  23. 'r9uFxEs9GEVaQR9CJJ0uTKFGhFSOTRjOY2FCLFlCIZ0Cr-VGTVpMGlXDbNR8RMT55trMpSrzWtBVKq1LffOT2g',
  24. nonce: 'FL4oxA7IE1PW8shwFN9qZw=='
  25. };
  26. const authMiddleware = proxyquire('../../server/middleware/auth', {
  27. '../storage': storage
  28. }).hmac;
  29. describe('Owner Middleware', function() {
  30. afterEach(function() {
  31. storage.metadata.reset();
  32. storage.setField.reset();
  33. next.reset();
  34. });
  35. it('sends a 401 when no auth header is set', async function() {
  36. const req = request('x');
  37. const res = response();
  38. await authMiddleware(req, res, next);
  39. sinon.assert.calledWith(res.sendStatus, 401);
  40. sinon.assert.notCalled(next);
  41. });
  42. it('sends a 404 when metadata is not found', async function() {
  43. const req = request('x', 'y');
  44. const res = response();
  45. await authMiddleware(req, res, next);
  46. sinon.assert.calledWith(res.sendStatus, 404);
  47. sinon.assert.notCalled(next);
  48. });
  49. it('sends a 401 when the auth header is invalid base64', async function() {
  50. storage.metadata.returns(Promise.resolve(storedMeta));
  51. const req = request('x', '1');
  52. const res = response();
  53. await authMiddleware(req, res, next);
  54. sinon.assert.calledWith(res.sendStatus, 401);
  55. sinon.assert.notCalled(next);
  56. });
  57. it('authenticates when the hashes match', async function() {
  58. storage.metadata.returns(Promise.resolve(storedMeta));
  59. const req = request(
  60. 'x',
  61. 'send-v1 R7nZk14qJqZXtxpnAtw2uDIRQTRnO1qSO1Q0PiwcNA8'
  62. );
  63. const res = response();
  64. await authMiddleware(req, res, next);
  65. sinon.assert.calledOnce(next);
  66. sinon.assert.calledWith(storage.setField, 'x', 'nonce', req.nonce);
  67. sinon.assert.calledWith(
  68. res.set,
  69. 'WWW-Authenticate',
  70. `send-v1 ${req.nonce}`
  71. );
  72. sinon.assert.notCalled(res.sendStatus);
  73. assert.equal(req.authorized, true);
  74. assert.equal(req.meta, storedMeta);
  75. assert.notEqual(req.nonce, storedMeta.nonce);
  76. });
  77. it('sends a 401 when the hashes do not match', async function() {
  78. storage.metadata.returns(Promise.resolve(storedMeta));
  79. const req = request(
  80. 'x',
  81. 'send-v1 R8nZk14qJqZXtxpnAtw2uDIRQTRnO1qSO1Q0PiwcNA8'
  82. );
  83. const res = response();
  84. await authMiddleware(req, res, next);
  85. sinon.assert.calledWith(res.sendStatus, 401);
  86. sinon.assert.calledWith(
  87. res.set,
  88. 'WWW-Authenticate',
  89. `send-v1 ${storedMeta.nonce}`
  90. );
  91. sinon.assert.notCalled(next);
  92. });
  93. });