UserSessionsManagerTests.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 = ['sess:one', 'sess:two']
  204. @retain = []
  205. @rclient.smembers.callsArgWith(1, null, @sessionKeys)
  206. @rclient.exec.callsArgWith(0, null)
  207. @call = (callback) =>
  208. @UserSessionsManager.revokeAllUserSessions @user, @retain, callback
  209. it 'should not produce an error', (done) ->
  210. @call (err) =>
  211. expect(err).to.not.be.instanceof Error
  212. expect(err).to.equal null
  213. done()
  214. it 'should call the appropriate redis methods', (done) ->
  215. @call (err) =>
  216. @rclient.smembers.callCount.should.equal 1
  217. @rclient.multi.callCount.should.equal 1
  218. @rclient.del.callCount.should.equal 1
  219. expect(@rclient.del.firstCall.args[0]).to.deep.equal @sessionKeys
  220. @rclient.srem.callCount.should.equal 1
  221. expect(@rclient.srem.firstCall.args[1]).to.deep.equal @sessionKeys
  222. @rclient.exec.callCount.should.equal 1
  223. done()
  224. describe 'when a session is retained', ->
  225. beforeEach ->
  226. @sessionKeys = ['sess:one', 'sess:two', 'sess:three', 'sess:four']
  227. @retain = ['two']
  228. @rclient.smembers.callsArgWith(1, null, @sessionKeys)
  229. @rclient.exec.callsArgWith(0, null)
  230. @call = (callback) =>
  231. @UserSessionsManager.revokeAllUserSessions @user, @retain, callback
  232. it 'should not produce an error', (done) ->
  233. @call (err) =>
  234. expect(err).to.not.be.instanceof Error
  235. expect(err).to.equal null
  236. done()
  237. it 'should call the appropriate redis methods', (done) ->
  238. @call (err) =>
  239. @rclient.smembers.callCount.should.equal 1
  240. @rclient.multi.callCount.should.equal 1
  241. @rclient.del.callCount.should.equal 1
  242. @rclient.srem.callCount.should.equal 1
  243. @rclient.exec.callCount.should.equal 1
  244. done()
  245. it 'should remove all sessions except for the retained one', (done) ->
  246. @call (err) =>
  247. expect(@rclient.del.firstCall.args[0]).to.deep.equal(['sess:one', 'sess:three', 'sess:four'])
  248. expect(@rclient.srem.firstCall.args[1]).to.deep.equal(['sess:one', 'sess:three', 'sess:four'])
  249. done()
  250. describe 'when rclient produces an error', ->
  251. beforeEach ->
  252. @rclient.exec.callsArgWith(0, new Error('woops'))
  253. it 'should produce an error', (done) ->
  254. @call (err) =>
  255. expect(err).to.be.instanceof Error
  256. done()
  257. describe 'when no user is supplied', ->
  258. beforeEach ->
  259. @call = (callback) =>
  260. @UserSessionsManager.revokeAllUserSessions null, @retain, callback
  261. it 'should not produce an error', (done) ->
  262. @call (err) =>
  263. expect(err).to.not.be.instanceof Error
  264. expect(err).to.equal null
  265. done()
  266. it 'should not call the appropriate redis methods', (done) ->
  267. @call (err) =>
  268. @rclient.smembers.callCount.should.equal 0
  269. @rclient.multi.callCount.should.equal 0
  270. @rclient.del.callCount.should.equal 0
  271. @rclient.srem.callCount.should.equal 0
  272. @rclient.exec.callCount.should.equal 0
  273. done()
  274. describe 'when there are no keys to delete', ->
  275. beforeEach ->
  276. @rclient.smembers.callsArgWith(1, null, [])
  277. it 'should not produce an error', (done) ->
  278. @call (err) =>
  279. expect(err).to.not.be.instanceof Error
  280. expect(err).to.equal null
  281. done()
  282. it 'should not do the delete operation', (done) ->
  283. @call (err) =>
  284. @rclient.smembers.callCount.should.equal 1
  285. @rclient.multi.callCount.should.equal 0
  286. @rclient.del.callCount.should.equal 0
  287. @rclient.srem.callCount.should.equal 0
  288. @rclient.exec.callCount.should.equal 0
  289. done()
  290. describe 'touch', ->
  291. beforeEach ->
  292. @rclient.expire.callsArgWith(2, null)
  293. @call = (callback) =>
  294. @UserSessionsManager.touch @user, callback
  295. it 'should not produce an error', (done) ->
  296. @call (err) =>
  297. expect(err).to.not.be.instanceof Error
  298. expect(err).to.equal null
  299. done()
  300. it 'should call rclient.expire', (done) ->
  301. @call (err) =>
  302. @rclient.expire.callCount.should.equal 1
  303. done()
  304. describe 'when rclient produces an error', ->
  305. beforeEach ->
  306. @rclient.expire.callsArgWith(2, new Error('woops'))
  307. it 'should produce an error', (done) ->
  308. @call (err) =>
  309. expect(err).to.be.instanceof Error
  310. done()
  311. describe 'when no user is supplied', ->
  312. beforeEach ->
  313. @call = (callback) =>
  314. @UserSessionsManager.touch null, callback
  315. it 'should not produce an error', (done) ->
  316. @call (err) =>
  317. expect(err).to.not.be.instanceof Error
  318. expect(err).to.equal null
  319. done()
  320. it 'should not call expire', (done) ->
  321. @call (err) =>
  322. @rclient.expire.callCount.should.equal 0
  323. done()
  324. describe '_checkSessions', ->
  325. beforeEach ->
  326. @call = (callback) =>
  327. @UserSessionsManager._checkSessions @user, callback
  328. @sessionKeys = ['one', 'two']
  329. @rclient.smembers.callsArgWith(1, null, @sessionKeys)
  330. @rclient.get.callsArgWith(1, null, 'some-value')
  331. @rclient.srem.callsArgWith(2, null, {})
  332. it 'should not produce an error', (done) ->
  333. @call (err) =>
  334. expect(err).to.not.be.instanceof Error
  335. expect(err).to.equal undefined
  336. done()
  337. it 'should call the appropriate redis methods', (done) ->
  338. @call (err) =>
  339. @rclient.smembers.callCount.should.equal 1
  340. @rclient.get.callCount.should.equal 2
  341. @rclient.srem.callCount.should.equal 0
  342. done()
  343. describe 'when one of the keys is not present in redis', ->
  344. beforeEach ->
  345. @rclient.get.onCall(0).callsArgWith(1, null, 'some-val')
  346. @rclient.get.onCall(1).callsArgWith(1, null, null)
  347. it 'should not produce an error', (done) ->
  348. @call (err) =>
  349. expect(err).to.not.be.instanceof Error
  350. expect(err).to.equal undefined
  351. done()
  352. it 'should remove that key from the set', (done) ->
  353. @call (err) =>
  354. @rclient.smembers.callCount.should.equal 1
  355. @rclient.get.callCount.should.equal 2
  356. @rclient.srem.callCount.should.equal 1
  357. @rclient.srem.firstCall.args[1].should.equal 'two'
  358. done()
  359. describe 'when no user is supplied', ->
  360. beforeEach ->
  361. @call = (callback) =>
  362. @UserSessionsManager._checkSessions null, callback
  363. it 'should not produce an error', (done) ->
  364. @call (err) =>
  365. expect(err).to.not.be.instanceof Error
  366. expect(err).to.equal null
  367. done()
  368. it 'should not call redis methods', (done) ->
  369. @call (err) =>
  370. @rclient.smembers.callCount.should.equal 0
  371. @rclient.get.callCount.should.equal 0
  372. done()
  373. describe 'when one of the get operations produces an error', ->
  374. beforeEach ->
  375. @rclient.get.onCall(0).callsArgWith(1, new Error('woops'), null)
  376. @rclient.get.onCall(1).callsArgWith(1, null, null)
  377. it 'should produce an error', (done) ->
  378. @call (err) =>
  379. expect(err).to.be.instanceof Error
  380. done()
  381. it 'should call the right redis methods, bailing out early', (done) ->
  382. @call (err) =>
  383. @rclient.smembers.callCount.should.equal 1
  384. @rclient.get.callCount.should.equal 1
  385. @rclient.srem.callCount.should.equal 0
  386. done()