SessionSocketsTests.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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('node: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. beforeEach(function () {
  18. this.metrics = { inc: sinon.stub() }
  19. this.SessionSocketsModule = SandboxedModule.require(modulePath, {
  20. requires: {
  21. '@overleaf/metrics': this.metrics,
  22. },
  23. })
  24. this.io = new EventEmitter()
  25. this.id1 = Math.random().toString()
  26. this.id2 = Math.random().toString()
  27. const redisResponses = {
  28. error: [new Error('Redis: something went wrong'), null],
  29. unknownId: [null, null],
  30. }
  31. redisResponses[this.id1] = [null, { user: { _id: '123' } }]
  32. redisResponses[this.id2] = [null, { user: { _id: 'abc' } }]
  33. this.sessionStore = {
  34. get: sinon
  35. .stub()
  36. .callsFake((id, fn) => fn.apply(null, redisResponses[id])),
  37. }
  38. this.cookieParser = function (req, res, next) {
  39. req.signedCookies = req._signedCookies
  40. return next()
  41. }
  42. this.SessionSockets = this.SessionSocketsModule(
  43. this.io,
  44. this.sessionStore,
  45. this.cookieParser,
  46. 'ol.sid'
  47. )
  48. return (this.checkSocket = (socket, fn) => {
  49. this.SessionSockets.once('connection', fn)
  50. return this.io.emit('connection', socket)
  51. })
  52. })
  53. describe('without cookies', function () {
  54. beforeEach(function () {
  55. return (this.socket = { handshake: {} })
  56. })
  57. it('should return a lookup error', function (done) {
  58. return this.checkSocket(this.socket, error => {
  59. expect(error).to.exist
  60. expect(error.message).to.equal('could not look up session by key')
  61. return done()
  62. })
  63. })
  64. it('should not query redis', function (done) {
  65. return this.checkSocket(this.socket, () => {
  66. expect(this.sessionStore.get.called).to.equal(false)
  67. return done()
  68. })
  69. })
  70. it('should increment the session.cookie metric with status "none"', function (done) {
  71. return this.checkSocket(this.socket, () => {
  72. expect(this.metrics.inc).to.be.calledWith('session.cookie', 1, {
  73. status: 'none',
  74. })
  75. return done()
  76. })
  77. })
  78. })
  79. describe('with a different cookie', function () {
  80. beforeEach(function () {
  81. return (this.socket = { handshake: { _signedCookies: { other: 1 } } })
  82. })
  83. it('should return a lookup error', function (done) {
  84. return this.checkSocket(this.socket, error => {
  85. expect(error).to.exist
  86. expect(error.message).to.equal('could not look up session by key')
  87. return done()
  88. })
  89. })
  90. it('should not query redis', function (done) {
  91. return this.checkSocket(this.socket, () => {
  92. expect(this.sessionStore.get.called).to.equal(false)
  93. return done()
  94. })
  95. })
  96. })
  97. describe('with a cookie with an invalid signature', function () {
  98. beforeEach(function () {
  99. return (this.socket = {
  100. handshake: { _signedCookies: { 'ol.sid': false } },
  101. })
  102. })
  103. it('should return a lookup error', function (done) {
  104. return this.checkSocket(this.socket, error => {
  105. expect(error).to.exist
  106. expect(error.message).to.equal('could not look up session by key')
  107. return done()
  108. })
  109. })
  110. it('should not query redis', function (done) {
  111. return this.checkSocket(this.socket, () => {
  112. expect(this.sessionStore.get.called).to.equal(false)
  113. return done()
  114. })
  115. })
  116. it('should increment the session.cookie metric with status=bad-signature', function (done) {
  117. return this.checkSocket(this.socket, () => {
  118. expect(this.metrics.inc).to.be.calledWith('session.cookie', 1, {
  119. status: 'bad-signature',
  120. })
  121. return done()
  122. })
  123. })
  124. })
  125. describe('with a valid cookie and a failing session lookup', function () {
  126. beforeEach(function () {
  127. return (this.socket = {
  128. handshake: { _signedCookies: { 'ol.sid': 'error' } },
  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 return a redis error', function (done) {
  138. return this.checkSocket(this.socket, error => {
  139. expect(error).to.exist
  140. expect(error.message).to.equal('Redis: something went wrong')
  141. return done()
  142. })
  143. })
  144. it('should increment the session.cookie metric with status=error', function (done) {
  145. return this.checkSocket(this.socket, () => {
  146. expect(this.metrics.inc).to.be.calledWith('session.cookie', 1, {
  147. status: 'error',
  148. })
  149. return done()
  150. })
  151. })
  152. })
  153. describe('with a valid cookie and no matching session', function () {
  154. beforeEach(function () {
  155. return (this.socket = {
  156. handshake: { _signedCookies: { 'ol.sid': 'unknownId' } },
  157. })
  158. })
  159. it('should query redis', function (done) {
  160. return this.checkSocket(this.socket, () => {
  161. expect(this.sessionStore.get.called).to.equal(true)
  162. return done()
  163. })
  164. })
  165. it('should return a lookup error', function (done) {
  166. return this.checkSocket(this.socket, error => {
  167. expect(error).to.exist
  168. expect(error.message).to.equal('could not look up session by key')
  169. return done()
  170. })
  171. })
  172. it('should increment the session.cookie metric with status=missing', function (done) {
  173. return this.checkSocket(this.socket, () => {
  174. expect(this.metrics.inc).to.be.calledWith('session.cookie', 1, {
  175. status: 'missing',
  176. })
  177. return done()
  178. })
  179. })
  180. })
  181. describe('with a valid cookie and a matching session', function () {
  182. beforeEach(function () {
  183. return (this.socket = {
  184. handshake: { _signedCookies: { 'ol.sid': this.id1 } },
  185. })
  186. })
  187. it('should query redis', function (done) {
  188. return this.checkSocket(this.socket, () => {
  189. expect(this.sessionStore.get.called).to.equal(true)
  190. return done()
  191. })
  192. })
  193. it('should not return an error', function (done) {
  194. return this.checkSocket(this.socket, error => {
  195. expect(error).to.not.exist
  196. return done()
  197. })
  198. })
  199. it('should return the session', function (done) {
  200. return this.checkSocket(this.socket, (error, s, session) => {
  201. if (error) return done(error)
  202. expect(session).to.deep.equal({ user: { _id: '123' } })
  203. return done()
  204. })
  205. })
  206. it('should increment the session.cookie metric with status=signed', function (done) {
  207. return this.checkSocket(this.socket, () => {
  208. expect(this.metrics.inc).to.be.calledWith('session.cookie', 1, {
  209. status: 'signed',
  210. })
  211. return done()
  212. })
  213. })
  214. })
  215. describe('with a different valid cookie and matching session', function () {
  216. beforeEach(function () {
  217. return (this.socket = {
  218. handshake: { _signedCookies: { 'ol.sid': this.id2 } },
  219. })
  220. })
  221. it('should query redis', function (done) {
  222. return this.checkSocket(this.socket, () => {
  223. expect(this.sessionStore.get.called).to.equal(true)
  224. return done()
  225. })
  226. })
  227. it('should not return an error', function (done) {
  228. return this.checkSocket(this.socket, error => {
  229. expect(error).to.not.exist
  230. return done()
  231. })
  232. })
  233. it('should return the other session', function (done) {
  234. return this.checkSocket(this.socket, (error, s, session) => {
  235. if (error) return done(error)
  236. expect(session).to.deep.equal({ user: { _id: 'abc' } })
  237. return done()
  238. })
  239. })
  240. it('should increment the session.cookie metric with status=error', function (done) {
  241. return this.checkSocket(this.socket, () => {
  242. expect(this.metrics.inc).to.be.calledWith('session.cookie', 1, {
  243. status: 'signed',
  244. })
  245. return done()
  246. })
  247. })
  248. })
  249. })