UserCreatorTests.js 11 KB

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