CsrfTests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const { assert } = require('chai')
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../../app/src/infrastructure/Csrf.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('Csrf', function() {
  21. beforeEach(function() {
  22. this.csurf_csrf = sinon
  23. .stub()
  24. .callsArgWith(2, (this.err = { code: 'EBADCSRFTOKEN' }))
  25. this.Csrf = SandboxedModule.require(modulePath, {
  26. requires: {
  27. csurf: sinon.stub().returns(this.csurf_csrf)
  28. }
  29. })
  30. this.csrf = new this.Csrf()
  31. this.next = sinon.stub()
  32. this.path = '/foo/bar'
  33. this.req = {
  34. path: this.path,
  35. method: 'POST'
  36. }
  37. return (this.res = {})
  38. })
  39. describe('the middleware', function() {
  40. describe('when there are no excluded routes', () =>
  41. it('passes the csrf error on', function() {
  42. this.csrf.middleware(this.req, this.res, this.next)
  43. return expect(this.next.calledWith(this.err)).to.equal(true)
  44. }))
  45. describe('when the route is excluded', () =>
  46. it('does not pass the csrf error on', function() {
  47. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  48. this.csrf.middleware(this.req, this.res, this.next)
  49. return expect(this.next.calledWith(this.err)).to.equal(false)
  50. }))
  51. describe('when there is a partial route match', function() {
  52. it('passes the csrf error on when the match is too short', function() {
  53. this.csrf.disableDefaultCsrfProtection('/foo', 'POST')
  54. this.csrf.middleware(this.req, this.res, this.next)
  55. return expect(this.next.calledWith(this.err)).to.equal(true)
  56. })
  57. it('passes the csrf error on when the match is too long', function() {
  58. this.csrf.disableDefaultCsrfProtection('/foo/bar/baz', 'POST')
  59. this.csrf.middleware(this.req, this.res, this.next)
  60. return expect(this.next.calledWith(this.err)).to.equal(true)
  61. })
  62. })
  63. describe('when there are multiple exclusions', function() {
  64. it('does not pass the csrf error on when the match is present', function() {
  65. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  66. this.csrf.disableDefaultCsrfProtection('/test', 'POST')
  67. this.csrf.disableDefaultCsrfProtection('/a/b/c', 'POST')
  68. this.csrf.middleware(this.req, this.res, this.next)
  69. return expect(this.next.calledWith(this.err)).to.equal(false)
  70. })
  71. it('passes the csrf error on when the match is not present', function() {
  72. this.csrf.disableDefaultCsrfProtection('/url', 'POST')
  73. this.csrf.disableDefaultCsrfProtection('/test', 'POST')
  74. this.csrf.disableDefaultCsrfProtection('/a/b/c', 'POST')
  75. this.csrf.middleware(this.req, this.res, this.next)
  76. return expect(this.next.calledWith(this.err)).to.equal(true)
  77. })
  78. })
  79. describe('when the method does not match', () =>
  80. it('passes the csrf error on', function() {
  81. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  82. this.req.method = 'GET'
  83. this.csrf.middleware(this.req, this.res, this.next)
  84. return expect(this.next.calledWith(this.err)).to.equal(true)
  85. }))
  86. describe('when the route is excluded, but the error is not a bad-csrf-token error', () =>
  87. it('passes the error on', function() {
  88. let err
  89. this.Csrf = SandboxedModule.require(modulePath, {
  90. requires: {
  91. csurf: (this.csurf = sinon
  92. .stub()
  93. .returns(
  94. (this.csurf_csrf = sinon
  95. .stub()
  96. .callsArgWith(2, (err = { code: 'EOTHER' })))
  97. ))
  98. }
  99. })
  100. this.csrf = new this.Csrf()
  101. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  102. this.csrf.middleware(this.req, this.res, this.next)
  103. expect(this.next.calledWith(err)).to.equal(true)
  104. return expect(this.next.calledWith(this.err)).to.equal(false)
  105. }))
  106. })
  107. describe('validateRequest', function() {
  108. describe('when the request is invalid', () =>
  109. it('calls the callback with `false`', function() {
  110. this.cb = sinon.stub()
  111. this.Csrf.validateRequest(this.req, this.cb)
  112. return expect(this.cb.calledWith(false)).to.equal(true)
  113. }))
  114. describe('when the request is valid', () =>
  115. it('calls the callback with `true`', function() {
  116. this.Csrf = SandboxedModule.require(modulePath, {
  117. requires: {
  118. csurf: (this.csurf = sinon
  119. .stub()
  120. .returns((this.csurf_csrf = sinon.stub().callsArg(2))))
  121. }
  122. })
  123. this.cb = sinon.stub()
  124. this.Csrf.validateRequest(this.req, this.cb)
  125. return expect(this.cb.calledWith(true)).to.equal(true)
  126. }))
  127. })
  128. describe('validateToken', function() {
  129. describe('when the request is invalid', () =>
  130. it('calls the callback with `false`', function() {
  131. this.cb = sinon.stub()
  132. this.Csrf.validateToken('token', {}, this.cb)
  133. return expect(this.cb.calledWith(false)).to.equal(true)
  134. }))
  135. describe('when the request is valid', () =>
  136. it('calls the callback with `true`', function() {
  137. this.Csrf = SandboxedModule.require(modulePath, {
  138. requires: {
  139. csurf: (this.csurf = sinon
  140. .stub()
  141. .returns((this.csurf_csrf = sinon.stub().callsArg(2))))
  142. }
  143. })
  144. this.cb = sinon.stub()
  145. this.Csrf.validateToken('goodtoken', {}, this.cb)
  146. return expect(this.cb.calledWith(true)).to.equal(true)
  147. }))
  148. describe('when there is no token', () =>
  149. it('calls the callback with `false`', function() {
  150. this.Csrf = SandboxedModule.require(modulePath, {
  151. requires: {
  152. csurf: (this.csurf = sinon
  153. .stub()
  154. .returns((this.csurf_csrf = sinon.stub().callsArg(2))))
  155. }
  156. })
  157. this.cb = sinon.stub()
  158. this.Csrf.validateToken(null, {}, this.cb)
  159. return expect(this.cb.calledWith(false)).to.equal(true)
  160. }))
  161. })
  162. })