SessionSocketsTests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. const { EventEmitter } = require('events')
  12. const { expect } = require('chai')
  13. const SandboxedModule = require('sandboxed-module')
  14. const modulePath = '../../../app/js/SessionSockets'
  15. const sinon = require('sinon')
  16. describe('SessionSockets', function () {
  17. before(function () {
  18. this.SessionSocketsModule = SandboxedModule.require(modulePath)
  19. this.io = new EventEmitter()
  20. this.id1 = Math.random().toString()
  21. this.id2 = Math.random().toString()
  22. const redisResponses = {
  23. error: [new Error('Redis: something went wrong'), null],
  24. unknownId: [null, null],
  25. }
  26. redisResponses[this.id1] = [null, { user: { _id: '123' } }]
  27. redisResponses[this.id2] = [null, { user: { _id: 'abc' } }]
  28. this.sessionStore = {
  29. get: sinon
  30. .stub()
  31. .callsFake((id, fn) => fn.apply(null, redisResponses[id])),
  32. }
  33. this.cookieParser = function (req, res, next) {
  34. req.signedCookies = req._signedCookies
  35. return next()
  36. }
  37. this.SessionSockets = this.SessionSocketsModule(
  38. this.io,
  39. this.sessionStore,
  40. this.cookieParser,
  41. 'ol.sid'
  42. )
  43. return (this.checkSocket = (socket, fn) => {
  44. this.SessionSockets.once('connection', fn)
  45. return this.io.emit('connection', socket)
  46. })
  47. })
  48. describe('without cookies', function () {
  49. before(function () {
  50. return (this.socket = { handshake: {} })
  51. })
  52. it('should return a lookup error', function (done) {
  53. return this.checkSocket(this.socket, error => {
  54. expect(error).to.exist
  55. expect(error.message).to.equal('could not look up session by key')
  56. return done()
  57. })
  58. })
  59. return it('should not query redis', function (done) {
  60. return this.checkSocket(this.socket, () => {
  61. expect(this.sessionStore.get.called).to.equal(false)
  62. return done()
  63. })
  64. })
  65. })
  66. describe('with a different cookie', function () {
  67. before(function () {
  68. return (this.socket = { handshake: { _signedCookies: { other: 1 } } })
  69. })
  70. it('should return a lookup error', function (done) {
  71. return this.checkSocket(this.socket, error => {
  72. expect(error).to.exist
  73. expect(error.message).to.equal('could not look up session by key')
  74. return done()
  75. })
  76. })
  77. return it('should not query redis', function (done) {
  78. return this.checkSocket(this.socket, () => {
  79. expect(this.sessionStore.get.called).to.equal(false)
  80. return done()
  81. })
  82. })
  83. })
  84. describe('with a valid cookie and a failing session lookup', function () {
  85. before(function () {
  86. return (this.socket = {
  87. handshake: { _signedCookies: { 'ol.sid': 'error' } },
  88. })
  89. })
  90. it('should query redis', function (done) {
  91. return this.checkSocket(this.socket, () => {
  92. expect(this.sessionStore.get.called).to.equal(true)
  93. return done()
  94. })
  95. })
  96. return it('should return a redis error', function (done) {
  97. return this.checkSocket(this.socket, error => {
  98. expect(error).to.exist
  99. expect(error.message).to.equal('Redis: something went wrong')
  100. return done()
  101. })
  102. })
  103. })
  104. describe('with a valid cookie and no matching session', function () {
  105. before(function () {
  106. return (this.socket = {
  107. handshake: { _signedCookies: { 'ol.sid': 'unknownId' } },
  108. })
  109. })
  110. it('should query redis', function (done) {
  111. return this.checkSocket(this.socket, () => {
  112. expect(this.sessionStore.get.called).to.equal(true)
  113. return done()
  114. })
  115. })
  116. return it('should return a lookup error', function (done) {
  117. return this.checkSocket(this.socket, error => {
  118. expect(error).to.exist
  119. expect(error.message).to.equal('could not look up session by key')
  120. return done()
  121. })
  122. })
  123. })
  124. describe('with a valid cookie and a matching session', function () {
  125. before(function () {
  126. return (this.socket = {
  127. handshake: { _signedCookies: { 'ol.sid': this.id1 } },
  128. })
  129. })
  130. it('should query redis', function (done) {
  131. return this.checkSocket(this.socket, () => {
  132. expect(this.sessionStore.get.called).to.equal(true)
  133. return done()
  134. })
  135. })
  136. it('should not return an error', function (done) {
  137. return this.checkSocket(this.socket, error => {
  138. expect(error).to.not.exist
  139. return done()
  140. })
  141. })
  142. return it('should return the session', function (done) {
  143. return this.checkSocket(this.socket, (error, s, session) => {
  144. if (error) return done(error)
  145. expect(session).to.deep.equal({ user: { _id: '123' } })
  146. return done()
  147. })
  148. })
  149. })
  150. return describe('with a different valid cookie and matching session', function () {
  151. before(function () {
  152. return (this.socket = {
  153. handshake: { _signedCookies: { 'ol.sid': this.id2 } },
  154. })
  155. })
  156. it('should query redis', function (done) {
  157. return this.checkSocket(this.socket, () => {
  158. expect(this.sessionStore.get.called).to.equal(true)
  159. return done()
  160. })
  161. })
  162. it('should not return an error', function (done) {
  163. return this.checkSocket(this.socket, error => {
  164. expect(error).to.not.exist
  165. return done()
  166. })
  167. })
  168. return it('should return the other session', function (done) {
  169. return this.checkSocket(this.socket, (error, s, session) => {
  170. if (error) return done(error)
  171. expect(session).to.deep.equal({ user: { _id: 'abc' } })
  172. return done()
  173. })
  174. })
  175. })
  176. })