BetaProgramHandlerTests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const modulePath = path.join(
  4. __dirname,
  5. '../../../../app/src/Features/BetaProgram/BetaProgramHandler'
  6. )
  7. const sinon = require('sinon')
  8. const { expect } = require('chai')
  9. describe('BetaProgramHandler', function() {
  10. beforeEach(function() {
  11. this.user_id = 'some_id'
  12. this.user = {
  13. _id: this.user_id,
  14. email: 'user@example.com',
  15. features: {},
  16. betaProgram: false,
  17. save: sinon.stub().callsArgWith(0, null)
  18. }
  19. this.handler = SandboxedModule.require(modulePath, {
  20. globals: {
  21. console: console
  22. },
  23. requires: {
  24. 'metrics-sharelatex': (this.logger = {
  25. inc: sinon.stub()
  26. }),
  27. '../User/UserUpdater': (this.UserUpdater = {
  28. promises: {
  29. updateUser: sinon.stub().resolves()
  30. }
  31. })
  32. }
  33. })
  34. })
  35. describe('optIn', function() {
  36. beforeEach(function() {
  37. this.user.betaProgram = false
  38. this.call = callback => {
  39. this.handler.optIn(this.user_id, callback)
  40. }
  41. })
  42. it('should call userUpdater', function(done) {
  43. this.call(err => {
  44. expect(err).to.not.exist
  45. this.UserUpdater.promises.updateUser.callCount.should.equal(1)
  46. done()
  47. })
  48. })
  49. it('should not produce an error', function(done) {
  50. this.call(err => {
  51. expect(err).to.not.exist
  52. done()
  53. })
  54. })
  55. describe('when userUpdater produces an error', function() {
  56. beforeEach(function() {
  57. this.UserUpdater.promises.updateUser.rejects()
  58. })
  59. it('should produce an error', function(done) {
  60. this.call(err => {
  61. expect(err).to.exist
  62. expect(err).to.be.instanceof(Error)
  63. done()
  64. })
  65. })
  66. })
  67. })
  68. describe('optOut', function() {
  69. beforeEach(function() {
  70. this.user.betaProgram = true
  71. this.call = callback => {
  72. this.handler.optOut(this.user_id, callback)
  73. }
  74. })
  75. it('should call userUpdater', function(done) {
  76. this.call(err => {
  77. expect(err).to.not.exist
  78. this.UserUpdater.promises.updateUser.callCount.should.equal(1)
  79. done()
  80. })
  81. })
  82. it('should not produce an error', function(done) {
  83. this.call(err => {
  84. expect(err).to.not.exist
  85. done()
  86. })
  87. })
  88. describe('when userUpdater produces an error', function() {
  89. beforeEach(function() {
  90. this.UserUpdater.promises.updateUser.rejects()
  91. })
  92. it('should produce an error', function(done) {
  93. this.call(err => {
  94. expect(err).to.exist
  95. expect(err).to.be.instanceof(Error)
  96. done()
  97. })
  98. })
  99. })
  100. })
  101. })