UserMembershipViewModelTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const assertCalledWith = sinon.assert.calledWith
  4. const assertNotCalled = sinon.assert.notCalled
  5. const { ObjectId } = require('mongodb-legacy')
  6. const modulePath =
  7. '../../../../app/src/Features/UserMembership/UserMembershipViewModel'
  8. const SandboxedModule = require('sandboxed-module')
  9. const {
  10. isObjectIdInstance,
  11. normalizeQuery,
  12. } = require('../../../../app/src/Features/Helpers/Mongo')
  13. describe('UserMembershipViewModel', function () {
  14. beforeEach(function () {
  15. this.UserGetter = { promises: { getUsers: sinon.stub() } }
  16. this.UserMembershipViewModel = SandboxedModule.require(modulePath, {
  17. requires: {
  18. 'mongodb-legacy': { ObjectId },
  19. '../Helpers/Mongo': { isObjectIdInstance, normalizeQuery },
  20. '../User/UserGetter': this.UserGetter,
  21. },
  22. })
  23. this.email = 'mock-email@bar.com'
  24. this.user = {
  25. _id: 'mock-user-id',
  26. email: 'mock-email@baz.com',
  27. first_name: 'Name',
  28. lastLoggedIn: '2020-05-20T10:41:11.407Z',
  29. enrollment: {
  30. managedBy: 'mock-group-id',
  31. enrolledAt: new Date(),
  32. sso: {
  33. groupId: 'abc123abc123',
  34. linkedAt: new Date(),
  35. primary: true,
  36. },
  37. },
  38. }
  39. })
  40. describe('build', function () {
  41. it('build email', function () {
  42. const viewModel = this.UserMembershipViewModel.build(this.email)
  43. expect(viewModel).to.deep.equal({
  44. email: this.email,
  45. invite: true,
  46. last_active_at: null,
  47. last_logged_in_at: null,
  48. first_name: null,
  49. last_name: null,
  50. _id: null,
  51. enrollment: undefined,
  52. })
  53. })
  54. it('build user', function () {
  55. const viewModel = this.UserMembershipViewModel.build(this.user)
  56. expect(viewModel).to.deep.equal({
  57. email: this.user.email,
  58. invite: false,
  59. last_active_at: this.user.lastLoggedIn,
  60. last_logged_in_at: this.user.lastLoggedIn,
  61. first_name: this.user.first_name,
  62. last_name: null,
  63. _id: this.user._id,
  64. enrollment: this.user.enrollment,
  65. })
  66. })
  67. })
  68. describe('build async', function () {
  69. beforeEach(function () {
  70. this.UserMembershipViewModel.build = sinon.stub()
  71. })
  72. it('build email', async function () {
  73. this.UserGetter.promises.getUsers.resolves([])
  74. await this.UserMembershipViewModel.buildAsync([this.email])
  75. assertCalledWith(this.UserMembershipViewModel.build, this.email)
  76. })
  77. it('build user', async function () {
  78. this.UserGetter.promises.getUsers.resolves([])
  79. await this.UserMembershipViewModel.buildAsync([this.user])
  80. assertCalledWith(this.UserMembershipViewModel.build, this.user)
  81. })
  82. it('build user id', async function () {
  83. const user = {
  84. ...this.user,
  85. _id: new ObjectId(),
  86. }
  87. this.UserGetter.promises.getUsers.resolves([user])
  88. const [viewModel] = await this.UserMembershipViewModel.buildAsync([
  89. user._id,
  90. ])
  91. assertNotCalled(this.UserMembershipViewModel.build)
  92. expect(viewModel._id.toString()).to.equal(user._id.toString())
  93. expect(viewModel.email).to.equal(user.email)
  94. expect(viewModel.first_name).to.equal(user.first_name)
  95. expect(viewModel.invite).to.equal(false)
  96. expect(viewModel.email).to.exist
  97. expect(viewModel.enrollment).to.exist
  98. expect(viewModel.enrollment).to.deep.equal(user.enrollment)
  99. })
  100. it('build user id with error', async function () {
  101. this.UserGetter.promises.getUsers.rejects(new Error('nope'))
  102. const userId = new ObjectId()
  103. const [viewModel] = await this.UserMembershipViewModel.buildAsync([
  104. userId,
  105. ])
  106. assertNotCalled(this.UserMembershipViewModel.build)
  107. expect(viewModel._id).to.equal(userId.toString())
  108. expect(viewModel.email).not.to.exist
  109. })
  110. })
  111. })