SubscriptionGroupControllerTests.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/SubscriptionGroupController'
  5. describe('SubscriptionGroupController', function () {
  6. beforeEach(function () {
  7. this.user = { _id: '!@312431', email: 'user@email.com' }
  8. this.adminUserId = '123jlkj'
  9. this.subscriptionId = '123434325412'
  10. this.user_email = 'bob@gmail.com'
  11. this.req = {
  12. session: {
  13. user: {
  14. _id: this.adminUserId,
  15. email: this.user_email,
  16. },
  17. },
  18. params: {
  19. subscriptionId: this.subscriptionId,
  20. },
  21. query: {},
  22. }
  23. this.subscription = {
  24. _id: this.subscriptionId,
  25. }
  26. this.GroupHandler = { removeUserFromGroup: sinon.stub().callsArgWith(2) }
  27. this.SubscriptionLocator = {
  28. getSubscription: sinon.stub().callsArgWith(1, null, this.subscription),
  29. }
  30. this.SessionManager = {
  31. getLoggedInUserId(session) {
  32. return session.user._id
  33. },
  34. getSessionUser(session) {
  35. return session.user
  36. },
  37. }
  38. this.Controller = SandboxedModule.require(modulePath, {
  39. requires: {
  40. './SubscriptionGroupHandler': this.GroupHandler,
  41. './SubscriptionLocator': this.SubscriptionLocator,
  42. '../Authentication/SessionManager': this.SessionManager,
  43. },
  44. })
  45. })
  46. describe('removeUserFromGroup', function () {
  47. it('should use the subscription id for the logged in user and take the user id from the params', function (done) {
  48. const userIdToRemove = '31231'
  49. this.req.params = { user_id: userIdToRemove }
  50. this.req.entity = this.subscription
  51. const res = {
  52. sendStatus: () => {
  53. this.GroupHandler.removeUserFromGroup
  54. .calledWith(this.subscriptionId, userIdToRemove)
  55. .should.equal(true)
  56. done()
  57. },
  58. }
  59. this.Controller.removeUserFromGroup(this.req, res)
  60. })
  61. })
  62. describe('removeSelfFromGroup', function () {
  63. it('gets subscription and remove user', function (done) {
  64. this.req.query = { subscriptionId: this.subscriptionId }
  65. const memberUserIdToremove = 123456789
  66. this.req.session.user._id = memberUserIdToremove
  67. const res = {
  68. sendStatus: () => {
  69. sinon.assert.calledWith(
  70. this.SubscriptionLocator.getSubscription,
  71. this.subscriptionId
  72. )
  73. sinon.assert.calledWith(
  74. this.GroupHandler.removeUserFromGroup,
  75. this.subscriptionId,
  76. memberUserIdToremove
  77. )
  78. done()
  79. },
  80. }
  81. this.Controller.removeSelfFromGroup(this.req, res)
  82. })
  83. })
  84. })