UserOnboardingTests.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const { expect } = require('chai')
  2. const async = require('async')
  3. const User = require('./helpers/User')
  4. const request = require('./helpers/request')
  5. const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb')
  6. const _ = require('underscore')
  7. describe('UserOnboardingTests', function() {
  8. beforeEach(function(done) {
  9. // 2 new users
  10. this.user1 = new User()
  11. this.user2 = new User()
  12. // 1 older
  13. this.user3 = new User()
  14. this.user3._id = ObjectId('5d15fca20000000000000000')
  15. async.series(
  16. [
  17. cb => db.users.insert(this.user3, cb),
  18. this.user1.ensureUserExists.bind(this.user1),
  19. this.user2.ensureUserExists.bind(this.user2)
  20. ],
  21. done
  22. )
  23. })
  24. it('should send emails to the new users only', function(done) {
  25. request(
  26. {
  27. method: 'POST',
  28. url: '/user/onboarding_emails',
  29. auth: {
  30. username: 'sharelatex',
  31. password: 'password',
  32. sendImmediately: true
  33. }
  34. },
  35. (error, response, body) => {
  36. if (error != null) {
  37. throw error
  38. }
  39. // should have sent two emails to new users
  40. expect(response.statusCode).to.equal(200)
  41. expect(response.body).to.include(this.user1._id)
  42. expect(response.body).to.include(this.user2._id)
  43. expect(response.body).to.not.include(this.user3._id)
  44. // user 3 should still not have had an email sent
  45. const user3 = this.user3
  46. db.users
  47. .find({
  48. onboardingEmailSentAt: null
  49. })
  50. .toArray((error, users) => {
  51. if (error != null) {
  52. throw error
  53. }
  54. const ids = _.map(users, user => user._id.toString())
  55. expect(ids.length).to.equal(1)
  56. expect(ids).to.include(user3._id.toString())
  57. done()
  58. })
  59. }
  60. )
  61. })
  62. })