SessionSocketsTests.js 5.7 KB

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