TeamInvitesControllerTests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/TeamInvitesController'
  5. describe('TeamInvitesController', 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. query: {},
  20. ip: '0.0.0.0',
  21. }
  22. this.subscription = {
  23. _id: this.subscriptionId,
  24. }
  25. this.TeamInvitesHandler = {
  26. promises: { acceptInvite: sinon.stub().resolves(this.subscription) },
  27. }
  28. this.SubscriptionLocator = {
  29. promises: {
  30. hasSSOEnabled: sinon.stub().resolves(true),
  31. },
  32. }
  33. this.ErrorController = { notFound: sinon.stub() }
  34. this.SessionManager = {
  35. getLoggedInUserId(session) {
  36. return session.user._id
  37. },
  38. getSessionUser(session) {
  39. return session.user
  40. },
  41. }
  42. this.UserAuditLogHandler = {
  43. promises: {
  44. addEntry: sinon.stub().resolves(),
  45. },
  46. }
  47. this.UserGetter = {
  48. promises: {
  49. getUser: sinon.stub().resolves(this.user),
  50. getUserByMainEmail: sinon.stub().resolves(this.user),
  51. getUserByAnyEmail: sinon.stub().resolves(this.user),
  52. },
  53. }
  54. this.EmailHandler = {
  55. sendDeferredEmail: sinon.stub().resolves(),
  56. }
  57. this.RateLimiter = {
  58. RateLimiter: class {},
  59. }
  60. this.Controller = SandboxedModule.require(modulePath, {
  61. requires: {
  62. './TeamInvitesHandler': this.TeamInvitesHandler,
  63. '../Authentication/SessionManager': this.SessionManager,
  64. './SubscriptionLocator': this.SubscriptionLocator,
  65. '../User/UserAuditLogHandler': this.UserAuditLogHandler,
  66. '../Errors/ErrorController': this.ErrorController,
  67. '../User/UserGetter': this.UserGetter,
  68. '../Email/EmailHandler': this.EmailHandler,
  69. '../../infrastructure/RateLimiter': this.RateLimiter,
  70. },
  71. })
  72. })
  73. describe('acceptInvite', function () {
  74. it('should add an audit log entry', function (done) {
  75. this.req.params.token = 'foo'
  76. this.req.session.user = this.user
  77. const res = {
  78. json: () => {
  79. sinon.assert.calledWith(
  80. this.UserAuditLogHandler.promises.addEntry,
  81. this.user._id,
  82. 'accept-group-invitation',
  83. this.user._id,
  84. this.req.ip,
  85. { subscriptionId: this.subscriptionId }
  86. )
  87. done()
  88. },
  89. }
  90. this.Controller.acceptInvite(this.req, res)
  91. })
  92. })
  93. })