SessionManagerTests.js 3.4 KB

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