UserSessionsManagerTests.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../../app/src/Features/User/UserSessionsManager.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('UserSessionsManager', function() {
  21. beforeEach(function() {
  22. this.user = {
  23. _id: 'abcd',
  24. email: 'user@example.com'
  25. }
  26. this.sessionId = 'some_session_id'
  27. this.rclient = {
  28. multi: sinon.stub(),
  29. exec: sinon.stub(),
  30. get: sinon.stub(),
  31. del: sinon.stub(),
  32. sadd: sinon.stub(),
  33. srem: sinon.stub(),
  34. smembers: sinon.stub(),
  35. mget: sinon.stub(),
  36. pexpire: sinon.stub()
  37. }
  38. this.rclient.multi.returns(this.rclient)
  39. this.rclient.get.returns(this.rclient)
  40. this.rclient.del.returns(this.rclient)
  41. this.rclient.sadd.returns(this.rclient)
  42. this.rclient.srem.returns(this.rclient)
  43. this.rclient.smembers.returns(this.rclient)
  44. this.rclient.pexpire.returns(this.rclient)
  45. this.rclient.exec.callsArgWith(0, null)
  46. this.UserSessionsRedis = {
  47. client: () => this.rclient,
  48. sessionSetKey: user => `UserSessions:{${user._id}}`
  49. }
  50. this.logger = {
  51. err: sinon.stub(),
  52. error: sinon.stub(),
  53. warn: sinon.stub(),
  54. log: sinon.stub()
  55. }
  56. this.settings = {
  57. redis: {
  58. web: {}
  59. }
  60. }
  61. return (this.UserSessionsManager = SandboxedModule.require(modulePath, {
  62. globals: {
  63. console: console
  64. },
  65. requires: {
  66. 'logger-sharelatex': this.logger,
  67. 'settings-sharelatex': this.settings,
  68. './UserSessionsRedis': this.UserSessionsRedis
  69. }
  70. }))
  71. })
  72. describe('_sessionKey', function() {
  73. it('should build the correct key', function() {
  74. const result = this.UserSessionsManager._sessionKey(this.sessionId)
  75. return result.should.equal('sess:some_session_id')
  76. })
  77. })
  78. describe('trackSession', function() {
  79. beforeEach(function() {
  80. this.call = callback => {
  81. return this.UserSessionsManager.trackSession(
  82. this.user,
  83. this.sessionId,
  84. callback
  85. )
  86. }
  87. this.rclient.exec.callsArgWith(0, null)
  88. return (this._checkSessions = sinon
  89. .stub(this.UserSessionsManager, '_checkSessions')
  90. .returns(null))
  91. })
  92. afterEach(function() {
  93. return this._checkSessions.restore()
  94. })
  95. it('should not produce an error', function(done) {
  96. return this.call(err => {
  97. expect(err).to.not.be.instanceof(Error)
  98. return done()
  99. })
  100. })
  101. it('should call the appropriate redis methods', function(done) {
  102. return this.call(err => {
  103. this.rclient.multi.callCount.should.equal(1)
  104. this.rclient.sadd.callCount.should.equal(1)
  105. this.rclient.pexpire.callCount.should.equal(1)
  106. this.rclient.exec.callCount.should.equal(1)
  107. return done()
  108. })
  109. })
  110. it('should call _checkSessions', function(done) {
  111. return this.call(err => {
  112. this._checkSessions.callCount.should.equal(1)
  113. return done()
  114. })
  115. })
  116. describe('when rclient produces an error', function() {
  117. beforeEach(function() {
  118. return this.rclient.exec.callsArgWith(0, new Error('woops'))
  119. })
  120. it('should produce an error', function(done) {
  121. return this.call(err => {
  122. expect(err).to.be.instanceof(Error)
  123. return done()
  124. })
  125. })
  126. it('should not call _checkSessions', function(done) {
  127. return this.call(err => {
  128. this._checkSessions.callCount.should.equal(0)
  129. return done()
  130. })
  131. })
  132. })
  133. describe('when no user is supplied', function() {
  134. beforeEach(function() {
  135. return (this.call = callback => {
  136. return this.UserSessionsManager.trackSession(
  137. null,
  138. this.sessionId,
  139. callback
  140. )
  141. })
  142. })
  143. it('should not produce an error', function(done) {
  144. return this.call(err => {
  145. expect(err).to.not.be.instanceof(Error)
  146. expect(err).to.equal(null)
  147. return done()
  148. })
  149. })
  150. it('should not call the appropriate redis methods', function(done) {
  151. return this.call(err => {
  152. this.rclient.multi.callCount.should.equal(0)
  153. this.rclient.sadd.callCount.should.equal(0)
  154. this.rclient.pexpire.callCount.should.equal(0)
  155. this.rclient.exec.callCount.should.equal(0)
  156. return done()
  157. })
  158. })
  159. it('should not call _checkSessions', function(done) {
  160. return this.call(err => {
  161. this._checkSessions.callCount.should.equal(0)
  162. return done()
  163. })
  164. })
  165. })
  166. describe('when no sessionId is supplied', function() {
  167. beforeEach(function() {
  168. return (this.call = callback => {
  169. return this.UserSessionsManager.trackSession(
  170. this.user,
  171. null,
  172. callback
  173. )
  174. })
  175. })
  176. it('should not produce an error', function(done) {
  177. return this.call(err => {
  178. expect(err).to.not.be.instanceof(Error)
  179. expect(err).to.equal(null)
  180. return done()
  181. })
  182. })
  183. it('should not call the appropriate redis methods', function(done) {
  184. return this.call(err => {
  185. this.rclient.multi.callCount.should.equal(0)
  186. this.rclient.sadd.callCount.should.equal(0)
  187. this.rclient.pexpire.callCount.should.equal(0)
  188. this.rclient.exec.callCount.should.equal(0)
  189. return done()
  190. })
  191. })
  192. it('should not call _checkSessions', function(done) {
  193. return this.call(err => {
  194. this._checkSessions.callCount.should.equal(0)
  195. return done()
  196. })
  197. })
  198. })
  199. })
  200. describe('untrackSession', function() {
  201. beforeEach(function() {
  202. this.call = callback => {
  203. return this.UserSessionsManager.untrackSession(
  204. this.user,
  205. this.sessionId,
  206. callback
  207. )
  208. }
  209. this.rclient.exec.callsArgWith(0, null)
  210. return (this._checkSessions = sinon
  211. .stub(this.UserSessionsManager, '_checkSessions')
  212. .returns(null))
  213. })
  214. afterEach(function() {
  215. return this._checkSessions.restore()
  216. })
  217. it('should not produce an error', function(done) {
  218. return this.call(err => {
  219. expect(err).to.not.be.instanceof(Error)
  220. expect(err).to.equal(undefined)
  221. return done()
  222. })
  223. })
  224. it('should call the appropriate redis methods', function(done) {
  225. return this.call(err => {
  226. this.rclient.multi.callCount.should.equal(1)
  227. this.rclient.srem.callCount.should.equal(1)
  228. this.rclient.pexpire.callCount.should.equal(1)
  229. this.rclient.exec.callCount.should.equal(1)
  230. return done()
  231. })
  232. })
  233. it('should call _checkSessions', function(done) {
  234. return this.call(err => {
  235. this._checkSessions.callCount.should.equal(1)
  236. return done()
  237. })
  238. })
  239. describe('when rclient produces an error', function() {
  240. beforeEach(function() {
  241. return this.rclient.exec.callsArgWith(0, new Error('woops'))
  242. })
  243. it('should produce an error', function(done) {
  244. return this.call(err => {
  245. expect(err).to.be.instanceof(Error)
  246. return done()
  247. })
  248. })
  249. it('should not call _checkSessions', function(done) {
  250. return this.call(err => {
  251. this._checkSessions.callCount.should.equal(0)
  252. return done()
  253. })
  254. })
  255. })
  256. describe('when no user is supplied', function() {
  257. beforeEach(function() {
  258. return (this.call = callback => {
  259. return this.UserSessionsManager.untrackSession(
  260. null,
  261. this.sessionId,
  262. callback
  263. )
  264. })
  265. })
  266. it('should not produce an error', function(done) {
  267. return this.call(err => {
  268. expect(err).to.not.be.instanceof(Error)
  269. expect(err).to.equal(null)
  270. return done()
  271. })
  272. })
  273. it('should not call the appropriate redis methods', function(done) {
  274. return this.call(err => {
  275. this.rclient.multi.callCount.should.equal(0)
  276. this.rclient.srem.callCount.should.equal(0)
  277. this.rclient.pexpire.callCount.should.equal(0)
  278. this.rclient.exec.callCount.should.equal(0)
  279. return done()
  280. })
  281. })
  282. it('should not call _checkSessions', function(done) {
  283. return this.call(err => {
  284. this._checkSessions.callCount.should.equal(0)
  285. return done()
  286. })
  287. })
  288. })
  289. describe('when no sessionId is supplied', function() {
  290. beforeEach(function() {
  291. return (this.call = callback => {
  292. return this.UserSessionsManager.untrackSession(
  293. this.user,
  294. null,
  295. callback
  296. )
  297. })
  298. })
  299. it('should not produce an error', function(done) {
  300. return this.call(err => {
  301. expect(err).to.not.be.instanceof(Error)
  302. expect(err).to.equal(null)
  303. return done()
  304. })
  305. })
  306. it('should not call the appropriate redis methods', function(done) {
  307. return this.call(err => {
  308. this.rclient.multi.callCount.should.equal(0)
  309. this.rclient.srem.callCount.should.equal(0)
  310. this.rclient.pexpire.callCount.should.equal(0)
  311. this.rclient.exec.callCount.should.equal(0)
  312. return done()
  313. })
  314. })
  315. it('should not call _checkSessions', function(done) {
  316. return this.call(err => {
  317. this._checkSessions.callCount.should.equal(0)
  318. return done()
  319. })
  320. })
  321. })
  322. })
  323. describe('revokeAllUserSessions', function() {
  324. beforeEach(function() {
  325. this.sessionKeys = ['sess:one', 'sess:two']
  326. this.retain = []
  327. this.rclient.smembers.callsArgWith(1, null, this.sessionKeys)
  328. this.rclient.del = sinon.stub().callsArgWith(1, null)
  329. this.rclient.srem = sinon.stub().callsArgWith(2, null)
  330. return (this.call = callback => {
  331. return this.UserSessionsManager.revokeAllUserSessions(
  332. this.user,
  333. this.retain,
  334. callback
  335. )
  336. })
  337. })
  338. it('should not produce an error', function(done) {
  339. return this.call(err => {
  340. expect(err).to.not.be.instanceof(Error)
  341. expect(err).to.equal(null)
  342. return done()
  343. })
  344. })
  345. it('should call the appropriate redis methods', function(done) {
  346. return this.call(err => {
  347. this.rclient.smembers.callCount.should.equal(1)
  348. this.rclient.del.callCount.should.equal(2)
  349. expect(this.rclient.del.firstCall.args[0]).to.deep.equal(
  350. this.sessionKeys[0]
  351. )
  352. expect(this.rclient.del.secondCall.args[0]).to.deep.equal(
  353. this.sessionKeys[1]
  354. )
  355. this.rclient.srem.callCount.should.equal(1)
  356. expect(this.rclient.srem.firstCall.args[1]).to.deep.equal(
  357. this.sessionKeys
  358. )
  359. return done()
  360. })
  361. })
  362. describe('when a session is retained', function() {
  363. beforeEach(function() {
  364. this.sessionKeys = ['sess:one', 'sess:two', 'sess:three', 'sess:four']
  365. this.retain = ['two']
  366. this.rclient.smembers.callsArgWith(1, null, this.sessionKeys)
  367. this.rclient.del = sinon.stub().callsArgWith(1, null)
  368. return (this.call = callback => {
  369. return this.UserSessionsManager.revokeAllUserSessions(
  370. this.user,
  371. this.retain,
  372. callback
  373. )
  374. })
  375. })
  376. it('should not produce an error', function(done) {
  377. return this.call(err => {
  378. expect(err).to.not.be.instanceof(Error)
  379. expect(err).to.equal(null)
  380. return done()
  381. })
  382. })
  383. it('should call the appropriate redis methods', function(done) {
  384. return this.call(err => {
  385. this.rclient.smembers.callCount.should.equal(1)
  386. this.rclient.del.callCount.should.equal(this.sessionKeys.length - 1)
  387. this.rclient.srem.callCount.should.equal(1)
  388. return done()
  389. })
  390. })
  391. it('should remove all sessions except for the retained one', function(done) {
  392. return this.call(err => {
  393. expect(this.rclient.del.firstCall.args[0]).to.deep.equal('sess:one')
  394. expect(this.rclient.del.secondCall.args[0]).to.deep.equal(
  395. 'sess:three'
  396. )
  397. expect(this.rclient.del.thirdCall.args[0]).to.deep.equal('sess:four')
  398. expect(this.rclient.srem.firstCall.args[1]).to.deep.equal([
  399. 'sess:one',
  400. 'sess:three',
  401. 'sess:four'
  402. ])
  403. return done()
  404. })
  405. })
  406. })
  407. describe('when rclient produces an error', function() {
  408. beforeEach(function() {
  409. return (this.rclient.del = sinon
  410. .stub()
  411. .callsArgWith(1, new Error('woops')))
  412. })
  413. it('should produce an error', function(done) {
  414. return this.call(err => {
  415. expect(err).to.be.instanceof(Error)
  416. return done()
  417. })
  418. })
  419. it('should not call rclient.srem', function(done) {
  420. return this.call(err => {
  421. this.rclient.srem.callCount.should.equal(0)
  422. return done()
  423. })
  424. })
  425. })
  426. describe('when no user is supplied', function() {
  427. beforeEach(function() {
  428. return (this.call = callback => {
  429. return this.UserSessionsManager.revokeAllUserSessions(
  430. null,
  431. this.retain,
  432. callback
  433. )
  434. })
  435. })
  436. it('should not produce an error', function(done) {
  437. return this.call(err => {
  438. expect(err).to.not.be.instanceof(Error)
  439. expect(err).to.equal(null)
  440. return done()
  441. })
  442. })
  443. it('should not call the appropriate redis methods', function(done) {
  444. return this.call(err => {
  445. this.rclient.smembers.callCount.should.equal(0)
  446. this.rclient.del.callCount.should.equal(0)
  447. this.rclient.srem.callCount.should.equal(0)
  448. return done()
  449. })
  450. })
  451. })
  452. describe('when there are no keys to delete', function() {
  453. beforeEach(function() {
  454. return this.rclient.smembers.callsArgWith(1, null, [])
  455. })
  456. it('should not produce an error', function(done) {
  457. return this.call(err => {
  458. expect(err).to.not.be.instanceof(Error)
  459. expect(err).to.equal(null)
  460. return done()
  461. })
  462. })
  463. it('should not do the delete operation', function(done) {
  464. return this.call(err => {
  465. this.rclient.smembers.callCount.should.equal(1)
  466. this.rclient.del.callCount.should.equal(0)
  467. this.rclient.srem.callCount.should.equal(0)
  468. return done()
  469. })
  470. })
  471. })
  472. })
  473. describe('touch', function() {
  474. beforeEach(function() {
  475. this.rclient.pexpire.callsArgWith(2, null)
  476. return (this.call = callback => {
  477. return this.UserSessionsManager.touch(this.user, callback)
  478. })
  479. })
  480. it('should not produce an error', function(done) {
  481. return this.call(err => {
  482. expect(err).to.not.be.instanceof(Error)
  483. expect(err).to.equal(null)
  484. return done()
  485. })
  486. })
  487. it('should call rclient.pexpire', function(done) {
  488. return this.call(err => {
  489. this.rclient.pexpire.callCount.should.equal(1)
  490. return done()
  491. })
  492. })
  493. describe('when rclient produces an error', function() {
  494. beforeEach(function() {
  495. return this.rclient.pexpire.callsArgWith(2, new Error('woops'))
  496. })
  497. it('should produce an error', function(done) {
  498. return this.call(err => {
  499. expect(err).to.be.instanceof(Error)
  500. return done()
  501. })
  502. })
  503. })
  504. describe('when no user is supplied', function() {
  505. beforeEach(function() {
  506. return (this.call = callback => {
  507. return this.UserSessionsManager.touch(null, callback)
  508. })
  509. })
  510. it('should not produce an error', function(done) {
  511. return this.call(err => {
  512. expect(err).to.not.be.instanceof(Error)
  513. expect(err).to.equal(null)
  514. return done()
  515. })
  516. })
  517. it('should not call pexpire', function(done) {
  518. return this.call(err => {
  519. this.rclient.pexpire.callCount.should.equal(0)
  520. return done()
  521. })
  522. })
  523. })
  524. })
  525. describe('getAllUserSessions', function() {
  526. beforeEach(function() {
  527. this.sessionKeys = ['sess:one', 'sess:two', 'sess:three']
  528. this.sessions = [
  529. '{"user": {"ip_address": "a", "session_created": "b"}}',
  530. '{"passport": {"user": {"ip_address": "c", "session_created": "d"}}}'
  531. ]
  532. this.exclude = ['two']
  533. this.rclient.smembers.callsArgWith(1, null, this.sessionKeys)
  534. this.rclient.get = sinon.stub()
  535. this.rclient.get.onCall(0).callsArgWith(1, null, this.sessions[0])
  536. this.rclient.get.onCall(1).callsArgWith(1, null, this.sessions[1])
  537. return (this.call = callback => {
  538. return this.UserSessionsManager.getAllUserSessions(
  539. this.user,
  540. this.exclude,
  541. callback
  542. )
  543. })
  544. })
  545. it('should not produce an error', function(done) {
  546. return this.call((err, sessions) => {
  547. expect(err).to.equal(null)
  548. return done()
  549. })
  550. })
  551. it('should get sessions', function(done) {
  552. return this.call((err, sessions) => {
  553. expect(sessions).to.deep.equal([
  554. { ip_address: 'a', session_created: 'b' },
  555. { ip_address: 'c', session_created: 'd' }
  556. ])
  557. return done()
  558. })
  559. })
  560. it('should have called rclient.smembers', function(done) {
  561. return this.call((err, sessions) => {
  562. this.rclient.smembers.callCount.should.equal(1)
  563. return done()
  564. })
  565. })
  566. it('should have called rclient.get', function(done) {
  567. return this.call((err, sessions) => {
  568. this.rclient.get.callCount.should.equal(this.sessionKeys.length - 1)
  569. return done()
  570. })
  571. })
  572. describe('when there are no other sessions', function() {
  573. beforeEach(function() {
  574. this.sessionKeys = ['sess:two']
  575. return this.rclient.smembers.callsArgWith(1, null, this.sessionKeys)
  576. })
  577. it('should not produce an error', function(done) {
  578. return this.call((err, sessions) => {
  579. expect(err).to.equal(null)
  580. return done()
  581. })
  582. })
  583. it('should produce an empty list of sessions', function(done) {
  584. return this.call((err, sessions) => {
  585. expect(sessions).to.deep.equal([])
  586. return done()
  587. })
  588. })
  589. it('should have called rclient.smembers', function(done) {
  590. return this.call((err, sessions) => {
  591. this.rclient.smembers.callCount.should.equal(1)
  592. return done()
  593. })
  594. })
  595. it('should not have called rclient.mget', function(done) {
  596. return this.call((err, sessions) => {
  597. this.rclient.mget.callCount.should.equal(0)
  598. return done()
  599. })
  600. })
  601. })
  602. describe('when smembers produces an error', function() {
  603. beforeEach(function() {
  604. return this.rclient.smembers.callsArgWith(1, new Error('woops'))
  605. })
  606. it('should produce an error', function(done) {
  607. return this.call((err, sessions) => {
  608. expect(err).to.not.equal(null)
  609. expect(err).to.be.instanceof(Error)
  610. return done()
  611. })
  612. })
  613. it('should not have called rclient.mget', function(done) {
  614. return this.call((err, sessions) => {
  615. this.rclient.mget.callCount.should.equal(0)
  616. return done()
  617. })
  618. })
  619. })
  620. describe('when get produces an error', function() {
  621. beforeEach(function() {
  622. return (this.rclient.get = sinon
  623. .stub()
  624. .callsArgWith(1, new Error('woops')))
  625. })
  626. it('should produce an error', function(done) {
  627. return this.call((err, sessions) => {
  628. expect(err).to.not.equal(null)
  629. expect(err).to.be.instanceof(Error)
  630. return done()
  631. })
  632. })
  633. })
  634. })
  635. describe('_checkSessions', function() {
  636. beforeEach(function() {
  637. this.call = callback => {
  638. return this.UserSessionsManager._checkSessions(this.user, callback)
  639. }
  640. this.sessionKeys = ['one', 'two']
  641. this.rclient.smembers.callsArgWith(1, null, this.sessionKeys)
  642. this.rclient.get.callsArgWith(1, null, 'some-value')
  643. return this.rclient.srem.callsArgWith(2, null, {})
  644. })
  645. it('should not produce an error', function(done) {
  646. return this.call(err => {
  647. expect(err).to.not.be.instanceof(Error)
  648. expect(err).to.equal(undefined)
  649. return done()
  650. })
  651. })
  652. it('should call the appropriate redis methods', function(done) {
  653. return this.call(err => {
  654. this.rclient.smembers.callCount.should.equal(1)
  655. this.rclient.get.callCount.should.equal(2)
  656. this.rclient.srem.callCount.should.equal(0)
  657. return done()
  658. })
  659. })
  660. describe('when one of the keys is not present in redis', function() {
  661. beforeEach(function() {
  662. this.rclient.get.onCall(0).callsArgWith(1, null, 'some-val')
  663. return this.rclient.get.onCall(1).callsArgWith(1, null, null)
  664. })
  665. it('should not produce an error', function(done) {
  666. return this.call(err => {
  667. expect(err).to.not.be.instanceof(Error)
  668. expect(err).to.equal(undefined)
  669. return done()
  670. })
  671. })
  672. it('should remove that key from the set', function(done) {
  673. return this.call(err => {
  674. this.rclient.smembers.callCount.should.equal(1)
  675. this.rclient.get.callCount.should.equal(2)
  676. this.rclient.srem.callCount.should.equal(1)
  677. this.rclient.srem.firstCall.args[1].should.equal('two')
  678. return done()
  679. })
  680. })
  681. })
  682. describe('when no user is supplied', function() {
  683. beforeEach(function() {
  684. return (this.call = callback => {
  685. return this.UserSessionsManager._checkSessions(null, callback)
  686. })
  687. })
  688. it('should not produce an error', function(done) {
  689. return this.call(err => {
  690. expect(err).to.not.be.instanceof(Error)
  691. expect(err).to.equal(null)
  692. return done()
  693. })
  694. })
  695. it('should not call redis methods', function(done) {
  696. return this.call(err => {
  697. this.rclient.smembers.callCount.should.equal(0)
  698. this.rclient.get.callCount.should.equal(0)
  699. return done()
  700. })
  701. })
  702. })
  703. describe('when one of the get operations produces an error', function() {
  704. beforeEach(function() {
  705. this.rclient.get.onCall(0).callsArgWith(1, new Error('woops'), null)
  706. return this.rclient.get.onCall(1).callsArgWith(1, null, null)
  707. })
  708. it('should produce an error', function(done) {
  709. return this.call(err => {
  710. expect(err).to.be.instanceof(Error)
  711. return done()
  712. })
  713. })
  714. it('should call the right redis methods, bailing out early', function(done) {
  715. return this.call(err => {
  716. this.rclient.smembers.callCount.should.equal(1)
  717. this.rclient.get.callCount.should.equal(1)
  718. this.rclient.srem.callCount.should.equal(0)
  719. return done()
  720. })
  721. })
  722. })
  723. })
  724. })