TeamInvitesController.test.mjs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockResponse from '../helpers/MockResponse.mjs'
  4. const modulePath =
  5. '../../../../app/src/Features/Subscription/TeamInvitesController'
  6. describe('TeamInvitesController', function () {
  7. beforeEach(async function (ctx) {
  8. ctx.user = { _id: '!@312431', email: 'user@email.com' }
  9. ctx.adminUserId = '123jlkj'
  10. ctx.subscriptionId = '123434325412'
  11. ctx.user_email = 'bob@gmail.com'
  12. ctx.req = {
  13. session: {
  14. user: {
  15. _id: ctx.adminUserId,
  16. email: ctx.user_email,
  17. },
  18. },
  19. params: {},
  20. query: {},
  21. ip: '0.0.0.0',
  22. }
  23. ctx.subscription = {
  24. _id: ctx.subscriptionId,
  25. }
  26. ctx.TeamInvitesHandler = {
  27. promises: {
  28. acceptInvite: sinon.stub().resolves(ctx.subscription),
  29. getInvite: sinon.stub().resolves({
  30. invite: {
  31. email: ctx.user.email,
  32. token: 'token123',
  33. inviterName: ctx.user_email,
  34. },
  35. subscription: ctx.subscription,
  36. }),
  37. },
  38. }
  39. ctx.SubscriptionLocator = {
  40. promises: {
  41. hasSSOEnabled: sinon.stub().resolves(true),
  42. getUsersSubscription: sinon.stub().resolves(),
  43. },
  44. }
  45. ctx.ErrorController = { notFound: sinon.stub() }
  46. ctx.SessionManager = {
  47. getLoggedInUserId(session) {
  48. return session.user?._id
  49. },
  50. getSessionUser(session) {
  51. return session.user
  52. },
  53. }
  54. ctx.UserAuditLogHandler = {
  55. promises: {
  56. addEntry: sinon.stub().resolves(),
  57. },
  58. }
  59. ctx.UserGetter = {
  60. promises: {
  61. getUser: sinon.stub().resolves(ctx.user),
  62. getUserByMainEmail: sinon.stub().resolves(ctx.user),
  63. getUserByAnyEmail: sinon.stub().resolves(ctx.user),
  64. },
  65. }
  66. ctx.EmailHandler = {
  67. sendDeferredEmail: sinon.stub().resolves(),
  68. }
  69. ctx.RateLimiter = {
  70. RateLimiter: class {
  71. consume = sinon.stub().resolves()
  72. },
  73. }
  74. vi.doMock(
  75. '../../../../app/src/Features/Subscription/TeamInvitesHandler',
  76. () => ({
  77. default: ctx.TeamInvitesHandler,
  78. })
  79. )
  80. vi.doMock(
  81. '../../../../app/src/Features/Authentication/SessionManager',
  82. () => ({
  83. default: ctx.SessionManager,
  84. })
  85. )
  86. vi.doMock(
  87. '../../../../app/src/Features/Subscription/SubscriptionLocator',
  88. () => ({
  89. default: ctx.SubscriptionLocator,
  90. })
  91. )
  92. vi.doMock('../../../../app/src/Features/User/UserAuditLogHandler', () => ({
  93. default: ctx.UserAuditLogHandler,
  94. }))
  95. vi.doMock('../../../../app/src/Features/Errors/ErrorController', () => ({
  96. default: ctx.ErrorController,
  97. }))
  98. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  99. default: ctx.UserGetter,
  100. }))
  101. vi.doMock('../../../../app/src/Features/Email/EmailHandler', () => ({
  102. default: ctx.EmailHandler,
  103. }))
  104. vi.doMock(
  105. '../../../../app/src/infrastructure/RateLimiter',
  106. () => ctx.RateLimiter
  107. )
  108. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  109. default: (ctx.Modules = {
  110. promises: {
  111. hooks: {
  112. fire: sinon.stub().resolves([]),
  113. },
  114. },
  115. }),
  116. }))
  117. vi.doMock(
  118. '../../../../app/src/Features/SplitTests/SplitTestHandler',
  119. () => ({
  120. default: (ctx.SplitTestHandler = {
  121. promises: {
  122. getAssignment: sinon.stub().resolves({}),
  123. },
  124. }),
  125. })
  126. )
  127. ctx.Controller = (await import(modulePath)).default
  128. })
  129. describe('acceptInvite', function () {
  130. it('should add an audit log entry', async function (ctx) {
  131. await new Promise(resolve => {
  132. ctx.req.params.token = 'foo'
  133. ctx.req.session.user = ctx.user
  134. const res = {
  135. json: () => {
  136. sinon.assert.calledWith(
  137. ctx.UserAuditLogHandler.promises.addEntry,
  138. ctx.user._id,
  139. 'accept-group-invitation',
  140. ctx.user._id,
  141. ctx.req.ip,
  142. { subscriptionId: ctx.subscriptionId }
  143. )
  144. resolve()
  145. },
  146. }
  147. ctx.Controller.acceptInvite(ctx.req, res)
  148. })
  149. })
  150. })
  151. describe('viewInvite', function () {
  152. const req = {
  153. params: { token: 'token123' },
  154. query: {},
  155. session: {
  156. user: { _id: 'user123' },
  157. },
  158. }
  159. describe('hasIndividualPaidSubscription', function () {
  160. it('is true for personal subscription', async function (ctx) {
  161. await new Promise(resolve => {
  162. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  163. recurlySubscription_id: 'subscription123',
  164. groupPlan: false,
  165. })
  166. const res = {
  167. render: (template, data) => {
  168. expect(data.hasIndividualPaidSubscription).to.be.true
  169. resolve()
  170. },
  171. }
  172. ctx.Controller.viewInvite(req, res)
  173. })
  174. })
  175. it('is true for group subscriptions', async function (ctx) {
  176. await new Promise(resolve => {
  177. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  178. recurlySubscription_id: 'subscription123',
  179. groupPlan: true,
  180. })
  181. const res = {
  182. render: (template, data) => {
  183. expect(data.hasIndividualPaidSubscription).to.be.false
  184. resolve()
  185. },
  186. }
  187. ctx.Controller.viewInvite(req, res)
  188. })
  189. })
  190. it('is false for canceled subscriptions', async function (ctx) {
  191. await new Promise(resolve => {
  192. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  193. recurlySubscription_id: 'subscription123',
  194. groupPlan: false,
  195. recurlyStatus: {
  196. state: 'canceled',
  197. },
  198. })
  199. const res = {
  200. render: (template, data) => {
  201. expect(data.hasIndividualPaidSubscription).to.be.false
  202. resolve()
  203. },
  204. }
  205. ctx.Controller.viewInvite(req, res)
  206. })
  207. })
  208. })
  209. describe('when user is logged out', function () {
  210. it('renders logged out invite page', async function (ctx) {
  211. await new Promise(resolve => {
  212. const res = {
  213. render: (template, data) => {
  214. expect(template).to.equal('subscriptions/team/invite_logged_out')
  215. expect(data.groupSSOActive).to.be.undefined
  216. resolve()
  217. },
  218. }
  219. ctx.Controller.viewInvite(
  220. { params: { token: 'token123' }, session: {} },
  221. res
  222. )
  223. })
  224. })
  225. it('includes groupSSOActive flag when the group has SSO enabled', async function (ctx) {
  226. await new Promise(resolve => {
  227. ctx.Modules.promises.hooks.fire = sinon.stub().resolves([true])
  228. const res = {
  229. render: (template, data) => {
  230. expect(data.groupSSOActive).to.be.true
  231. resolve()
  232. },
  233. }
  234. ctx.Controller.viewInvite(
  235. { params: { token: 'token123' }, session: {} },
  236. res
  237. )
  238. })
  239. })
  240. })
  241. it('renders the view', async function (ctx) {
  242. await new Promise(resolve => {
  243. const res = {
  244. render: template => {
  245. expect(template).to.equal('subscriptions/team/invite')
  246. resolve()
  247. },
  248. }
  249. ctx.Controller.viewInvite(req, res)
  250. })
  251. })
  252. })
  253. describe('resendInvite', function () {
  254. const email = 'user@example.com'
  255. const initPath = '/saml/ukamf/init?group_id=12345'
  256. const token = 'token123'
  257. beforeEach(function (ctx) {
  258. ctx.subscription = {
  259. teamInvites: [{ email, token }],
  260. populate: sinon.stub(),
  261. }
  262. ctx.req = {
  263. entity: ctx.subscription,
  264. body: {
  265. email,
  266. },
  267. }
  268. ctx.res = new MockResponse(vi)
  269. ctx.next = sinon.stub()
  270. })
  271. it('sends the invite email again', async function (ctx) {
  272. await new Promise(resolve => {
  273. const res = new MockResponse(vi)
  274. res.callback = () => {
  275. res.statusCode.should.equal(200)
  276. resolve()
  277. }
  278. ctx.Controller.resendInvite(ctx.req, res, ctx.next)
  279. })
  280. })
  281. describe('when domain capture is enabled', function () {
  282. beforeEach(function (ctx) {
  283. ctx.req.entity.domainCaptureEnabled = true
  284. ctx.Modules.promises.hooks.fire.resolves([initPath])
  285. })
  286. it('sends the invite again', async function (ctx) {
  287. await new Promise(resolve => {
  288. const res = new MockResponse(vi)
  289. res.callback = () => {
  290. sinon.assert.calledWith(
  291. ctx.Modules.promises.hooks.fire,
  292. 'getGroupSSOInitPath',
  293. ctx.subscription,
  294. email
  295. )
  296. res.statusCode.should.equal(200)
  297. resolve()
  298. }
  299. ctx.Controller.resendInvite(ctx.req, res, ctx.next)
  300. })
  301. })
  302. })
  303. describe('when invite was created with domain capture enabled but domain capture is now disabled', function () {
  304. beforeEach(function (ctx) {
  305. ctx.subscription = {
  306. teamInvites: [
  307. { email, inviterName: 'Test Inviter', domainCapture: true },
  308. ],
  309. populate: sinon.stub(),
  310. save: sinon.stub().resolves(),
  311. }
  312. ctx.req.entity = ctx.subscription
  313. ctx.req.entity.domainCaptureEnabled = false
  314. })
  315. it('generates a token and saves the subscription', async function (ctx) {
  316. await new Promise(resolve => {
  317. const res = new MockResponse(vi)
  318. res.callback = () => {
  319. expect(ctx.subscription.teamInvites[0].token).to.be.a('string')
  320. expect(ctx.subscription.teamInvites[0].token).to.have.length(64)
  321. sinon.assert.calledOnce(ctx.subscription.save)
  322. res.statusCode.should.equal(200)
  323. resolve()
  324. }
  325. ctx.Controller.resendInvite(ctx.req, res, ctx.next)
  326. })
  327. })
  328. })
  329. })
  330. })