SessionSocketsTests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import RealTimeClient from './helpers/RealTimeClient.js'
  2. import FixturesManager from './helpers/FixturesManager.js'
  3. import Settings from '@overleaf/settings'
  4. import signature from 'cookie-signature'
  5. import { expect } from 'chai'
  6. describe('SessionSockets', function () {
  7. beforeEach(function (done) {
  8. FixturesManager.setUpProject(
  9. {
  10. privilegeLevel: 'owner',
  11. },
  12. (err, options) => {
  13. if (err) return done(err)
  14. this.checkSocket = function (fn) {
  15. RealTimeClient.connect(options.project_id, fn)
  16. }
  17. done()
  18. }
  19. )
  20. })
  21. describe('without cookies', function () {
  22. beforeEach(function () {
  23. RealTimeClient.cookie = null
  24. })
  25. it('should return a lookup error', function (done) {
  26. this.checkSocket(error => {
  27. expect(error).to.exist
  28. expect(error.message).to.equal('invalid session')
  29. done()
  30. })
  31. })
  32. })
  33. describe('with a different cookie', function () {
  34. beforeEach(function () {
  35. RealTimeClient.cookie = 'some.key=someValue'
  36. })
  37. it('should return a lookup error', function (done) {
  38. this.checkSocket(error => {
  39. expect(error).to.exist
  40. expect(error.message).to.equal('invalid session')
  41. done()
  42. })
  43. })
  44. })
  45. describe('with an invalid cookie', function () {
  46. beforeEach(function (done) {
  47. RealTimeClient.setSession({}, error => {
  48. if (error) {
  49. return done(error)
  50. }
  51. RealTimeClient.cookie = `${
  52. Settings.cookieName
  53. }=${RealTimeClient.cookie.slice(17, 49)}`
  54. done()
  55. })
  56. })
  57. it('should return a lookup error', function (done) {
  58. this.checkSocket(error => {
  59. expect(error).to.exist
  60. expect(error.message).to.equal('invalid session')
  61. done()
  62. })
  63. })
  64. })
  65. describe('with a valid cookie and no matching session', function () {
  66. beforeEach(function () {
  67. RealTimeClient.cookie = `${Settings.cookieName}=unknownId`
  68. })
  69. it('should return a lookup error', function (done) {
  70. this.checkSocket(error => {
  71. expect(error).to.exist
  72. expect(error.message).to.equal('invalid session')
  73. done()
  74. })
  75. })
  76. })
  77. describe('with a valid cookie and a matching session', function () {
  78. it('should not return an error', function (done) {
  79. this.checkSocket(error => {
  80. expect(error).to.not.exist
  81. done()
  82. })
  83. })
  84. })
  85. describe('with a cookie signed by the fallback key and a matching session', function () {
  86. beforeEach(function () {
  87. RealTimeClient.cookie =
  88. RealTimeClient.cookieSignedWith.sessionSecretFallback
  89. })
  90. it('should not return an error', function (done) {
  91. this.checkSocket(error => {
  92. expect(error).to.not.exist
  93. done()
  94. })
  95. })
  96. })
  97. describe('with a cookie signed by the upcoming key and a matching session', function () {
  98. beforeEach(function () {
  99. RealTimeClient.cookie =
  100. RealTimeClient.cookieSignedWith.sessionSecretUpcoming
  101. })
  102. it('should not return an error', function (done) {
  103. this.checkSocket(error => {
  104. expect(error).to.not.exist
  105. done()
  106. })
  107. })
  108. })
  109. describe('with a cookie signed with an unrecognized secret and a matching session', function () {
  110. beforeEach(function () {
  111. const [sessionKey] = RealTimeClient.cookie.split('.')
  112. // sign the session key with a unrecognized secret
  113. RealTimeClient.cookie = signature.sign(
  114. sessionKey,
  115. 'unrecognised-session-secret'
  116. )
  117. })
  118. it('should return a lookup error', function (done) {
  119. this.checkSocket(error => {
  120. expect(error).to.exist
  121. expect(error.message).to.equal('invalid session')
  122. done()
  123. })
  124. })
  125. })
  126. })