SecurityHeadersTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* eslint-disable
  2. camelcase,
  3. node/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. const assert_has_asset_caching_headers = function (response) {
  43. const { headers } = response
  44. assert.equal(headers['cache-control'], 'public, max-age=31536000')
  45. }
  46. describe('SecurityHeaders', function () {
  47. beforeEach(function () {
  48. return (this.user = new User())
  49. })
  50. it('should not have x-powered-by header', function (done) {
  51. return request.get('/', (err, res, body) => {
  52. assert.isUndefined(res.headers['x-powered-by'])
  53. return done()
  54. })
  55. })
  56. it('should have all common headers', function (done) {
  57. return request.get('/', (err, res, body) => {
  58. assert_has_common_headers(res)
  59. return done()
  60. })
  61. })
  62. it('should not have cache headers on public pages', function (done) {
  63. return request.get('/', (err, res, body) => {
  64. assert_has_no_cache_headers(res)
  65. return done()
  66. })
  67. })
  68. it('should have caching headers on static assets', function (done) {
  69. request.get('/favicon.ico', (err, res) => {
  70. assert_has_asset_caching_headers(res)
  71. done(err)
  72. })
  73. })
  74. it('should have cache headers when user is logged in', function (done) {
  75. return async.series(
  76. [
  77. cb => this.user.login(cb),
  78. cb => this.user.request.get('/', cb),
  79. cb => this.user.logout(cb),
  80. ],
  81. (err, results) => {
  82. const main_response = results[1][0]
  83. assert_has_cache_headers(main_response)
  84. return done()
  85. }
  86. )
  87. })
  88. it('should have cache headers on project page', function (done) {
  89. return async.series(
  90. [
  91. cb => this.user.login(cb),
  92. cb => {
  93. return this.user.createProject(
  94. 'public-project',
  95. (error, project_id) => {
  96. if (error != null) {
  97. return done(error)
  98. }
  99. this.project_id = project_id
  100. return this.user.makePublic(this.project_id, 'readAndWrite', cb)
  101. }
  102. )
  103. },
  104. cb => this.user.logout(cb),
  105. ],
  106. (err, results) => {
  107. return request.get(`/project/${this.project_id}`, (err, res, body) => {
  108. assert_has_cache_headers(res)
  109. return done()
  110. })
  111. }
  112. )
  113. })
  114. it('should have caching headers on static assets when user is logged in', function (done) {
  115. async.series(
  116. [
  117. cb => this.user.login(cb),
  118. cb => this.user.request.get('/favicon.ico', cb),
  119. cb => this.user.logout(cb),
  120. ],
  121. (err, results) => {
  122. const res = results[1][0]
  123. assert_has_asset_caching_headers(res)
  124. done()
  125. }
  126. )
  127. })
  128. })