UserSessionsManager.test.mjs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/Features/User/UserSessionsManager.mjs'
  4. describe('UserSessionsManager', function () {
  5. beforeEach(async function (ctx) {
  6. ctx.user = {
  7. _id: 'abcd',
  8. email: 'user@example.com',
  9. }
  10. ctx.sessionId = 'some_session_id'
  11. ctx.rclient = {
  12. multi: sinon.stub(),
  13. exec: sinon.stub(),
  14. get: sinon.stub(),
  15. del: sinon.stub(),
  16. sadd: sinon.stub(),
  17. srem: sinon.stub(),
  18. smembers: sinon.stub(),
  19. mget: sinon.stub(),
  20. pexpire: sinon.stub(),
  21. }
  22. ctx.rclient.multi.returns({
  23. sadd: sinon.stub().returnsThis(),
  24. srem: sinon.stub().returnsThis(),
  25. pexpire: sinon.stub().returnsThis(),
  26. exec: sinon.stub().resolves(),
  27. })
  28. ctx.rclient.get.resolves()
  29. ctx.rclient.del.resolves()
  30. ctx.rclient.sadd.resolves()
  31. ctx.rclient.srem.resolves()
  32. ctx.rclient.smembers.resolves([])
  33. ctx.rclient.pexpire.resolves()
  34. ctx.UserSessionsRedis = {
  35. client: () => ctx.rclient,
  36. sessionSetKey: user => `UserSessions:{${user._id}}`,
  37. }
  38. ctx.settings = {
  39. redis: {
  40. web: {},
  41. },
  42. }
  43. vi.doMock('@overleaf/settings', () => ({
  44. default: ctx.settings,
  45. }))
  46. vi.doMock('../../../../app/src/Features/User/UserSessionsRedis', () => ({
  47. default: ctx.UserSessionsRedis,
  48. }))
  49. return (ctx.UserSessionsManager = (await import(modulePath)).default)
  50. })
  51. describe('_sessionKey', function () {
  52. it('should build the correct key', function (ctx) {
  53. const result = ctx.UserSessionsManager._sessionKey(ctx.sessionId)
  54. return result.should.equal('sess:some_session_id')
  55. })
  56. })
  57. describe('trackSession', function () {
  58. beforeEach(function (ctx) {
  59. ctx._checkSessions = sinon
  60. .stub(ctx.UserSessionsManager.promises, '_checkSessions')
  61. .resolves()
  62. })
  63. afterEach(function (ctx) {
  64. return ctx._checkSessions.restore()
  65. })
  66. it('should not produce an error', async function (ctx) {
  67. await ctx.UserSessionsManager.promises.trackSession(
  68. ctx.user,
  69. ctx.sessionId
  70. )
  71. })
  72. it('should call the appropriate redis methods', async function (ctx) {
  73. await ctx.UserSessionsManager.promises.trackSession(
  74. ctx.user,
  75. ctx.sessionId
  76. )
  77. ctx.rclient.multi.callCount.should.equal(1)
  78. const multiInstance = ctx.rclient.multi.returnValues[0]
  79. multiInstance.sadd.callCount.should.equal(1)
  80. multiInstance.pexpire.callCount.should.equal(1)
  81. multiInstance.exec.callCount.should.equal(1)
  82. })
  83. it('should call _checkSessions', async function (ctx) {
  84. await ctx.UserSessionsManager.promises.trackSession(
  85. ctx.user,
  86. ctx.sessionId
  87. )
  88. ctx._checkSessions.callCount.should.equal(1)
  89. })
  90. describe('when rclient produces an error', function () {
  91. beforeEach(function (ctx) {
  92. ctx.rclient.multi.returns({
  93. sadd: sinon.stub().returnsThis(),
  94. pexpire: sinon.stub().returnsThis(),
  95. exec: sinon.stub().rejects(new Error('woops')),
  96. })
  97. })
  98. it('should produce an error', async function (ctx) {
  99. await expect(
  100. ctx.UserSessionsManager.promises.trackSession(ctx.user, ctx.sessionId)
  101. ).to.be.rejectedWith(Error)
  102. })
  103. it('should not call _checkSessions', async function (ctx) {
  104. try {
  105. await ctx.UserSessionsManager.promises.trackSession(
  106. ctx.user,
  107. ctx.sessionId
  108. )
  109. } catch (err) {
  110. // Expected error
  111. }
  112. ctx._checkSessions.callCount.should.equal(0)
  113. })
  114. })
  115. describe('when no user is supplied', function () {
  116. it('should not produce an error', async function (ctx) {
  117. await ctx.UserSessionsManager.promises.trackSession(null, ctx.sessionId)
  118. })
  119. it('should not call the appropriate redis methods', async function (ctx) {
  120. await ctx.UserSessionsManager.promises.trackSession(null, ctx.sessionId)
  121. ctx.rclient.multi.callCount.should.equal(0)
  122. })
  123. it('should not call _checkSessions', async function (ctx) {
  124. await ctx.UserSessionsManager.promises.trackSession(null, ctx.sessionId)
  125. ctx._checkSessions.callCount.should.equal(0)
  126. })
  127. })
  128. describe('when no sessionId is supplied', function () {
  129. it('should not produce an error', async function (ctx) {
  130. await ctx.UserSessionsManager.promises.trackSession(ctx.user, null)
  131. })
  132. it('should not call the appropriate redis methods', async function (ctx) {
  133. await ctx.UserSessionsManager.promises.trackSession(ctx.user, null)
  134. ctx.rclient.multi.callCount.should.equal(0)
  135. })
  136. it('should not call _checkSessions', async function (ctx) {
  137. await ctx.UserSessionsManager.promises.trackSession(ctx.user, null)
  138. ctx._checkSessions.callCount.should.equal(0)
  139. })
  140. })
  141. })
  142. describe('untrackSession', function () {
  143. beforeEach(function (ctx) {
  144. ctx._checkSessions = sinon
  145. .stub(ctx.UserSessionsManager.promises, '_checkSessions')
  146. .resolves()
  147. })
  148. afterEach(function (ctx) {
  149. return ctx._checkSessions.restore()
  150. })
  151. it('should not produce an error', async function (ctx) {
  152. await ctx.UserSessionsManager.promises.untrackSession(
  153. ctx.user,
  154. ctx.sessionId
  155. )
  156. })
  157. it('should call the appropriate redis methods', async function (ctx) {
  158. await ctx.UserSessionsManager.promises.untrackSession(
  159. ctx.user,
  160. ctx.sessionId
  161. )
  162. ctx.rclient.multi.callCount.should.equal(1)
  163. const multiInstance = ctx.rclient.multi.returnValues[0]
  164. multiInstance.srem.callCount.should.equal(1)
  165. multiInstance.pexpire.callCount.should.equal(1)
  166. multiInstance.exec.callCount.should.equal(1)
  167. })
  168. it('should call _checkSessions', async function (ctx) {
  169. await ctx.UserSessionsManager.promises.untrackSession(
  170. ctx.user,
  171. ctx.sessionId
  172. )
  173. ctx._checkSessions.callCount.should.equal(1)
  174. })
  175. describe('when rclient produces an error', function () {
  176. beforeEach(function (ctx) {
  177. ctx.rclient.multi.returns({
  178. srem: sinon.stub().returnsThis(),
  179. pexpire: sinon.stub().returnsThis(),
  180. exec: sinon.stub().rejects(new Error('woops')),
  181. })
  182. })
  183. it('should produce an error', async function (ctx) {
  184. await expect(
  185. ctx.UserSessionsManager.promises.untrackSession(
  186. ctx.user,
  187. ctx.sessionId
  188. )
  189. ).to.be.rejectedWith(Error)
  190. })
  191. it('should not call _checkSessions', async function (ctx) {
  192. try {
  193. await ctx.UserSessionsManager.promises.untrackSession(
  194. ctx.user,
  195. ctx.sessionId
  196. )
  197. } catch (err) {
  198. // Expected error
  199. }
  200. ctx._checkSessions.callCount.should.equal(0)
  201. })
  202. })
  203. describe('when no user is supplied', function () {
  204. it('should not produce an error', async function (ctx) {
  205. await ctx.UserSessionsManager.promises.untrackSession(
  206. null,
  207. ctx.sessionId
  208. )
  209. })
  210. it('should not call the appropriate redis methods', async function (ctx) {
  211. await ctx.UserSessionsManager.promises.untrackSession(
  212. null,
  213. ctx.sessionId
  214. )
  215. ctx.rclient.multi.callCount.should.equal(0)
  216. })
  217. it('should not call _checkSessions', async function (ctx) {
  218. await ctx.UserSessionsManager.promises.untrackSession(
  219. null,
  220. ctx.sessionId
  221. )
  222. ctx._checkSessions.callCount.should.equal(0)
  223. })
  224. })
  225. describe('when no sessionId is supplied', function () {
  226. it('should not produce an error', async function (ctx) {
  227. await ctx.UserSessionsManager.promises.untrackSession(ctx.user, null)
  228. })
  229. it('should not call the appropriate redis methods', async function (ctx) {
  230. await ctx.UserSessionsManager.promises.untrackSession(ctx.user, null)
  231. ctx.rclient.multi.callCount.should.equal(0)
  232. })
  233. it('should not call _checkSessions', async function (ctx) {
  234. await ctx.UserSessionsManager.promises.untrackSession(ctx.user, null)
  235. ctx._checkSessions.callCount.should.equal(0)
  236. })
  237. })
  238. })
  239. describe('removeSessionsFromRedis', function () {
  240. beforeEach(function (ctx) {
  241. ctx.sessionKeys = ['sess:one', 'sess:two']
  242. ctx.currentSessionID = undefined
  243. ctx.rclient.smembers.resolves(ctx.sessionKeys)
  244. ctx.rclient.del.resolves()
  245. ctx.rclient.srem.resolves()
  246. })
  247. it('should not produce an error', async function (ctx) {
  248. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  249. ctx.user,
  250. ctx.currentSessionID
  251. )
  252. })
  253. it('should yield the number of purged sessions', async function (ctx) {
  254. const result =
  255. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  256. ctx.user,
  257. ctx.currentSessionID
  258. )
  259. expect(result).to.equal(ctx.sessionKeys.length)
  260. })
  261. it('should call the appropriate redis methods', async function (ctx) {
  262. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  263. ctx.user,
  264. ctx.currentSessionID
  265. )
  266. ctx.rclient.smembers.callCount.should.equal(1)
  267. ctx.rclient.del.callCount.should.equal(2)
  268. expect(ctx.rclient.del.firstCall.args[0]).to.deep.equal(
  269. ctx.sessionKeys[0]
  270. )
  271. expect(ctx.rclient.del.secondCall.args[0]).to.deep.equal(
  272. ctx.sessionKeys[1]
  273. )
  274. ctx.rclient.srem.callCount.should.equal(1)
  275. expect(ctx.rclient.srem.firstCall.args[0]).to.deep.equal(
  276. 'UserSessions:{abcd}'
  277. )
  278. expect(ctx.rclient.srem.firstCall.args[1]).to.deep.equal(ctx.sessionKeys)
  279. })
  280. describe('when a session is retained', function () {
  281. beforeEach(function (ctx) {
  282. ctx.sessionKeys = ['sess:one', 'sess:two', 'sess:three', 'sess:four']
  283. ctx.currentSessionID = 'two'
  284. ctx.rclient.smembers.resolves(ctx.sessionKeys)
  285. ctx.rclient.del.resolves()
  286. })
  287. it('should not produce an error', async function (ctx) {
  288. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  289. ctx.user,
  290. ctx.currentSessionID
  291. )
  292. })
  293. it('should call the appropriate redis methods', async function (ctx) {
  294. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  295. ctx.user,
  296. ctx.currentSessionID
  297. )
  298. ctx.rclient.smembers.callCount.should.equal(1)
  299. ctx.rclient.del.callCount.should.equal(ctx.sessionKeys.length - 1)
  300. ctx.rclient.srem.callCount.should.equal(1)
  301. })
  302. it('should remove all sessions except for the retained one', async function (ctx) {
  303. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  304. ctx.user,
  305. ctx.currentSessionID
  306. )
  307. expect(ctx.rclient.del.firstCall.args[0]).to.deep.equal('sess:one')
  308. expect(ctx.rclient.del.secondCall.args[0]).to.deep.equal('sess:three')
  309. expect(ctx.rclient.del.thirdCall.args[0]).to.deep.equal('sess:four')
  310. expect(ctx.rclient.srem.firstCall.args[1]).to.deep.equal([
  311. 'sess:one',
  312. 'sess:three',
  313. 'sess:four',
  314. ])
  315. })
  316. })
  317. describe('when rclient produces an error', function () {
  318. beforeEach(function (ctx) {
  319. ctx.rclient.del.rejects(new Error('woops'))
  320. })
  321. it('should produce an error', async function (ctx) {
  322. await expect(
  323. ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  324. ctx.user,
  325. ctx.currentSessionID
  326. )
  327. ).to.be.rejectedWith(Error)
  328. })
  329. it('should not call rclient.srem', async function (ctx) {
  330. try {
  331. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  332. ctx.user,
  333. ctx.currentSessionID
  334. )
  335. } catch (err) {
  336. // Expected error
  337. }
  338. ctx.rclient.srem.callCount.should.equal(0)
  339. })
  340. })
  341. describe('when no user is supplied', function () {
  342. it('should produce an error', async function (ctx) {
  343. await expect(
  344. ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  345. null,
  346. ctx.currentSessionID
  347. )
  348. ).to.be.rejectedWith(/bug: user not passed to removeSessionsFromRedis/)
  349. })
  350. it('should not call the appropriate redis methods', async function (ctx) {
  351. try {
  352. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  353. null,
  354. ctx.currentSessionID
  355. )
  356. } catch (err) {
  357. // Expected error
  358. }
  359. ctx.rclient.smembers.callCount.should.equal(0)
  360. ctx.rclient.del.callCount.should.equal(0)
  361. ctx.rclient.srem.callCount.should.equal(0)
  362. })
  363. })
  364. describe('when there are no keys to delete', function () {
  365. beforeEach(function (ctx) {
  366. ctx.rclient.smembers.resolves([])
  367. })
  368. it('should not produce an error', async function (ctx) {
  369. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  370. ctx.user,
  371. ctx.currentSessionID
  372. )
  373. })
  374. it('should not do the delete operation', async function (ctx) {
  375. await ctx.UserSessionsManager.promises.removeSessionsFromRedis(
  376. ctx.user,
  377. ctx.currentSessionID
  378. )
  379. ctx.rclient.smembers.callCount.should.equal(1)
  380. ctx.rclient.del.callCount.should.equal(0)
  381. ctx.rclient.srem.callCount.should.equal(0)
  382. })
  383. })
  384. })
  385. describe('touch', function () {
  386. it('should not produce an error', async function (ctx) {
  387. await ctx.UserSessionsManager.promises.touch(ctx.user)
  388. })
  389. it('should call rclient.pexpire', async function (ctx) {
  390. await ctx.UserSessionsManager.promises.touch(ctx.user)
  391. ctx.rclient.pexpire.callCount.should.equal(1)
  392. })
  393. describe('when rclient produces an error', function () {
  394. beforeEach(function (ctx) {
  395. ctx.rclient.pexpire.rejects(new Error('woops'))
  396. })
  397. it('should produce an error', async function (ctx) {
  398. await expect(
  399. ctx.UserSessionsManager.promises.touch(ctx.user)
  400. ).to.be.rejectedWith(Error)
  401. })
  402. })
  403. describe('when no user is supplied', function () {
  404. it('should not produce an error', async function (ctx) {
  405. await ctx.UserSessionsManager.promises.touch(null)
  406. })
  407. it('should not call pexpire', async function (ctx) {
  408. await ctx.UserSessionsManager.promises.touch(null)
  409. ctx.rclient.pexpire.callCount.should.equal(0)
  410. })
  411. })
  412. })
  413. describe('getAllUserSessions', function () {
  414. beforeEach(function (ctx) {
  415. ctx.sessionKeys = ['sess:one', 'sess:two', 'sess:three']
  416. ctx.sessions = [
  417. '{"user": {"ip_address": "a", "session_created": "b"}}',
  418. '{"passport": {"user": {"ip_address": "c", "session_created": "d"}}}',
  419. ]
  420. ctx.exclude = ['two']
  421. ctx.rclient.smembers.resolves(ctx.sessionKeys)
  422. ctx.rclient.get = sinon.stub()
  423. ctx.rclient.get.onCall(0).resolves(ctx.sessions[0])
  424. ctx.rclient.get.onCall(1).resolves(ctx.sessions[1])
  425. })
  426. it('should not produce an error', async function (ctx) {
  427. await ctx.UserSessionsManager.promises.getAllUserSessions(
  428. ctx.user,
  429. ctx.exclude
  430. )
  431. })
  432. it('should get sessions', async function (ctx) {
  433. const sessions =
  434. await ctx.UserSessionsManager.promises.getAllUserSessions(
  435. ctx.user,
  436. ctx.exclude
  437. )
  438. expect(sessions).to.deep.equal([
  439. { ip_address: 'a', session_created: 'b' },
  440. { ip_address: 'c', session_created: 'd' },
  441. ])
  442. })
  443. it('should have called rclient.smembers', async function (ctx) {
  444. await ctx.UserSessionsManager.promises.getAllUserSessions(
  445. ctx.user,
  446. ctx.exclude
  447. )
  448. ctx.rclient.smembers.callCount.should.equal(1)
  449. })
  450. it('should have called rclient.get', async function (ctx) {
  451. await ctx.UserSessionsManager.promises.getAllUserSessions(
  452. ctx.user,
  453. ctx.exclude
  454. )
  455. ctx.rclient.get.callCount.should.equal(ctx.sessionKeys.length - 1)
  456. })
  457. describe('when there are no other sessions', function () {
  458. beforeEach(function (ctx) {
  459. ctx.sessionKeys = ['sess:two']
  460. ctx.rclient.smembers.resolves(ctx.sessionKeys)
  461. })
  462. it('should not produce an error', async function (ctx) {
  463. await ctx.UserSessionsManager.promises.getAllUserSessions(
  464. ctx.user,
  465. ctx.exclude
  466. )
  467. })
  468. it('should produce an empty list of sessions', async function (ctx) {
  469. const sessions =
  470. await ctx.UserSessionsManager.promises.getAllUserSessions(
  471. ctx.user,
  472. ctx.exclude
  473. )
  474. expect(sessions).to.deep.equal([])
  475. })
  476. it('should have called rclient.smembers', async function (ctx) {
  477. await ctx.UserSessionsManager.promises.getAllUserSessions(
  478. ctx.user,
  479. ctx.exclude
  480. )
  481. ctx.rclient.smembers.callCount.should.equal(1)
  482. })
  483. it('should not have called rclient.get for individual keys', async function (ctx) {
  484. await ctx.UserSessionsManager.promises.getAllUserSessions(
  485. ctx.user,
  486. ctx.exclude
  487. )
  488. ctx.rclient.get.callCount.should.equal(0)
  489. })
  490. })
  491. describe('when smembers produces an error', function () {
  492. beforeEach(function (ctx) {
  493. ctx.rclient.smembers.rejects(new Error('woops'))
  494. })
  495. it('should produce an error', async function (ctx) {
  496. await expect(
  497. ctx.UserSessionsManager.promises.getAllUserSessions(
  498. ctx.user,
  499. ctx.exclude
  500. )
  501. ).to.be.rejectedWith(Error)
  502. })
  503. it('should not have called rclient.get', async function (ctx) {
  504. try {
  505. await ctx.UserSessionsManager.promises.getAllUserSessions(
  506. ctx.user,
  507. ctx.exclude
  508. )
  509. } catch (err) {
  510. // Expected error
  511. }
  512. ctx.rclient.get.callCount.should.equal(0)
  513. })
  514. })
  515. describe('when get produces an error', function () {
  516. beforeEach(function (ctx) {
  517. ctx.rclient.get = sinon.stub().rejects(new Error('woops'))
  518. })
  519. it('should produce an error', async function (ctx) {
  520. await expect(
  521. ctx.UserSessionsManager.promises.getAllUserSessions(
  522. ctx.user,
  523. ctx.exclude
  524. )
  525. ).to.be.rejectedWith(Error)
  526. })
  527. })
  528. })
  529. describe('_checkSessions', function () {
  530. beforeEach(function (ctx) {
  531. ctx.sessionKeys = ['one', 'two']
  532. ctx.rclient.smembers.resolves(ctx.sessionKeys)
  533. ctx.rclient.get.resolves('some-value')
  534. ctx.rclient.srem.resolves({})
  535. })
  536. it('should not produce an error', async function (ctx) {
  537. await ctx.UserSessionsManager.promises._checkSessions(ctx.user)
  538. })
  539. it('should call the appropriate redis methods', async function (ctx) {
  540. await ctx.UserSessionsManager.promises._checkSessions(ctx.user)
  541. ctx.rclient.smembers.callCount.should.equal(1)
  542. ctx.rclient.get.callCount.should.equal(2)
  543. ctx.rclient.srem.callCount.should.equal(0)
  544. })
  545. describe('when one of the keys is not present in redis', function () {
  546. beforeEach(function (ctx) {
  547. ctx.rclient.get.onCall(0).resolves('some-val')
  548. ctx.rclient.get.onCall(1).resolves(null)
  549. })
  550. it('should not produce an error', async function (ctx) {
  551. await ctx.UserSessionsManager.promises._checkSessions(ctx.user)
  552. })
  553. it('should remove that key from the set', async function (ctx) {
  554. await ctx.UserSessionsManager.promises._checkSessions(ctx.user)
  555. ctx.rclient.smembers.callCount.should.equal(1)
  556. ctx.rclient.get.callCount.should.equal(2)
  557. ctx.rclient.srem.callCount.should.equal(1)
  558. ctx.rclient.srem.firstCall.args[1].should.equal('two')
  559. })
  560. })
  561. describe('when no user is supplied', function () {
  562. it('should not produce an error', async function (ctx) {
  563. await ctx.UserSessionsManager.promises._checkSessions(null)
  564. })
  565. it('should not call redis methods', async function (ctx) {
  566. await ctx.UserSessionsManager.promises._checkSessions(null)
  567. ctx.rclient.smembers.callCount.should.equal(0)
  568. ctx.rclient.get.callCount.should.equal(0)
  569. })
  570. })
  571. describe('when one of the get operations produces an error', function () {
  572. beforeEach(function (ctx) {
  573. ctx.rclient.get.onCall(0).rejects(new Error('woops'))
  574. ctx.rclient.get.onCall(1).resolves(null)
  575. })
  576. it('should produce an error', async function (ctx) {
  577. await expect(
  578. ctx.UserSessionsManager.promises._checkSessions(ctx.user)
  579. ).to.be.rejectedWith(Error)
  580. })
  581. it('should call the right redis methods, bailing out early', async function (ctx) {
  582. try {
  583. await ctx.UserSessionsManager.promises._checkSessions(ctx.user)
  584. } catch (err) {
  585. // Expected error
  586. }
  587. ctx.rclient.smembers.callCount.should.equal(1)
  588. ctx.rclient.get.callCount.should.equal(1)
  589. ctx.rclient.srem.callCount.should.equal(0)
  590. })
  591. })
  592. })
  593. })