SecurityHeadersTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. beforeEach(function() {
  44. return (this.user = new User())
  45. })
  46. it('should not have x-powered-by header', function(done) {
  47. return request.get('/', (err, res, body) => {
  48. assert.isUndefined(res.headers['x-powered-by'])
  49. return done()
  50. })
  51. })
  52. it('should have all common headers', function(done) {
  53. return request.get('/', (err, res, body) => {
  54. assert_has_common_headers(res)
  55. return done()
  56. })
  57. })
  58. it('should not have cache headers on public pages', function(done) {
  59. return request.get('/', (err, res, body) => {
  60. assert_has_no_cache_headers(res)
  61. return done()
  62. })
  63. })
  64. it('should have cache headers when user is logged in', function(done) {
  65. return async.series(
  66. [
  67. cb => this.user.login(cb),
  68. cb => this.user.request.get('/', cb),
  69. cb => this.user.logout(cb)
  70. ],
  71. (err, results) => {
  72. const main_response = results[1][0]
  73. assert_has_cache_headers(main_response)
  74. return done()
  75. }
  76. )
  77. })
  78. it('should have cache headers on project page', function(done) {
  79. return async.series(
  80. [
  81. cb => this.user.login(cb),
  82. cb => {
  83. return this.user.createProject(
  84. 'public-project',
  85. (error, project_id) => {
  86. if (error != null) {
  87. return done(error)
  88. }
  89. this.project_id = project_id
  90. return this.user.makePublic(this.project_id, 'readAndWrite', cb)
  91. }
  92. )
  93. },
  94. cb => this.user.logout(cb)
  95. ],
  96. (err, results) => {
  97. return request.get(`/project/${this.project_id}`, (err, res, body) => {
  98. assert_has_cache_headers(res)
  99. return done()
  100. })
  101. }
  102. )
  103. })
  104. })