UserCreatorTests.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const SandboxedModule = require('sandboxed-module')
  2. const chai = require('chai')
  3. const sinon = require('sinon')
  4. const assert = chai.assert
  5. const modulePath = '../../../../app/src/Features/User/UserCreator.js'
  6. describe('UserCreator', function() {
  7. beforeEach(function() {
  8. const self = this
  9. this.user = { _id: '12390i', ace: {} }
  10. this.user.save = sinon.stub().resolves(self.user)
  11. this.UserModel = class Project {
  12. constructor() {
  13. return self.user
  14. }
  15. }
  16. this.UserGetter = { getUserByMainEmail: sinon.stub() }
  17. this.addAffiliation = sinon.stub().yields()
  18. this.UserCreator = SandboxedModule.require(modulePath, {
  19. globals: {
  20. console: console
  21. },
  22. requires: {
  23. '../../models/User': {
  24. User: this.UserModel
  25. },
  26. 'logger-sharelatex': { log: sinon.stub(), err: sinon.stub() },
  27. 'metrics-sharelatex': { timeAsyncMethod() {} },
  28. '../Institutions/InstitutionsAPI': {
  29. addAffiliation: this.addAffiliation
  30. }
  31. }
  32. })
  33. this.email = 'bob.oswald@gmail.com'
  34. })
  35. describe('createNewUser', function() {
  36. describe('with callbacks', function() {
  37. it('should take the opts and put them in the model', function(done) {
  38. const opts = {
  39. email: this.email,
  40. holdingAccount: true
  41. }
  42. this.UserCreator.createNewUser(opts, (err, user) => {
  43. assert.ifError(err)
  44. assert.equal(user.email, this.email)
  45. assert.equal(user.holdingAccount, true)
  46. assert.equal(user.first_name, 'bob.oswald')
  47. done()
  48. })
  49. })
  50. it('should use the start of the email if the first name is empty string', function(done) {
  51. const opts = {
  52. email: this.email,
  53. holdingAccount: true,
  54. first_name: ''
  55. }
  56. this.UserCreator.createNewUser(opts, (err, user) => {
  57. assert.ifError(err)
  58. assert.equal(user.email, this.email)
  59. assert.equal(user.holdingAccount, true)
  60. assert.equal(user.first_name, 'bob.oswald')
  61. done()
  62. })
  63. })
  64. it('should use the first name if passed', function(done) {
  65. const opts = {
  66. email: this.email,
  67. holdingAccount: true,
  68. first_name: 'fiiirstname'
  69. }
  70. this.UserCreator.createNewUser(opts, (err, user) => {
  71. assert.ifError(err)
  72. assert.equal(user.email, this.email)
  73. assert.equal(user.holdingAccount, true)
  74. assert.equal(user.first_name, 'fiiirstname')
  75. done()
  76. })
  77. })
  78. it('should use the last name if passed', function(done) {
  79. const opts = {
  80. email: this.email,
  81. holdingAccount: true,
  82. last_name: 'lastNammmmeee'
  83. }
  84. this.UserCreator.createNewUser(opts, (err, user) => {
  85. assert.ifError(err)
  86. assert.equal(user.email, this.email)
  87. assert.equal(user.holdingAccount, true)
  88. assert.equal(user.last_name, 'lastNammmmeee')
  89. done()
  90. })
  91. })
  92. it('should set emails attribute', function(done) {
  93. this.UserCreator.createNewUser({ email: this.email }, (err, user) => {
  94. assert.ifError(err)
  95. user.email.should.equal(this.email)
  96. user.emails.length.should.equal(1)
  97. user.emails[0].email.should.equal(this.email)
  98. user.emails[0].createdAt.should.be.a('date')
  99. user.emails[0].reversedHostname.should.equal('moc.liamg')
  100. done()
  101. })
  102. })
  103. it('should add affiliation', function(done) {
  104. const attributes = { email: this.email }
  105. this.UserCreator.createNewUser(attributes, (err, user) => {
  106. assert.ifError(err)
  107. sinon.assert.calledOnce(this.addAffiliation)
  108. sinon.assert.calledWithMatch(
  109. this.addAffiliation,
  110. user._id,
  111. this.email
  112. )
  113. done()
  114. })
  115. })
  116. it('should not add affiliation if skipping', function(done) {
  117. const attributes = { email: this.email }
  118. const options = { skip_affiliation: true }
  119. this.UserCreator.createNewUser(attributes, options, (err, user) => {
  120. assert.ifError(err)
  121. process.nextTick(() => {
  122. sinon.assert.notCalled(this.addAffiliation)
  123. done()
  124. })
  125. })
  126. })
  127. })
  128. describe('with promises', function() {
  129. it('should take the opts and put them in the model', async function() {
  130. const opts = {
  131. email: this.email,
  132. holdingAccount: true
  133. }
  134. const user = await this.UserCreator.promises.createNewUser(opts)
  135. assert.equal(user.email, this.email)
  136. assert.equal(user.holdingAccount, true)
  137. assert.equal(user.first_name, 'bob.oswald')
  138. })
  139. it('should add affiliation', async function() {
  140. const attributes = { email: this.email }
  141. const user = await this.UserCreator.promises.createNewUser(attributes)
  142. sinon.assert.calledOnce(this.addAffiliation)
  143. sinon.assert.calledWithMatch(this.addAffiliation, user._id, this.email)
  144. })
  145. it('should not add affiliation if skipping', async function() {
  146. const attributes = { email: this.email }
  147. const opts = { skip_affiliation: true }
  148. await this.UserCreator.promises.createNewUser(attributes, opts)
  149. sinon.assert.notCalled(this.addAffiliation)
  150. })
  151. it('should include SAML provider ID with email', async function() {
  152. const attributes = {
  153. email: this.email,
  154. samlIdentifiers: [{ email: this.email, providerId: '1' }]
  155. }
  156. const user = await this.UserCreator.promises.createNewUser(attributes)
  157. assert.equal(user.emails[0].samlProviderId, '1')
  158. })
  159. })
  160. })
  161. })