SubscriptionGroupControllerTests.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.SubscriptionGroupHandler = {
  27. promises: {
  28. removeUserFromGroup: sinon.stub().resolves(),
  29. },
  30. }
  31. this.SubscriptionLocator = {
  32. promises: {
  33. getSubscription: sinon.stub().resolves(this.subscription),
  34. },
  35. }
  36. this.SessionManager = {
  37. getLoggedInUserId(session) {
  38. return session.user._id
  39. },
  40. getSessionUser(session) {
  41. return session.user
  42. },
  43. }
  44. this.UserAuditLogHandler = {
  45. promises: {
  46. addEntry: sinon.stub().resolves(),
  47. },
  48. }
  49. this.Modules = {
  50. promises: {
  51. hooks: {
  52. fire: sinon.stub().resolves(),
  53. },
  54. },
  55. }
  56. this.Controller = SandboxedModule.require(modulePath, {
  57. requires: {
  58. './SubscriptionGroupHandler': this.SubscriptionGroupHandler,
  59. './SubscriptionLocator': this.SubscriptionLocator,
  60. '../Authentication/SessionManager': this.SessionManager,
  61. '../User/UserAuditLogHandler': this.UserAuditLogHandler,
  62. '../../infrastructure/Modules': this.Modules,
  63. },
  64. })
  65. })
  66. describe('removeUserFromGroup', function () {
  67. it('should use the subscription id for the logged in user and take the user id from the params', function (done) {
  68. const userIdToRemove = '31231'
  69. this.req.params = { user_id: userIdToRemove }
  70. this.req.entity = this.subscription
  71. const res = {
  72. sendStatus: () => {
  73. this.SubscriptionGroupHandler.promises.removeUserFromGroup
  74. .calledWith(this.subscriptionId, userIdToRemove)
  75. .should.equal(true)
  76. done()
  77. },
  78. }
  79. this.Controller.removeUserFromGroup(this.req, res, done)
  80. })
  81. it('should log that the user has been removed', function (done) {
  82. const userIdToRemove = '31231'
  83. this.req.params = { user_id: userIdToRemove }
  84. this.req.entity = this.subscription
  85. const res = {
  86. sendStatus: () => {
  87. sinon.assert.calledWith(
  88. this.UserAuditLogHandler.promises.addEntry,
  89. userIdToRemove,
  90. 'remove-from-group-subscription',
  91. this.adminUserId,
  92. this.req.ip,
  93. { subscriptionId: this.subscriptionId }
  94. )
  95. done()
  96. },
  97. }
  98. this.Controller.removeUserFromGroup(this.req, res, done)
  99. })
  100. it('should call the group SSO hooks with group SSO enabled', function (done) {
  101. const userIdToRemove = '31231'
  102. this.req.params = { user_id: userIdToRemove }
  103. this.req.entity = this.subscription
  104. this.Modules.promises.hooks.fire
  105. .withArgs('hasGroupSSOEnabled', this.subscription)
  106. .resolves([true])
  107. const res = {
  108. sendStatus: () => {
  109. this.Modules.promises.hooks.fire
  110. .calledWith('hasGroupSSOEnabled', this.subscription)
  111. .should.equal(true)
  112. this.Modules.promises.hooks.fire
  113. .calledWith(
  114. 'unlinkUserFromGroupSSO',
  115. userIdToRemove,
  116. this.subscriptionId
  117. )
  118. .should.equal(true)
  119. sinon.assert.calledTwice(this.Modules.promises.hooks.fire)
  120. done()
  121. },
  122. }
  123. this.Controller.removeUserFromGroup(this.req, res, done)
  124. })
  125. it('should call the group SSO hooks with group SSO disabled', function (done) {
  126. const userIdToRemove = '31231'
  127. this.req.params = { user_id: userIdToRemove }
  128. this.req.entity = this.subscription
  129. this.Modules.promises.hooks.fire
  130. .withArgs('hasGroupSSOEnabled', this.subscription)
  131. .resolves([false])
  132. const res = {
  133. sendStatus: () => {
  134. this.Modules.promises.hooks.fire
  135. .calledWith('hasGroupSSOEnabled', this.subscription)
  136. .should.equal(true)
  137. sinon.assert.calledOnce(this.Modules.promises.hooks.fire)
  138. done()
  139. },
  140. }
  141. this.Controller.removeUserFromGroup(this.req, res, done)
  142. })
  143. })
  144. describe('removeSelfFromGroup', function () {
  145. it('gets subscription and remove user', function (done) {
  146. this.req.query = { subscriptionId: this.subscriptionId }
  147. const memberUserIdToremove = 123456789
  148. this.req.session.user._id = memberUserIdToremove
  149. const res = {
  150. sendStatus: () => {
  151. sinon.assert.calledWith(
  152. this.SubscriptionLocator.promises.getSubscription,
  153. this.subscriptionId
  154. )
  155. sinon.assert.calledWith(
  156. this.SubscriptionGroupHandler.promises.removeUserFromGroup,
  157. this.subscriptionId,
  158. memberUserIdToremove
  159. )
  160. done()
  161. },
  162. }
  163. this.Controller.removeSelfFromGroup(this.req, res, done)
  164. })
  165. it('should log that the user has left the subscription', function (done) {
  166. this.req.query = { subscriptionId: this.subscriptionId }
  167. const memberUserIdToremove = '123456789'
  168. this.req.session.user._id = memberUserIdToremove
  169. const res = {
  170. sendStatus: () => {
  171. sinon.assert.calledWith(
  172. this.UserAuditLogHandler.promises.addEntry,
  173. memberUserIdToremove,
  174. 'remove-from-group-subscription',
  175. memberUserIdToremove,
  176. this.req.ip,
  177. { subscriptionId: this.subscriptionId }
  178. )
  179. done()
  180. },
  181. }
  182. this.Controller.removeSelfFromGroup(this.req, res, done)
  183. })
  184. it('should call the group SSO hooks with group SSO enabled', function (done) {
  185. this.req.query = { subscriptionId: this.subscriptionId }
  186. const memberUserIdToremove = '123456789'
  187. this.req.session.user._id = memberUserIdToremove
  188. this.Modules.promises.hooks.fire
  189. .withArgs('hasGroupSSOEnabled', this.subscription)
  190. .resolves([true])
  191. const res = {
  192. sendStatus: () => {
  193. this.Modules.promises.hooks.fire
  194. .calledWith('hasGroupSSOEnabled', this.subscription)
  195. .should.equal(true)
  196. this.Modules.promises.hooks.fire
  197. .calledWith(
  198. 'unlinkUserFromGroupSSO',
  199. memberUserIdToremove,
  200. this.subscriptionId
  201. )
  202. .should.equal(true)
  203. sinon.assert.calledTwice(this.Modules.promises.hooks.fire)
  204. done()
  205. },
  206. }
  207. this.Controller.removeSelfFromGroup(this.req, res, done)
  208. })
  209. it('should call the group SSO hooks with group SSO disabled', function (done) {
  210. const userIdToRemove = '31231'
  211. this.req.session.user._id = userIdToRemove
  212. this.req.params = { user_id: userIdToRemove }
  213. this.req.entity = this.subscription
  214. this.Modules.promises.hooks.fire
  215. .withArgs('hasGroupSSOEnabled', this.subscription)
  216. .resolves([false])
  217. const res = {
  218. sendStatus: () => {
  219. this.Modules.promises.hooks.fire
  220. .calledWith('hasGroupSSOEnabled', this.subscription)
  221. .should.equal(true)
  222. sinon.assert.calledOnce(this.Modules.promises.hooks.fire)
  223. done()
  224. },
  225. }
  226. this.Controller.removeSelfFromGroup(this.req, res, done)
  227. })
  228. })
  229. })