UserCreatorTests.coffee 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. assert = require("assert")
  4. should = chai.should()
  5. modulePath = "../../../../app/js/Features/User/UserCreator.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "UserCreator", ->
  8. beforeEach ->
  9. self = @
  10. @user = {_id:"12390i", ace: {}}
  11. @user.save = sinon.stub().callsArgWith(0)
  12. @UserModel = class Project
  13. constructor: ->
  14. return self.user
  15. @UserGetter =
  16. getUserByMainEmail: sinon.stub()
  17. @addAffiliation = sinon.stub().yields()
  18. @UserCreator = SandboxedModule.require modulePath, requires:
  19. "../../models/User": User:@UserModel
  20. "logger-sharelatex":{ log: sinon.stub(), err: sinon.stub() }
  21. 'metrics-sharelatex': {timeAsyncMethod: ()->}
  22. "../Institutions/InstitutionsAPI": addAffiliation: @addAffiliation
  23. @email = "bob.oswald@gmail.com"
  24. describe "createNewUser", ->
  25. it "should take the opts and put them in the model", (done)->
  26. opts =
  27. email:@email
  28. holdingAccount:true
  29. @UserCreator.createNewUser opts, (err, user)=>
  30. assert.equal user.email, @email
  31. assert.equal user.holdingAccount, true
  32. assert.equal user.first_name, "bob.oswald"
  33. done()
  34. it "should use the start of the email if the first name is empty string", (done)->
  35. opts =
  36. email:@email
  37. holdingAccount:true
  38. first_name:""
  39. @UserCreator.createNewUser opts, (err, user)=>
  40. assert.equal user.email, @email
  41. assert.equal user.holdingAccount, true
  42. assert.equal user.first_name, "bob.oswald"
  43. done()
  44. it "should use the first name if passed", (done)->
  45. opts =
  46. email:@email
  47. holdingAccount:true
  48. first_name:"fiiirstname"
  49. @UserCreator.createNewUser opts, (err, user)=>
  50. assert.equal user.email, @email
  51. assert.equal user.holdingAccount, true
  52. assert.equal user.first_name, "fiiirstname"
  53. done()
  54. it "should use the last name if passed", (done)->
  55. opts =
  56. email:@email
  57. holdingAccount:true
  58. last_name:"lastNammmmeee"
  59. @UserCreator.createNewUser opts, (err, user)=>
  60. assert.equal user.email, @email
  61. assert.equal user.holdingAccount, true
  62. assert.equal user.last_name, "lastNammmmeee"
  63. done()
  64. it "should set emails attribute", (done)->
  65. @UserCreator.createNewUser email: @email, (err, user)=>
  66. user.email.should.equal @email
  67. user.emails.length.should.equal 1
  68. user.emails[0].email.should.equal @email
  69. user.emails[0].createdAt.should.be.a 'date'
  70. done()
  71. it "should add affiliation in background", (done)->
  72. @UserCreator.createNewUser email: @email, (err, user) =>
  73. # addaffiliation should not be called before the callback but only after
  74. # a tick of the event loop
  75. sinon.assert.notCalled(@addAffiliation)
  76. process.nextTick () =>
  77. sinon.assert.calledWith(@addAffiliation, user._id, user.email)
  78. done()
  79. it "should not add affiliation if skipping", (done)->
  80. attributes = email: @email
  81. options = skip_affiliation: true
  82. @UserCreator.createNewUser attributes, options, (err, user) =>
  83. process.nextTick () =>
  84. sinon.assert.notCalled(@addAffiliation)
  85. done()