UserSessionsManager.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. del: sinon.stub()
  18. sadd: sinon.stub()
  19. srem: sinon.stub()
  20. smembers: sinon.stub()
  21. expire: sinon.stub()
  22. @rclient.multi.returns(@rclient)
  23. @rclient.get.returns(@rclient)
  24. @rclient.del.returns(@rclient)
  25. @rclient.sadd.returns(@rclient)
  26. @rclient.srem.returns(@rclient)
  27. @rclient.smembers.returns(@rclient)
  28. @rclient.expire.returns(@rclient)
  29. @rclient.exec.callsArgWith(0, null)
  30. @redis =
  31. createClient: () => @rclient
  32. @logger =
  33. err: sinon.stub()
  34. error: sinon.stub()
  35. log: sinon.stub()
  36. @settings =
  37. redis:
  38. web: {}
  39. @UserSessionsManager = SandboxedModule.require modulePath, requires:
  40. "redis-sharelatex": @redis
  41. "logger-sharelatex": @logger
  42. "settings-sharelatex": @settings
  43. describe '_sessionSetKey', ->
  44. it 'should build the correct key', ->
  45. result = @UserSessionsManager._sessionSetKey(@user)
  46. result.should.equal 'UserSessions:abcd'
  47. describe '_sessionKey', ->
  48. it 'should build the correct key', ->
  49. result = @UserSessionsManager._sessionKey(@sessionId)
  50. result.should.equal 'sess:some_session_id'
  51. describe 'trackSession', ->
  52. beforeEach ->
  53. @call = (callback) =>
  54. @UserSessionsManager.trackSession @user, @sessionId, callback
  55. @rclient.exec.callsArgWith(0, null)
  56. @_checkSessions = sinon.stub(@UserSessionsManager, '_checkSessions').returns(null)
  57. afterEach ->
  58. @_checkSessions.restore()
  59. it 'should not produce an error', (done) ->
  60. @call (err) =>
  61. expect(err).to.not.be.instanceof Error
  62. done()
  63. it 'should call the appropriate redis methods', (done) ->
  64. @call (err) =>
  65. @rclient.multi.callCount.should.equal 1
  66. @rclient.sadd.callCount.should.equal 1
  67. @rclient.expire.callCount.should.equal 1
  68. @rclient.exec.callCount.should.equal 1
  69. done()
  70. it 'should call _checkSessions', (done) ->
  71. @call (err) =>
  72. @_checkSessions.callCount.should.equal 1
  73. done()
  74. describe 'when rclient produces an error', ->
  75. beforeEach ->
  76. @rclient.exec.callsArgWith(0, new Error('woops'))
  77. it 'should produce an error', (done) ->
  78. @call (err) =>
  79. expect(err).to.be.instanceof Error
  80. done()
  81. it 'should not call _checkSessions', (done) ->
  82. @call (err) =>
  83. @_checkSessions.callCount.should.equal 0
  84. done()
  85. describe 'when no user is supplied', ->
  86. beforeEach ->
  87. @call = (callback) =>
  88. @UserSessionsManager.trackSession null, @sessionId, callback
  89. it 'should not produce an error', (done) ->
  90. @call (err) =>
  91. expect(err).to.not.be.instanceof Error
  92. expect(err).to.equal null
  93. done()
  94. it 'should not call the appropriate redis methods', (done) ->
  95. @call (err) =>
  96. @rclient.multi.callCount.should.equal 0
  97. @rclient.sadd.callCount.should.equal 0
  98. @rclient.expire.callCount.should.equal 0
  99. @rclient.exec.callCount.should.equal 0
  100. done()
  101. it 'should not call _checkSessions', (done) ->
  102. @call (err) =>
  103. @_checkSessions.callCount.should.equal 0
  104. done()
  105. describe 'when no sessionId is supplied', ->
  106. beforeEach ->
  107. @call = (callback) =>
  108. @UserSessionsManager.trackSession @user, null, callback
  109. it 'should not produce an error', (done) ->
  110. @call (err) =>
  111. expect(err).to.not.be.instanceof Error
  112. expect(err).to.equal null
  113. done()
  114. it 'should not call the appropriate redis methods', (done) ->
  115. @call (err) =>
  116. @rclient.multi.callCount.should.equal 0
  117. @rclient.sadd.callCount.should.equal 0
  118. @rclient.expire.callCount.should.equal 0
  119. @rclient.exec.callCount.should.equal 0
  120. done()
  121. it 'should not call _checkSessions', (done) ->
  122. @call (err) =>
  123. @_checkSessions.callCount.should.equal 0
  124. done()
  125. describe 'untrackSession', ->
  126. beforeEach ->
  127. @call = (callback) =>
  128. @UserSessionsManager.untrackSession @user, @sessionId, callback
  129. @rclient.exec.callsArgWith(0, null)
  130. @_checkSessions = sinon.stub(@UserSessionsManager, '_checkSessions').returns(null)
  131. afterEach ->
  132. @_checkSessions.restore()
  133. it 'should not produce an error', (done) ->
  134. @call (err) =>
  135. expect(err).to.not.be.instanceof Error
  136. expect(err).to.equal undefined
  137. done()
  138. it 'should call the appropriate redis methods', (done) ->
  139. @call (err) =>
  140. @rclient.multi.callCount.should.equal 1
  141. @rclient.srem.callCount.should.equal 1
  142. @rclient.expire.callCount.should.equal 1
  143. @rclient.exec.callCount.should.equal 1
  144. done()
  145. it 'should call _checkSessions', (done) ->
  146. @call (err) =>
  147. @_checkSessions.callCount.should.equal 1
  148. done()
  149. describe 'when rclient produces an error', ->
  150. beforeEach ->
  151. @rclient.exec.callsArgWith(0, new Error('woops'))
  152. it 'should produce an error', (done) ->
  153. @call (err) =>
  154. expect(err).to.be.instanceof Error
  155. done()
  156. it 'should not call _checkSessions', (done) ->
  157. @call (err) =>
  158. @_checkSessions.callCount.should.equal 0
  159. done()
  160. describe 'when no user is supplied', ->
  161. beforeEach ->
  162. @call = (callback) =>
  163. @UserSessionsManager.untrackSession null, @sessionId, callback
  164. it 'should not produce an error', (done) ->
  165. @call (err) =>
  166. expect(err).to.not.be.instanceof Error
  167. expect(err).to.equal null
  168. done()
  169. it 'should not call the appropriate redis methods', (done) ->
  170. @call (err) =>
  171. @rclient.multi.callCount.should.equal 0
  172. @rclient.srem.callCount.should.equal 0
  173. @rclient.expire.callCount.should.equal 0
  174. @rclient.exec.callCount.should.equal 0
  175. done()
  176. it 'should not call _checkSessions', (done) ->
  177. @call (err) =>
  178. @_checkSessions.callCount.should.equal 0
  179. done()
  180. describe 'when no sessionId is supplied', ->
  181. beforeEach ->
  182. @call = (callback) =>
  183. @UserSessionsManager.untrackSession @user, null, callback
  184. it 'should not produce an error', (done) ->
  185. @call (err) =>
  186. expect(err).to.not.be.instanceof Error
  187. expect(err).to.equal null
  188. done()
  189. it 'should not call the appropriate redis methods', (done) ->
  190. @call (err) =>
  191. @rclient.multi.callCount.should.equal 0
  192. @rclient.srem.callCount.should.equal 0
  193. @rclient.expire.callCount.should.equal 0
  194. @rclient.exec.callCount.should.equal 0
  195. done()
  196. it 'should not call _checkSessions', (done) ->
  197. @call (err) =>
  198. @_checkSessions.callCount.should.equal 0
  199. done()
  200. ##
  201. describe 'revokeAllUserSessions', ->
  202. beforeEach ->
  203. @sessionKeys = ['one', 'two']
  204. @rclient.smembers.callsArgWith(1, null, @sessionKeys)
  205. @rclient.exec.callsArgWith(0, null)
  206. @call = (callback) =>
  207. @UserSessionsManager.revokeAllUserSessions @user, [], callback
  208. it 'should not produce an error', (done) ->
  209. @call (err) =>
  210. expect(err).to.not.be.instanceof Error
  211. expect(err).to.equal null
  212. done()
  213. it 'should call the appropriate redis methods', (done) ->
  214. @call (err) =>
  215. @rclient.smembers.callCount.should.equal 1
  216. @rclient.multi.callCount.should.equal 1
  217. @rclient.del.callCount.should.equal 1
  218. expect(@rclient.del.firstCall.args[0]).to.deep.equal @sessionKeys
  219. @rclient.srem.callCount.should.equal 1
  220. expect(@rclient.srem.firstCall.args[1]).to.deep.equal @sessionKeys
  221. @rclient.exec.callCount.should.equal 1
  222. done()
  223. describe 'when rclient produces an error', ->
  224. beforeEach ->
  225. @rclient.exec.callsArgWith(0, new Error('woops'))
  226. it 'should produce an error', (done) ->
  227. @call (err) =>
  228. expect(err).to.be.instanceof Error
  229. done()
  230. describe 'when no user is supplied', ->
  231. beforeEach ->
  232. @call = (callback) =>
  233. @UserSessionsManager.revokeAllUserSessions null, [], callback
  234. it 'should not produce an error', (done) ->
  235. @call (err) =>
  236. expect(err).to.not.be.instanceof Error
  237. expect(err).to.equal null
  238. done()
  239. it 'should not call the appropriate redis methods', (done) ->
  240. @call (err) =>
  241. @rclient.smembers.callCount.should.equal 0
  242. @rclient.multi.callCount.should.equal 0
  243. @rclient.del.callCount.should.equal 0
  244. @rclient.srem.callCount.should.equal 0
  245. @rclient.exec.callCount.should.equal 0
  246. done()
  247. describe 'touch', ->
  248. beforeEach ->
  249. @rclient.expire.callsArgWith(2, null)
  250. @call = (callback) =>
  251. @UserSessionsManager.touch @user, callback
  252. it 'should not produce an error', (done) ->
  253. @call (err) =>
  254. expect(err).to.not.be.instanceof Error
  255. expect(err).to.equal null
  256. done()
  257. it 'should call rclient.expire', (done) ->
  258. @call (err) =>
  259. @rclient.expire.callCount.should.equal 1
  260. done()
  261. describe 'when rclient produces an error', ->
  262. beforeEach ->
  263. @rclient.expire.callsArgWith(2, new Error('woops'))
  264. it 'should produce an error', (done) ->
  265. @call (err) =>
  266. expect(err).to.be.instanceof Error
  267. done()
  268. describe 'when no user is supplied', ->
  269. beforeEach ->
  270. @call = (callback) =>
  271. @UserSessionsManager.touch null, callback
  272. it 'should not produce an error', (done) ->
  273. @call (err) =>
  274. expect(err).to.not.be.instanceof Error
  275. expect(err).to.equal null
  276. done()
  277. it 'should not call expire', (done) ->
  278. @call (err) =>
  279. @rclient.expire.callCount.should.equal 0
  280. done()
  281. describe '_checkSessions', ->
  282. beforeEach ->
  283. @call = (callback) =>
  284. @UserSessionsManager._checkSessions @user, callback
  285. @sessionKeys = ['one', 'two']
  286. @rclient.smembers.callsArgWith(1, null, @sessionKeys)
  287. @rclient.get.callsArgWith(1, null, 'some-value')
  288. @rclient.srem.callsArgWith(2, null, {})
  289. it 'should not produce an error', (done) ->
  290. @call (err) =>
  291. expect(err).to.not.be.instanceof Error
  292. expect(err).to.equal undefined
  293. done()
  294. it 'should call the appropriate redis methods', (done) ->
  295. @call (err) =>
  296. @rclient.smembers.callCount.should.equal 1
  297. @rclient.get.callCount.should.equal 2
  298. @rclient.srem.callCount.should.equal 0
  299. done()
  300. describe 'when one of the keys is not present in redis', ->
  301. beforeEach ->
  302. @rclient.get.onCall(0).callsArgWith(1, null, 'some-val')
  303. @rclient.get.onCall(1).callsArgWith(1, null, null)
  304. it 'should not produce an error', (done) ->
  305. @call (err) =>
  306. expect(err).to.not.be.instanceof Error
  307. expect(err).to.equal undefined
  308. done()
  309. it 'should remove that key from the set', (done) ->
  310. @call (err) =>
  311. @rclient.smembers.callCount.should.equal 1
  312. @rclient.get.callCount.should.equal 2
  313. @rclient.srem.callCount.should.equal 1
  314. @rclient.srem.firstCall.args[1].should.equal 'two'
  315. done()
  316. describe 'when no user is supplied', ->
  317. beforeEach ->
  318. @call = (callback) =>
  319. @UserSessionsManager._checkSessions null, callback
  320. it 'should not produce an error', (done) ->
  321. @call (err) =>
  322. expect(err).to.not.be.instanceof Error
  323. expect(err).to.equal null
  324. done()
  325. it 'should not call redis methods', (done) ->
  326. @call (err) =>
  327. @rclient.smembers.callCount.should.equal 0
  328. @rclient.get.callCount.should.equal 0
  329. done()
  330. describe 'when one of the get operations produces an error', ->
  331. beforeEach ->
  332. @rclient.get.onCall(0).callsArgWith(1, new Error('woops'), null)
  333. @rclient.get.onCall(1).callsArgWith(1, null, null)
  334. it 'should produce an error', (done) ->
  335. @call (err) =>
  336. expect(err).to.be.instanceof Error
  337. done()
  338. it 'should call the right redis methods, bailing out early', (done) ->
  339. @call (err) =>
  340. @rclient.smembers.callCount.should.equal 1
  341. @rclient.get.callCount.should.equal 1
  342. @rclient.srem.callCount.should.equal 0
  343. done()