BetaProgramControllerTests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const modulePath = path.join(
  5. __dirname,
  6. '../../../../app/src/Features/BetaProgram/BetaProgramController'
  7. )
  8. describe('BetaProgramController', function () {
  9. beforeEach(function () {
  10. this.user = {
  11. _id: (this.user_id = 'a_simple_id'),
  12. email: 'user@example.com',
  13. features: {},
  14. betaProgram: false,
  15. }
  16. this.req = {
  17. query: {},
  18. session: {
  19. user: this.user,
  20. },
  21. }
  22. this.BetaProgramController = SandboxedModule.require(modulePath, {
  23. requires: {
  24. './BetaProgramHandler': (this.BetaProgramHandler = {
  25. optIn: sinon.stub(),
  26. optOut: sinon.stub(),
  27. }),
  28. '../User/UserGetter': (this.UserGetter = {
  29. getUser: sinon.stub(),
  30. }),
  31. '@overleaf/settings': (this.settings = {
  32. languages: {},
  33. }),
  34. '../Authentication/AuthenticationController':
  35. (this.AuthenticationController = {
  36. getLoggedInUserId: sinon.stub().returns(this.user._id),
  37. }),
  38. },
  39. })
  40. this.res = {
  41. send: sinon.stub(),
  42. redirect: sinon.stub(),
  43. render: sinon.stub(),
  44. }
  45. this.next = sinon.stub()
  46. })
  47. describe('optIn', function () {
  48. beforeEach(function () {
  49. this.BetaProgramHandler.optIn.callsArgWith(1, null)
  50. })
  51. it("should redirect to '/beta/participate'", function () {
  52. this.BetaProgramController.optIn(this.req, this.res, this.next)
  53. this.res.redirect.callCount.should.equal(1)
  54. this.res.redirect.firstCall.args[0].should.equal('/beta/participate')
  55. })
  56. it('should not call next with an error', function () {
  57. this.BetaProgramController.optIn(this.req, this.res, this.next)
  58. this.next.callCount.should.equal(0)
  59. })
  60. it('should call BetaProgramHandler.optIn', function () {
  61. this.BetaProgramController.optIn(this.req, this.res, this.next)
  62. this.BetaProgramHandler.optIn.callCount.should.equal(1)
  63. })
  64. describe('when BetaProgramHandler.opIn produces an error', function () {
  65. beforeEach(function () {
  66. this.BetaProgramHandler.optIn.callsArgWith(1, new Error('woops'))
  67. })
  68. it("should not redirect to '/beta/participate'", function () {
  69. this.BetaProgramController.optIn(this.req, this.res, this.next)
  70. this.res.redirect.callCount.should.equal(0)
  71. })
  72. it('should produce an error', function () {
  73. this.BetaProgramController.optIn(this.req, this.res, this.next)
  74. this.next.callCount.should.equal(1)
  75. this.next.firstCall.args[0].should.be.instanceof(Error)
  76. })
  77. })
  78. })
  79. describe('optOut', function () {
  80. beforeEach(function () {
  81. this.BetaProgramHandler.optOut.callsArgWith(1, null)
  82. })
  83. it("should redirect to '/beta/participate'", function () {
  84. this.BetaProgramController.optOut(this.req, this.res, this.next)
  85. this.res.redirect.callCount.should.equal(1)
  86. this.res.redirect.firstCall.args[0].should.equal('/beta/participate')
  87. })
  88. it('should not call next with an error', function () {
  89. this.BetaProgramController.optOut(this.req, this.res, this.next)
  90. this.next.callCount.should.equal(0)
  91. })
  92. it('should call BetaProgramHandler.optOut', function () {
  93. this.BetaProgramController.optOut(this.req, this.res, this.next)
  94. this.BetaProgramHandler.optOut.callCount.should.equal(1)
  95. })
  96. describe('when BetaProgramHandler.optOut produces an error', function () {
  97. beforeEach(function () {
  98. this.BetaProgramHandler.optOut.callsArgWith(1, new Error('woops'))
  99. })
  100. it("should not redirect to '/beta/participate'", function () {
  101. this.BetaProgramController.optOut(this.req, this.res, this.next)
  102. this.res.redirect.callCount.should.equal(0)
  103. })
  104. it('should produce an error', function () {
  105. this.BetaProgramController.optOut(this.req, this.res, this.next)
  106. this.next.callCount.should.equal(1)
  107. this.next.firstCall.args[0].should.be.instanceof(Error)
  108. })
  109. })
  110. })
  111. describe('optInPage', function () {
  112. beforeEach(function () {
  113. this.UserGetter.getUser.callsArgWith(1, null, this.user)
  114. })
  115. it('should render the opt-in page', function () {
  116. this.BetaProgramController.optInPage(this.req, this.res, this.next)
  117. this.res.render.callCount.should.equal(1)
  118. const { args } = this.res.render.firstCall
  119. args[0].should.equal('beta_program/opt_in')
  120. })
  121. describe('when UserGetter.getUser produces an error', function () {
  122. beforeEach(function () {
  123. this.UserGetter.getUser.callsArgWith(1, new Error('woops'))
  124. })
  125. it('should not render the opt-in page', function () {
  126. this.BetaProgramController.optInPage(this.req, this.res, this.next)
  127. this.res.render.callCount.should.equal(0)
  128. })
  129. it('should produce an error', function () {
  130. this.BetaProgramController.optInPage(this.req, this.res, this.next)
  131. this.next.callCount.should.equal(1)
  132. this.next.firstCall.args[0].should.be.instanceof(Error)
  133. })
  134. })
  135. })
  136. })