UserSessionsManager.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/User/UserSessionsManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'UserSessionsManager', ->
  8. beforeEach ->
  9. @user =
  10. _id: "abcd"
  11. email: "user@example.com"
  12. @sessionId = 'some_session_id'
  13. @rclient =
  14. multi: sinon.stub()
  15. exec: sinon.stub()
  16. get: sinon.stub()
  17. sadd: sinon.stub()
  18. srem: sinon.stub()
  19. smembers: sinon.stub()
  20. expire: sinon.stub()
  21. @rclient.multi.returns(@rclient)
  22. @rclient.get.returns(@rclient)
  23. @rclient.sadd.returns(@rclient)
  24. @rclient.srem.returns(@rclient)
  25. @rclient.smembers.returns(@rclient)
  26. @rclient.expire.returns(@rclient)
  27. @rclient.exec.callsArgWith(0, null)
  28. @redis =
  29. createClient: () => @rclient
  30. @logger =
  31. err: sinon.stub()
  32. error: sinon.stub()
  33. log: sinon.stub()
  34. @settings =
  35. redis:
  36. web: {}
  37. @UserSessionsManager = SandboxedModule.require modulePath, requires:
  38. "redis-sharelatex": @redis
  39. "logger-sharelatex": @logger
  40. "settings-sharelatex": @settings
  41. describe '_sessionSetKey', ->
  42. it 'should build the correct key', ->
  43. result = @UserSessionsManager._sessionSetKey(@user)
  44. result.should.equal 'UserSessions:abcd'
  45. describe '_sessionKey', ->
  46. it 'should build the correct key', ->
  47. result = @UserSessionsManager._sessionKey(@sessionId)
  48. result.should.equal 'sess:some_session_id'
  49. describe 'trackSession', ->
  50. beforeEach ->
  51. @call = (callback) =>
  52. @UserSessionsManager.trackSession @user, @sessionId, callback
  53. @rclient.exec.callsArgWith(0, null)
  54. @_checkSessions = sinon.stub(@UserSessionsManager, '_checkSessions').returns(null)
  55. afterEach ->
  56. @_checkSessions.restore()
  57. it 'should not produce an error', (done) ->
  58. @call (err) =>
  59. expect(err).to.not.be.instanceof Error
  60. done()
  61. it 'should call the appropriate redis methods', (done) ->
  62. @call (err) =>
  63. @rclient.multi.callCount.should.equal 1
  64. @rclient.sadd.callCount.should.equal 1
  65. @rclient.expire.callCount.should.equal 1
  66. @rclient.exec.callCount.should.equal 1
  67. done()
  68. it 'should call _checkSessions', (done) ->
  69. @call (err) =>
  70. @_checkSessions.callCount.should.equal 1
  71. done()
  72. describe 'when rclient produces an error', ->
  73. beforeEach ->
  74. @rclient.exec.callsArgWith(0, new Error('woops'))
  75. it 'should produce an error', (done) ->
  76. @call (err) =>
  77. expect(err).to.be.instanceof Error
  78. done()
  79. it 'should not call _checkSessions', (done) ->
  80. @call (err) =>
  81. @_checkSessions.callCount.should.equal 0
  82. done()
  83. describe 'when no user is supplied', ->
  84. beforeEach ->
  85. @call = (callback) =>
  86. @UserSessionsManager.trackSession null, @sessionId, callback
  87. it 'should not produce an error', (done) ->
  88. @call (err) =>
  89. expect(err).to.not.be.instanceof Error
  90. expect(err).to.equal null
  91. done()
  92. it 'should not call the appropriate redis methods', (done) ->
  93. @call (err) =>
  94. @rclient.multi.callCount.should.equal 0
  95. @rclient.sadd.callCount.should.equal 0
  96. @rclient.expire.callCount.should.equal 0
  97. @rclient.exec.callCount.should.equal 0
  98. done()
  99. it 'should not call _checkSessions', (done) ->
  100. @call (err) =>
  101. @_checkSessions.callCount.should.equal 0
  102. done()
  103. describe 'when no sessionId is supplied', ->
  104. beforeEach ->
  105. @call = (callback) =>
  106. @UserSessionsManager.trackSession @user, null, callback
  107. it 'should not produce an error', (done) ->
  108. @call (err) =>
  109. expect(err).to.not.be.instanceof Error
  110. expect(err).to.equal null
  111. done()
  112. it 'should not call the appropriate redis methods', (done) ->
  113. @call (err) =>
  114. @rclient.multi.callCount.should.equal 0
  115. @rclient.sadd.callCount.should.equal 0
  116. @rclient.expire.callCount.should.equal 0
  117. @rclient.exec.callCount.should.equal 0
  118. done()
  119. it 'should not call _checkSessions', (done) ->
  120. @call (err) =>
  121. @_checkSessions.callCount.should.equal 0
  122. done()
  123. describe 'untrackSession', ->
  124. beforeEach ->
  125. @call = (callback) =>
  126. @UserSessionsManager.untrackSession @user, @sessionId, callback
  127. @rclient.exec.callsArgWith(0, null)
  128. @_checkSessions = sinon.stub(@UserSessionsManager, '_checkSessions').returns(null)
  129. afterEach ->
  130. @_checkSessions.restore()
  131. it 'should not produce an error', (done) ->
  132. @call (err) =>
  133. expect(err).to.not.be.instanceof Error
  134. done()
  135. it 'should call the appropriate redis methods', (done) ->
  136. @call (err) =>
  137. @rclient.multi.callCount.should.equal 1
  138. @rclient.srem.callCount.should.equal 1
  139. @rclient.expire.callCount.should.equal 1
  140. @rclient.exec.callCount.should.equal 1
  141. done()
  142. it 'should call _checkSessions', (done) ->
  143. @call (err) =>
  144. @_checkSessions.callCount.should.equal 1
  145. done()
  146. describe 'when rclient produces an error', ->
  147. beforeEach ->
  148. @rclient.exec.callsArgWith(0, new Error('woops'))
  149. it 'should produce an error', (done) ->
  150. @call (err) =>
  151. expect(err).to.be.instanceof Error
  152. done()
  153. it 'should not call _checkSessions', (done) ->
  154. @call (err) =>
  155. @_checkSessions.callCount.should.equal 0
  156. done()
  157. describe 'when no user is supplied', ->
  158. beforeEach ->
  159. @call = (callback) =>
  160. @UserSessionsManager.untrackSession null, @sessionId, callback
  161. it 'should not produce an error', (done) ->
  162. @call (err) =>
  163. expect(err).to.not.be.instanceof Error
  164. expect(err).to.equal null
  165. done()
  166. it 'should not call the appropriate redis methods', (done) ->
  167. @call (err) =>
  168. @rclient.multi.callCount.should.equal 0
  169. @rclient.srem.callCount.should.equal 0
  170. @rclient.expire.callCount.should.equal 0
  171. @rclient.exec.callCount.should.equal 0
  172. done()
  173. it 'should not call _checkSessions', (done) ->
  174. @call (err) =>
  175. @_checkSessions.callCount.should.equal 0
  176. done()
  177. describe 'when no sessionId is supplied', ->
  178. beforeEach ->
  179. @call = (callback) =>
  180. @UserSessionsManager.untrackSession @user, null, callback
  181. it 'should not produce an error', (done) ->
  182. @call (err) =>
  183. expect(err).to.not.be.instanceof Error
  184. expect(err).to.equal null
  185. done()
  186. it 'should not call the appropriate redis methods', (done) ->
  187. @call (err) =>
  188. @rclient.multi.callCount.should.equal 0
  189. @rclient.srem.callCount.should.equal 0
  190. @rclient.expire.callCount.should.equal 0
  191. @rclient.exec.callCount.should.equal 0
  192. done()
  193. it 'should not call _checkSessions', (done) ->
  194. @call (err) =>
  195. @_checkSessions.callCount.should.equal 0
  196. done()