UserCreator.test.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { vi, assert } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/Features/User/UserCreator.mjs'
  4. describe('UserCreator', function () {
  5. beforeEach(async function (ctx) {
  6. const self = ctx
  7. ctx.user = { _id: '12390i', ace: {}, analyticsId: 'uuid' }
  8. ctx.user.save = sinon.stub().resolves(self.user)
  9. ctx.UserModel = class Project {
  10. constructor() {
  11. return self.user
  12. }
  13. }
  14. ctx.logger = {
  15. error: sinon.stub(),
  16. }
  17. vi.doMock('@overleaf/logger', () => ({
  18. default: ctx.logger,
  19. }))
  20. vi.doMock('../../../../app/src/models/User', () => ({
  21. User: ctx.UserModel,
  22. }))
  23. vi.doMock('../../../../app/src/infrastructure/Features', () => ({
  24. default: (ctx.Features = {
  25. hasFeature: sinon.stub().returns(false),
  26. }),
  27. }))
  28. vi.doMock('../../../../app/src/Features/User/UserDeleter', () => ({
  29. default: (ctx.UserDeleter = {
  30. promises: {
  31. deleteNewUser: sinon.stub().resolves(),
  32. },
  33. }),
  34. }))
  35. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  36. default: (ctx.UserGetter = {
  37. promises: {
  38. getUser: sinon.stub().resolves(ctx.user),
  39. },
  40. }),
  41. }))
  42. vi.doMock('../../../../app/src/Features/User/UserUpdater', () => ({
  43. default: (ctx.UserUpdater = {
  44. promises: {
  45. addAffiliationForNewUser: sinon.stub().resolves({
  46. matchedCount: 1,
  47. modifiedCount: 1,
  48. acknowledged: true,
  49. }),
  50. updateUser: sinon.stub().resolves(),
  51. },
  52. }),
  53. }))
  54. vi.doMock(
  55. '../../../../app/src/Features/Analytics/AnalyticsManager',
  56. () => ({
  57. default: (ctx.Analytics = {
  58. recordEventForMongoUserInBackground: sinon.stub(),
  59. recordEventForUserInBackground: sinon.stub(),
  60. setUserPropertyForMongoUser: sinon.stub(),
  61. }),
  62. })
  63. )
  64. vi.doMock(
  65. '../../../../app/src/Features/SplitTests/SplitTestHandler',
  66. () => ({
  67. default: (ctx.SplitTestHandler = {
  68. promises: {
  69. getAssignmentForUser: sinon.stub().resolves({ variant: 'active' }),
  70. },
  71. }),
  72. })
  73. )
  74. vi.doMock(
  75. '../../../../app/src/Features/User/UserOnboardingEmailManager',
  76. () => ({
  77. default: (ctx.UserOnboardingEmailManager = {
  78. scheduleOnboardingEmail: sinon.stub(),
  79. }),
  80. })
  81. )
  82. vi.doMock(
  83. '../../../../app/src/Features/User/UserPostRegistrationAnalyticsManager',
  84. () => ({
  85. default: (ctx.UserPostRegistrationAnalyticsManager = {
  86. schedulePostRegistrationAnalytics: sinon.stub(),
  87. }),
  88. })
  89. )
  90. ctx.UserCreator = (await import(modulePath)).default
  91. ctx.email = 'bob.oswald@gmail.com'
  92. ctx.attributes = {
  93. email: ctx.email,
  94. analyticsId: '8055c676-bcc7-4e64-a66f-8069f9a0bd92',
  95. }
  96. })
  97. describe('createNewUser', function () {
  98. describe('with callbacks', function () {
  99. it('should take the opts and put them in the model', async function (ctx) {
  100. const user = await ctx.UserCreator.promises.createNewUser({
  101. ...ctx.attributes,
  102. holdingAccount: true,
  103. })
  104. assert.equal(user.email, ctx.email)
  105. assert.equal(user.holdingAccount, true)
  106. assert.equal(user.first_name, 'bob.oswald')
  107. })
  108. it('should use the start of the email if the first name is empty string', async function (ctx) {
  109. const user = await ctx.UserCreator.promises.createNewUser({
  110. ...ctx.attributes,
  111. holdingAccount: true,
  112. first_name: '',
  113. })
  114. assert.equal(user.email, ctx.email)
  115. assert.equal(user.holdingAccount, true)
  116. assert.equal(user.first_name, 'bob.oswald')
  117. })
  118. it('should use the first name if passed', async function (ctx) {
  119. const user = await ctx.UserCreator.promises.createNewUser({
  120. ...ctx.attributes,
  121. holdingAccount: true,
  122. first_name: 'fiiirstname',
  123. })
  124. assert.equal(user.email, ctx.email)
  125. assert.equal(user.holdingAccount, true)
  126. assert.equal(user.first_name, 'fiiirstname')
  127. })
  128. it('should use the last name if passed', async function (ctx) {
  129. const user = await ctx.UserCreator.promises.createNewUser({
  130. ...ctx.attributes,
  131. holdingAccount: true,
  132. last_name: 'lastNammmmeee',
  133. })
  134. assert.equal(user.email, ctx.email)
  135. assert.equal(user.holdingAccount, true)
  136. assert.equal(user.last_name, 'lastNammmmeee')
  137. })
  138. it('should set emails attribute', async function (ctx) {
  139. const user = await ctx.UserCreator.promises.createNewUser(
  140. ctx.attributes
  141. )
  142. user.email.should.equal(ctx.email)
  143. user.emails.length.should.equal(1)
  144. user.emails[0].email.should.equal(ctx.email)
  145. user.emails[0].createdAt.should.be.a('date')
  146. user.emails[0].reversedHostname.should.equal('moc.liamg')
  147. })
  148. describe('with affiliations feature', function () {
  149. let user
  150. beforeEach(function (ctx) {
  151. ctx.Features.hasFeature = sinon
  152. .stub()
  153. .withArgs('affiliations')
  154. .returns(true)
  155. })
  156. describe('when v1 affiliations API does not return an error', function () {
  157. beforeEach(async function (ctx) {
  158. user = await ctx.UserCreator.promises.createNewUser(ctx.attributes)
  159. })
  160. it('should flag that affiliation is unchecked', function () {
  161. user.emails[0].affiliationUnchecked.should.equal(true)
  162. })
  163. it('should try to add affiliation to v1', function (ctx) {
  164. sinon.assert.calledOnce(
  165. ctx.UserUpdater.promises.addAffiliationForNewUser
  166. )
  167. sinon.assert.calledWithMatch(
  168. ctx.UserUpdater.promises.addAffiliationForNewUser,
  169. user._id,
  170. ctx.email
  171. )
  172. })
  173. it('should query for updated user data', function (ctx) {
  174. sinon.assert.calledOnce(ctx.UserGetter.promises.getUser)
  175. })
  176. })
  177. describe('when v1 affiliations API does return an error', function () {
  178. beforeEach(async function (ctx) {
  179. ctx.UserUpdater.promises.addAffiliationForNewUser.rejects()
  180. user = await ctx.UserCreator.promises.createNewUser(ctx.attributes)
  181. })
  182. it('should flag that affiliation is unchecked', function () {
  183. user.emails[0].affiliationUnchecked.should.equal(true)
  184. })
  185. it('should try to add affiliation to v1', function (ctx) {
  186. sinon.assert.calledOnce(
  187. ctx.UserUpdater.promises.addAffiliationForNewUser
  188. )
  189. sinon.assert.calledWithMatch(
  190. ctx.UserUpdater.promises.addAffiliationForNewUser,
  191. user._id,
  192. ctx.email
  193. )
  194. })
  195. it('should not query for updated user data', function (ctx) {
  196. sinon.assert.notCalled(ctx.UserGetter.promises.getUser)
  197. })
  198. it('should log error', function (ctx) {
  199. sinon.assert.calledOnce(ctx.logger.error)
  200. })
  201. })
  202. describe('when v1 affiliations API returns an error and requireAffiliation=true', function () {
  203. beforeEach(async function (ctx) {
  204. ctx.UserUpdater.promises.addAffiliationForNewUser.rejects()
  205. user = await ctx.UserCreator.promises.createNewUser(ctx.attributes)
  206. })
  207. it('should flag that affiliation is unchecked', function () {
  208. user.emails[0].affiliationUnchecked.should.equal(true)
  209. })
  210. it('should try to add affiliation to v1', function (ctx) {
  211. sinon.assert.calledOnce(
  212. ctx.UserUpdater.promises.addAffiliationForNewUser
  213. )
  214. sinon.assert.calledWithMatch(
  215. ctx.UserUpdater.promises.addAffiliationForNewUser,
  216. user._id,
  217. ctx.email
  218. )
  219. })
  220. it('should not query for updated user data', function (ctx) {
  221. sinon.assert.notCalled(ctx.UserGetter.promises.getUser)
  222. })
  223. it('should log error', function (ctx) {
  224. sinon.assert.calledOnce(ctx.logger.error)
  225. })
  226. })
  227. })
  228. it('should not add affiliation when without affiliation feature', async function (ctx) {
  229. await ctx.UserCreator.promises.createNewUser(ctx.attributes)
  230. sinon.assert.notCalled(
  231. ctx.UserUpdater.promises.addAffiliationForNewUser
  232. )
  233. })
  234. })
  235. describe('with promises', function () {
  236. it('should take the opts and put them in the model', async function (ctx) {
  237. const opts = {
  238. ...ctx.attributes,
  239. holdingAccount: true,
  240. }
  241. const user = await ctx.UserCreator.promises.createNewUser(opts)
  242. assert.equal(user.email, ctx.email)
  243. assert.equal(user.holdingAccount, true)
  244. assert.equal(user.first_name, 'bob.oswald')
  245. })
  246. it('should add affiliation when with affiliation feature', async function (ctx) {
  247. ctx.Features.hasFeature = sinon
  248. .stub()
  249. .withArgs('affiliations')
  250. .returns(true)
  251. const user = await ctx.UserCreator.promises.createNewUser(
  252. ctx.attributes
  253. )
  254. sinon.assert.calledOnce(
  255. ctx.UserUpdater.promises.addAffiliationForNewUser
  256. )
  257. sinon.assert.calledWithMatch(
  258. ctx.UserUpdater.promises.addAffiliationForNewUser,
  259. user._id,
  260. ctx.email
  261. )
  262. })
  263. it('should not add affiliation when without affiliation feature', async function (ctx) {
  264. ctx.Features.hasFeature = sinon.stub().returns(false)
  265. await ctx.UserCreator.promises.createNewUser(ctx.attributes)
  266. sinon.assert.notCalled(
  267. ctx.UserUpdater.promises.addAffiliationForNewUser
  268. )
  269. })
  270. it('should include SAML provider ID with email', async function (ctx) {
  271. const attributes = {
  272. ...ctx.attributes,
  273. samlIdentifiers: [{ email: ctx.email, providerId: '1' }],
  274. }
  275. const user = await ctx.UserCreator.promises.createNewUser(attributes)
  276. assert.equal(user.emails[0].samlProviderId, '1')
  277. })
  278. it('should fire an analytics event and user property on registration', async function (ctx) {
  279. const user = await ctx.UserCreator.promises.createNewUser(
  280. ctx.attributes
  281. )
  282. assert.equal(user.email, ctx.email)
  283. sinon.assert.calledWith(
  284. ctx.Analytics.recordEventForMongoUserInBackground,
  285. user,
  286. 'user-registered'
  287. )
  288. sinon.assert.calledWith(
  289. ctx.Analytics.setUserPropertyForMongoUser,
  290. user,
  291. 'created-at'
  292. )
  293. })
  294. it('should schedule post registration jobs on registration with saas feature', async function (ctx) {
  295. ctx.Features.hasFeature = sinon.stub().withArgs('saas').returns(true)
  296. const user = await ctx.UserCreator.promises.createNewUser(
  297. ctx.attributes
  298. )
  299. assert.equal(user.email, ctx.email)
  300. sinon.assert.calledWith(
  301. ctx.UserOnboardingEmailManager.scheduleOnboardingEmail,
  302. user
  303. )
  304. sinon.assert.calledWith(
  305. ctx.UserPostRegistrationAnalyticsManager
  306. .schedulePostRegistrationAnalytics,
  307. user
  308. )
  309. })
  310. it('should not schedule post registration checks when without saas feature', async function (ctx) {
  311. await ctx.UserCreator.promises.createNewUser(ctx.attributes)
  312. sinon.assert.notCalled(
  313. ctx.UserOnboardingEmailManager.scheduleOnboardingEmail
  314. )
  315. sinon.assert.notCalled(
  316. ctx.UserPostRegistrationAnalyticsManager
  317. .schedulePostRegistrationAnalytics
  318. )
  319. })
  320. it('should throw without analyticsId', async function (ctx) {
  321. await expect(
  322. ctx.UserCreator.promises.createNewUser({ email: ctx.email })
  323. ).to.eventually.be.rejectedWith(
  324. /bug: attributes.analyticsId is missing/
  325. )
  326. })
  327. })
  328. })
  329. })