SecurityHeadersTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const { assert } = require('chai')
  16. const async = require('async')
  17. const User = require('./helpers/User')
  18. const request = require('./helpers/request')
  19. const assert_has_common_headers = function(response) {
  20. const { headers } = response
  21. assert.equal(headers['x-download-options'], 'noopen')
  22. assert.equal(headers['x-xss-protection'], '1; mode=block')
  23. return assert.equal(headers['referrer-policy'], 'origin-when-cross-origin')
  24. }
  25. const assert_has_cache_headers = function(response) {
  26. const { headers } = response
  27. assert.equal(headers['surrogate-control'], 'no-store')
  28. assert.equal(
  29. headers['cache-control'],
  30. 'no-store, no-cache, must-revalidate, proxy-revalidate'
  31. )
  32. assert.equal(headers['pragma'], 'no-cache')
  33. return assert.equal(headers['expires'], '0')
  34. }
  35. const assert_has_no_cache_headers = function(response) {
  36. const { headers } = response
  37. assert.isUndefined(headers['surrogate-control'])
  38. assert.isUndefined(headers['cache-control'])
  39. assert.isUndefined(headers['pragma'])
  40. return assert.isUndefined(headers['expires'])
  41. }
  42. describe('SecurityHeaders', function() {
  43. before(function() {
  44. return (this.user = new User())
  45. })
  46. it('should not have x-powered-by header', done =>
  47. request.get('/', (err, res, body) => {
  48. assert.isUndefined(res.headers['x-powered-by'])
  49. return done()
  50. }))
  51. it('should have all common headers', done =>
  52. request.get('/', (err, res, body) => {
  53. assert_has_common_headers(res)
  54. return done()
  55. }))
  56. it('should not have cache headers on public pages', done =>
  57. request.get('/', (err, res, body) => {
  58. assert_has_no_cache_headers(res)
  59. return done()
  60. }))
  61. it('should have cache headers when user is logged in', function(done) {
  62. return async.series(
  63. [
  64. cb => this.user.login(cb),
  65. cb => this.user.request.get('/', cb),
  66. cb => this.user.logout(cb)
  67. ],
  68. (err, results) => {
  69. const main_response = results[1][0]
  70. assert_has_cache_headers(main_response)
  71. return done()
  72. }
  73. )
  74. })
  75. it('should have cache headers on project page', function(done) {
  76. return async.series(
  77. [
  78. cb => this.user.login(cb),
  79. cb => {
  80. return this.user.createProject(
  81. 'public-project',
  82. (error, project_id) => {
  83. if (error != null) {
  84. return done(error)
  85. }
  86. this.project_id = project_id
  87. return this.user.makePublic(this.project_id, 'readAndWrite', cb)
  88. }
  89. )
  90. },
  91. cb => this.user.logout(cb)
  92. ],
  93. (err, results) => {
  94. return request.get(`/project/${this.project_id}`, (err, res, body) => {
  95. assert_has_cache_headers(res)
  96. return done()
  97. })
  98. }
  99. )
  100. })
  101. })