TeamInvitesController.test.mjs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/TeamInvitesController'
  5. describe('TeamInvitesController', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.user = { _id: '!@312431', email: 'user@email.com' }
  8. ctx.adminUserId = '123jlkj'
  9. ctx.subscriptionId = '123434325412'
  10. ctx.user_email = 'bob@gmail.com'
  11. ctx.req = {
  12. session: {
  13. user: {
  14. _id: ctx.adminUserId,
  15. email: ctx.user_email,
  16. },
  17. },
  18. params: {},
  19. query: {},
  20. ip: '0.0.0.0',
  21. }
  22. ctx.subscription = {
  23. _id: ctx.subscriptionId,
  24. }
  25. ctx.TeamInvitesHandler = {
  26. promises: {
  27. acceptInvite: sinon.stub().resolves(ctx.subscription),
  28. getInvite: sinon.stub().resolves({
  29. invite: {
  30. email: ctx.user.email,
  31. token: 'token123',
  32. inviterName: ctx.user_email,
  33. },
  34. subscription: ctx.subscription,
  35. }),
  36. },
  37. }
  38. ctx.SubscriptionLocator = {
  39. promises: {
  40. hasSSOEnabled: sinon.stub().resolves(true),
  41. getUsersSubscription: sinon.stub().resolves(),
  42. },
  43. }
  44. ctx.ErrorController = { notFound: sinon.stub() }
  45. ctx.SessionManager = {
  46. getLoggedInUserId(session) {
  47. return session.user?._id
  48. },
  49. getSessionUser(session) {
  50. return session.user
  51. },
  52. }
  53. ctx.UserAuditLogHandler = {
  54. promises: {
  55. addEntry: sinon.stub().resolves(),
  56. },
  57. }
  58. ctx.UserGetter = {
  59. promises: {
  60. getUser: sinon.stub().resolves(ctx.user),
  61. getUserByMainEmail: sinon.stub().resolves(ctx.user),
  62. getUserByAnyEmail: sinon.stub().resolves(ctx.user),
  63. },
  64. }
  65. ctx.EmailHandler = {
  66. sendDeferredEmail: sinon.stub().resolves(),
  67. }
  68. ctx.RateLimiter = {
  69. RateLimiter: class {},
  70. }
  71. vi.doMock(
  72. '../../../../app/src/Features/Subscription/TeamInvitesHandler',
  73. () => ({
  74. default: ctx.TeamInvitesHandler,
  75. })
  76. )
  77. vi.doMock(
  78. '../../../../app/src/Features/Authentication/SessionManager',
  79. () => ({
  80. default: ctx.SessionManager,
  81. })
  82. )
  83. vi.doMock(
  84. '../../../../app/src/Features/Subscription/SubscriptionLocator',
  85. () => ({
  86. default: ctx.SubscriptionLocator,
  87. })
  88. )
  89. vi.doMock('../../../../app/src/Features/User/UserAuditLogHandler', () => ({
  90. default: ctx.UserAuditLogHandler,
  91. }))
  92. vi.doMock('../../../../app/src/Features/Errors/ErrorController', () => ({
  93. default: ctx.ErrorController,
  94. }))
  95. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  96. default: ctx.UserGetter,
  97. }))
  98. vi.doMock('../../../../app/src/Features/Email/EmailHandler', () => ({
  99. default: ctx.EmailHandler,
  100. }))
  101. vi.doMock(
  102. '../../../../app/src/infrastructure/RateLimiter',
  103. () => ctx.RateLimiter
  104. )
  105. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  106. default: (ctx.Modules = {
  107. promises: {
  108. hooks: {
  109. fire: sinon.stub().resolves([]),
  110. },
  111. },
  112. }),
  113. }))
  114. vi.doMock(
  115. '../../../../app/src/Features/SplitTests/SplitTestHandler',
  116. () => ({
  117. default: (ctx.SplitTestHandler = {
  118. promises: {
  119. getAssignment: sinon.stub().resolves({}),
  120. },
  121. }),
  122. })
  123. )
  124. ctx.Controller = (await import(modulePath)).default
  125. })
  126. describe('acceptInvite', function () {
  127. it('should add an audit log entry', async function (ctx) {
  128. await new Promise(resolve => {
  129. ctx.req.params.token = 'foo'
  130. ctx.req.session.user = ctx.user
  131. const res = {
  132. json: () => {
  133. sinon.assert.calledWith(
  134. ctx.UserAuditLogHandler.promises.addEntry,
  135. ctx.user._id,
  136. 'accept-group-invitation',
  137. ctx.user._id,
  138. ctx.req.ip,
  139. { subscriptionId: ctx.subscriptionId }
  140. )
  141. resolve()
  142. },
  143. }
  144. ctx.Controller.acceptInvite(ctx.req, res)
  145. })
  146. })
  147. })
  148. describe('viewInvite', function () {
  149. const req = {
  150. params: { token: 'token123' },
  151. query: {},
  152. session: {
  153. user: { _id: 'user123' },
  154. },
  155. }
  156. describe('hasIndividualPaidSubscription', function () {
  157. it('is true for personal subscription', async function (ctx) {
  158. await new Promise(resolve => {
  159. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  160. recurlySubscription_id: 'subscription123',
  161. groupPlan: false,
  162. })
  163. const res = {
  164. render: (template, data) => {
  165. expect(data.hasIndividualPaidSubscription).to.be.true
  166. resolve()
  167. },
  168. }
  169. ctx.Controller.viewInvite(req, res)
  170. })
  171. })
  172. it('is true for group subscriptions', async function (ctx) {
  173. await new Promise(resolve => {
  174. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  175. recurlySubscription_id: 'subscription123',
  176. groupPlan: true,
  177. })
  178. const res = {
  179. render: (template, data) => {
  180. expect(data.hasIndividualPaidSubscription).to.be.false
  181. resolve()
  182. },
  183. }
  184. ctx.Controller.viewInvite(req, res)
  185. })
  186. })
  187. it('is false for canceled subscriptions', async function (ctx) {
  188. await new Promise(resolve => {
  189. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  190. recurlySubscription_id: 'subscription123',
  191. groupPlan: false,
  192. recurlyStatus: {
  193. state: 'canceled',
  194. },
  195. })
  196. const res = {
  197. render: (template, data) => {
  198. expect(data.hasIndividualPaidSubscription).to.be.false
  199. resolve()
  200. },
  201. }
  202. ctx.Controller.viewInvite(req, res)
  203. })
  204. })
  205. })
  206. describe('when user is logged out', function () {
  207. it('renders logged out invite page', async function (ctx) {
  208. await new Promise(resolve => {
  209. const res = {
  210. render: (template, data) => {
  211. expect(template).to.equal('subscriptions/team/invite_logged_out')
  212. expect(data.groupSSOActive).to.be.undefined
  213. resolve()
  214. },
  215. }
  216. ctx.Controller.viewInvite(
  217. { params: { token: 'token123' }, session: {} },
  218. res
  219. )
  220. })
  221. })
  222. it('includes groupSSOActive flag when the group has SSO enabled', async function (ctx) {
  223. await new Promise(resolve => {
  224. ctx.Modules.promises.hooks.fire = sinon.stub().resolves([true])
  225. const res = {
  226. render: (template, data) => {
  227. expect(data.groupSSOActive).to.be.true
  228. resolve()
  229. },
  230. }
  231. ctx.Controller.viewInvite(
  232. { params: { token: 'token123' }, session: {} },
  233. res
  234. )
  235. })
  236. })
  237. })
  238. it('renders the view', async function (ctx) {
  239. await new Promise(resolve => {
  240. const res = {
  241. render: template => {
  242. expect(template).to.equal('subscriptions/team/invite')
  243. resolve()
  244. },
  245. }
  246. ctx.Controller.viewInvite(req, res)
  247. })
  248. })
  249. })
  250. })