SessionManager.test.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import tk from 'timekeeper'
  4. import mongodb from 'mongodb-legacy'
  5. const modulePath =
  6. '../../../../app/src/Features/Authentication/SessionManager.mjs'
  7. const { ObjectId } = mongodb
  8. describe('SessionManager', function () {
  9. beforeEach(async function (ctx) {
  10. ctx.UserModel = { findOne: sinon.stub() }
  11. ctx.SessionManager = (await import(modulePath)).default
  12. ctx.user = {
  13. _id: new ObjectId(),
  14. email: (ctx.email = 'USER@example.com'),
  15. first_name: 'bob',
  16. last_name: 'brown',
  17. referal_id: 1234,
  18. isAdmin: false,
  19. }
  20. ctx.session = sinon.stub()
  21. })
  22. afterEach(function () {
  23. tk.reset()
  24. })
  25. describe('isUserLoggedIn', function () {
  26. beforeEach(function (ctx) {
  27. ctx.stub = sinon.stub(ctx.SessionManager, 'getLoggedInUserId')
  28. })
  29. afterEach(function (ctx) {
  30. ctx.stub.restore()
  31. })
  32. it('should do the right thing in all cases', function (ctx) {
  33. ctx.SessionManager.getLoggedInUserId.returns('some_id')
  34. expect(ctx.SessionManager.isUserLoggedIn(ctx.session)).to.equal(true)
  35. ctx.SessionManager.getLoggedInUserId.returns(null)
  36. expect(ctx.SessionManager.isUserLoggedIn(ctx.session)).to.equal(false)
  37. ctx.SessionManager.getLoggedInUserId.returns(false)
  38. expect(ctx.SessionManager.isUserLoggedIn(ctx.session)).to.equal(false)
  39. ctx.SessionManager.getLoggedInUserId.returns(undefined)
  40. expect(ctx.SessionManager.isUserLoggedIn(ctx.session)).to.equal(false)
  41. })
  42. })
  43. describe('setInSessionUser', function () {
  44. beforeEach(function (ctx) {
  45. ctx.user = {
  46. _id: 'id',
  47. first_name: 'a',
  48. last_name: 'b',
  49. email: 'c',
  50. }
  51. ctx.SessionManager.getSessionUser = sinon.stub().returns(ctx.user)
  52. })
  53. it('should update the right properties', function (ctx) {
  54. ctx.SessionManager.setInSessionUser(ctx.session, {
  55. first_name: 'new_first_name',
  56. email: 'new_email',
  57. })
  58. const expectedUser = {
  59. _id: 'id',
  60. first_name: 'new_first_name',
  61. last_name: 'b',
  62. email: 'new_email',
  63. }
  64. expect(ctx.user).to.deep.equal(expectedUser)
  65. expect(ctx.user).to.deep.equal(expectedUser)
  66. })
  67. })
  68. describe('getLoggedInUserId', function () {
  69. beforeEach(function (ctx) {
  70. ctx.req = { session: {} }
  71. })
  72. it('should return the user id from the session', function (ctx) {
  73. ctx.user_id = '2134'
  74. ctx.session.user = { _id: ctx.user_id }
  75. const result = ctx.SessionManager.getLoggedInUserId(ctx.session)
  76. expect(result).to.equal(ctx.user_id)
  77. })
  78. it('should return user for passport session', function (ctx) {
  79. ctx.user_id = '2134'
  80. ctx.session = {
  81. passport: {
  82. user: {
  83. _id: ctx.user_id,
  84. },
  85. },
  86. }
  87. const result = ctx.SessionManager.getLoggedInUserId(ctx.session)
  88. expect(result).to.equal(ctx.user_id)
  89. })
  90. it('should return null if there is no user on the session', function (ctx) {
  91. ctx.session = {}
  92. const result = ctx.SessionManager.getLoggedInUserId(ctx.session)
  93. expect(result).to.equal(null)
  94. })
  95. it('should return null if there is no session', function (ctx) {
  96. const result = ctx.SessionManager.getLoggedInUserId(undefined)
  97. expect(result).to.equal(null)
  98. })
  99. })
  100. })