SubscriptionGroupControllerTests.mjs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import esmock from 'esmock'
  2. import sinon from 'sinon'
  3. import { expect } from 'chai'
  4. const modulePath =
  5. '../../../../app/src/Features/Subscription/SubscriptionGroupController'
  6. describe('SubscriptionGroupController', function () {
  7. beforeEach(async function () {
  8. this.user = { _id: '!@312431', email: 'user@email.com' }
  9. this.adminUserId = '123jlkj'
  10. this.subscriptionId = '123434325412'
  11. this.user_email = 'bob@gmail.com'
  12. this.req = {
  13. session: {
  14. user: {
  15. _id: this.adminUserId,
  16. email: this.user_email,
  17. },
  18. },
  19. params: {
  20. subscriptionId: this.subscriptionId,
  21. },
  22. query: {},
  23. }
  24. this.subscription = {
  25. _id: this.subscriptionId,
  26. teamName: 'Cool group',
  27. }
  28. this.SubscriptionGroupHandler = {
  29. promises: {
  30. removeUserFromGroup: sinon.stub().resolves(),
  31. },
  32. }
  33. this.SubscriptionLocator = {
  34. promises: {
  35. getSubscription: sinon.stub().resolves(this.subscription),
  36. getUsersSubscription: sinon.stub().resolves(this.subscription),
  37. },
  38. }
  39. this.SessionManager = {
  40. getLoggedInUserId(session) {
  41. return session.user._id
  42. },
  43. getSessionUser(session) {
  44. return session.user
  45. },
  46. }
  47. this.UserAuditLogHandler = {
  48. promises: {
  49. addEntry: sinon.stub().resolves(),
  50. },
  51. }
  52. this.Modules = {
  53. promises: {
  54. hooks: {
  55. fire: sinon.stub().resolves(),
  56. },
  57. },
  58. }
  59. this.SplitTestHandler = {
  60. promises: {
  61. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  62. },
  63. getAssignment: sinon.stub().yields(null, { variant: 'default' }),
  64. }
  65. this.Controller = await esmock.strict(modulePath, {
  66. '../../../../app/src/Features/Subscription/SubscriptionGroupHandler':
  67. this.SubscriptionGroupHandler,
  68. '../../../../app/src/Features/Subscription/SubscriptionLocator':
  69. this.SubscriptionLocator,
  70. '../../../../app/src/Features/Authentication/SessionManager':
  71. this.SessionManager,
  72. '../../../../app/src/Features/User/UserAuditLogHandler':
  73. this.UserAuditLogHandler,
  74. '../../../../app/src/infrastructure/Modules': this.Modules,
  75. '../../../../app/src/Features/SplitTests/SplitTestHandler':
  76. this.SplitTestHandler,
  77. '../../../../app/src/Features/Errors/ErrorController':
  78. (this.ErrorController = {
  79. notFound: sinon.stub(),
  80. }),
  81. })
  82. })
  83. describe('removeUserFromGroup', function () {
  84. it('should use the subscription id for the logged in user and take the user id from the params', function (done) {
  85. const userIdToRemove = '31231'
  86. this.req.params = { user_id: userIdToRemove }
  87. this.req.entity = this.subscription
  88. const res = {
  89. sendStatus: () => {
  90. this.SubscriptionGroupHandler.promises.removeUserFromGroup
  91. .calledWith(this.subscriptionId, userIdToRemove)
  92. .should.equal(true)
  93. done()
  94. },
  95. }
  96. this.Controller.removeUserFromGroup(this.req, res, done)
  97. })
  98. it('should log that the user has been removed', function (done) {
  99. const userIdToRemove = '31231'
  100. this.req.params = { user_id: userIdToRemove }
  101. this.req.entity = this.subscription
  102. const res = {
  103. sendStatus: () => {
  104. sinon.assert.calledWith(
  105. this.UserAuditLogHandler.promises.addEntry,
  106. userIdToRemove,
  107. 'remove-from-group-subscription',
  108. this.adminUserId,
  109. this.req.ip,
  110. { subscriptionId: this.subscriptionId }
  111. )
  112. done()
  113. },
  114. }
  115. this.Controller.removeUserFromGroup(this.req, res, done)
  116. })
  117. it('should call the group SSO hooks with group SSO enabled', function (done) {
  118. const userIdToRemove = '31231'
  119. this.req.params = { user_id: userIdToRemove }
  120. this.req.entity = this.subscription
  121. this.Modules.promises.hooks.fire
  122. .withArgs('hasGroupSSOEnabled', this.subscription)
  123. .resolves([true])
  124. const res = {
  125. sendStatus: () => {
  126. this.Modules.promises.hooks.fire
  127. .calledWith('hasGroupSSOEnabled', this.subscription)
  128. .should.equal(true)
  129. this.Modules.promises.hooks.fire
  130. .calledWith(
  131. 'unlinkUserFromGroupSSO',
  132. userIdToRemove,
  133. this.subscriptionId
  134. )
  135. .should.equal(true)
  136. sinon.assert.calledTwice(this.Modules.promises.hooks.fire)
  137. done()
  138. },
  139. }
  140. this.Controller.removeUserFromGroup(this.req, res, done)
  141. })
  142. it('should call the group SSO hooks with group SSO disabled', function (done) {
  143. const userIdToRemove = '31231'
  144. this.req.params = { user_id: userIdToRemove }
  145. this.req.entity = this.subscription
  146. this.Modules.promises.hooks.fire
  147. .withArgs('hasGroupSSOEnabled', this.subscription)
  148. .resolves([false])
  149. const res = {
  150. sendStatus: () => {
  151. this.Modules.promises.hooks.fire
  152. .calledWith('hasGroupSSOEnabled', this.subscription)
  153. .should.equal(true)
  154. sinon.assert.calledOnce(this.Modules.promises.hooks.fire)
  155. done()
  156. },
  157. }
  158. this.Controller.removeUserFromGroup(this.req, res, done)
  159. })
  160. })
  161. describe('removeSelfFromGroup', function () {
  162. it('gets subscription and remove user', function (done) {
  163. this.req.query = { subscriptionId: this.subscriptionId }
  164. const memberUserIdToremove = 123456789
  165. this.req.session.user._id = memberUserIdToremove
  166. const res = {
  167. sendStatus: () => {
  168. sinon.assert.calledWith(
  169. this.SubscriptionLocator.promises.getSubscription,
  170. this.subscriptionId
  171. )
  172. sinon.assert.calledWith(
  173. this.SubscriptionGroupHandler.promises.removeUserFromGroup,
  174. this.subscriptionId,
  175. memberUserIdToremove
  176. )
  177. done()
  178. },
  179. }
  180. this.Controller.removeSelfFromGroup(this.req, res, done)
  181. })
  182. it('should log that the user has left the subscription', function (done) {
  183. this.req.query = { subscriptionId: this.subscriptionId }
  184. const memberUserIdToremove = '123456789'
  185. this.req.session.user._id = memberUserIdToremove
  186. const res = {
  187. sendStatus: () => {
  188. sinon.assert.calledWith(
  189. this.UserAuditLogHandler.promises.addEntry,
  190. memberUserIdToremove,
  191. 'remove-from-group-subscription',
  192. memberUserIdToremove,
  193. this.req.ip,
  194. { subscriptionId: this.subscriptionId }
  195. )
  196. done()
  197. },
  198. }
  199. this.Controller.removeSelfFromGroup(this.req, res, done)
  200. })
  201. it('should call the group SSO hooks with group SSO enabled', function (done) {
  202. this.req.query = { subscriptionId: this.subscriptionId }
  203. const memberUserIdToremove = '123456789'
  204. this.req.session.user._id = memberUserIdToremove
  205. this.Modules.promises.hooks.fire
  206. .withArgs('hasGroupSSOEnabled', this.subscription)
  207. .resolves([true])
  208. const res = {
  209. sendStatus: () => {
  210. this.Modules.promises.hooks.fire
  211. .calledWith('hasGroupSSOEnabled', this.subscription)
  212. .should.equal(true)
  213. this.Modules.promises.hooks.fire
  214. .calledWith(
  215. 'unlinkUserFromGroupSSO',
  216. memberUserIdToremove,
  217. this.subscriptionId
  218. )
  219. .should.equal(true)
  220. sinon.assert.calledTwice(this.Modules.promises.hooks.fire)
  221. done()
  222. },
  223. }
  224. this.Controller.removeSelfFromGroup(this.req, res, done)
  225. })
  226. it('should call the group SSO hooks with group SSO disabled', function (done) {
  227. const userIdToRemove = '31231'
  228. this.req.session.user._id = userIdToRemove
  229. this.req.params = { user_id: userIdToRemove }
  230. this.req.entity = this.subscription
  231. this.Modules.promises.hooks.fire
  232. .withArgs('hasGroupSSOEnabled', this.subscription)
  233. .resolves([false])
  234. const res = {
  235. sendStatus: () => {
  236. this.Modules.promises.hooks.fire
  237. .calledWith('hasGroupSSOEnabled', this.subscription)
  238. .should.equal(true)
  239. sinon.assert.calledOnce(this.Modules.promises.hooks.fire)
  240. done()
  241. },
  242. }
  243. this.Controller.removeSelfFromGroup(this.req, res, done)
  244. })
  245. })
  246. describe('add seats', function () {
  247. it('render the request confirmation view', async function () {
  248. this.SplitTestHandler.promises.getAssignment.resolves({
  249. variant: 'enabled',
  250. })
  251. await this.Controller.requestConfirmation(this.req, {
  252. render: (viewPath, viewParams) => {
  253. expect(viewPath).to.equal('subscriptions/request-confirmation-react')
  254. expect(viewParams.groupName).to.equal('Cool group')
  255. },
  256. })
  257. expect(this.ErrorController.notFound).to.not.have.been.called
  258. })
  259. })
  260. })