BetaProgramControllerTests.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const MockResponse = require('../helpers/MockResponse')
  6. const modulePath = path.join(
  7. __dirname,
  8. '../../../../app/src/Features/BetaProgram/BetaProgramController'
  9. )
  10. describe('BetaProgramController', function () {
  11. beforeEach(function () {
  12. this.user = {
  13. _id: (this.user_id = 'a_simple_id'),
  14. email: 'user@example.com',
  15. features: {},
  16. betaProgram: false,
  17. }
  18. this.req = {
  19. query: {},
  20. session: {
  21. user: this.user,
  22. },
  23. }
  24. this.SplitTestHandler = {
  25. promises: {
  26. sessionMaintenance: sinon.stub(),
  27. },
  28. }
  29. this.BetaProgramController = SandboxedModule.require(modulePath, {
  30. requires: {
  31. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  32. './BetaProgramHandler': (this.BetaProgramHandler = {
  33. promises: {
  34. optIn: sinon.stub().resolves(),
  35. optOut: sinon.stub().resolves(),
  36. },
  37. }),
  38. '../User/UserGetter': (this.UserGetter = {
  39. promises: {
  40. getUser: sinon.stub().resolves(),
  41. },
  42. }),
  43. '@overleaf/settings': (this.settings = {
  44. languages: {},
  45. }),
  46. '../Authentication/AuthenticationController':
  47. (this.AuthenticationController = {
  48. getLoggedInUserId: sinon.stub().returns(this.user._id),
  49. }),
  50. },
  51. })
  52. this.res = new MockResponse()
  53. this.next = sinon.stub()
  54. })
  55. describe('optIn', function () {
  56. it("should redirect to '/beta/participate'", function (done) {
  57. this.res.callback = () => {
  58. this.res.redirectedTo.should.equal('/beta/participate')
  59. done()
  60. }
  61. this.BetaProgramController.optIn(this.req, this.res, done)
  62. })
  63. it('should not call next with an error', function () {
  64. this.BetaProgramController.optIn(this.req, this.res, this.next)
  65. this.next.callCount.should.equal(0)
  66. })
  67. it('should call BetaProgramHandler.optIn', function () {
  68. this.BetaProgramController.optIn(this.req, this.res, this.next)
  69. this.BetaProgramHandler.promises.optIn.callCount.should.equal(1)
  70. })
  71. it('should invoke the session maintenance', function (done) {
  72. this.res.callback = () => {
  73. this.SplitTestHandler.promises.sessionMaintenance.should.have.been.calledWith(
  74. this.req
  75. )
  76. done()
  77. }
  78. this.BetaProgramController.optIn(this.req, this.res, done)
  79. })
  80. describe('when BetaProgramHandler.opIn produces an error', function () {
  81. beforeEach(function () {
  82. this.BetaProgramHandler.promises.optIn.throws(new Error('woops'))
  83. })
  84. it("should not redirect to '/beta/participate'", function () {
  85. this.BetaProgramController.optIn(this.req, this.res, this.next)
  86. this.res.redirect.callCount.should.equal(0)
  87. })
  88. it('should produce an error', function (done) {
  89. this.BetaProgramController.optIn(this.req, this.res, err => {
  90. expect(err).to.be.instanceof(Error)
  91. done()
  92. })
  93. })
  94. })
  95. })
  96. describe('optOut', function () {
  97. it("should redirect to '/beta/participate'", function (done) {
  98. this.res.callback = () => {
  99. expect(this.res.redirectedTo).to.equal('/beta/participate')
  100. done()
  101. }
  102. this.BetaProgramController.optOut(this.req, this.res, done)
  103. })
  104. it('should not call next with an error', function (done) {
  105. this.res.callback = () => {
  106. this.next.callCount.should.equal(0)
  107. done()
  108. }
  109. this.BetaProgramController.optOut(this.req, this.res, done)
  110. })
  111. it('should call BetaProgramHandler.optOut', function (done) {
  112. this.res.callback = () => {
  113. this.BetaProgramHandler.promises.optOut.callCount.should.equal(1)
  114. done()
  115. }
  116. this.BetaProgramController.optOut(this.req, this.res, done)
  117. })
  118. it('should invoke the session maintenance', function (done) {
  119. this.res.callback = () => {
  120. this.SplitTestHandler.promises.sessionMaintenance.should.have.been.calledWith(
  121. this.req,
  122. null
  123. )
  124. done()
  125. }
  126. this.BetaProgramController.optOut(this.req, this.res, done)
  127. })
  128. describe('when BetaProgramHandler.optOut produces an error', function () {
  129. beforeEach(function () {
  130. this.BetaProgramHandler.promises.optOut.throws(new Error('woops'))
  131. })
  132. it("should not redirect to '/beta/participate'", function (done) {
  133. this.BetaProgramController.optOut(this.req, this.res, error => {
  134. expect(error).to.exist
  135. expect(this.res.redirected).to.equal(false)
  136. done()
  137. })
  138. })
  139. it('should produce an error', function (done) {
  140. this.BetaProgramController.optOut(this.req, this.res, error => {
  141. expect(error).to.exist
  142. done()
  143. })
  144. })
  145. })
  146. })
  147. describe('optInPage', function () {
  148. beforeEach(function () {
  149. this.UserGetter.promises.getUser.resolves(this.user)
  150. })
  151. it('should render the opt-in page', function (done) {
  152. this.res.callback = () => {
  153. expect(this.res.renderedTemplate).to.equal('beta_program/opt_in')
  154. done()
  155. }
  156. this.BetaProgramController.optInPage(this.req, this.res, done)
  157. })
  158. describe('when UserGetter.getUser produces an error', function () {
  159. beforeEach(function () {
  160. this.UserGetter.promises.getUser.throws(new Error('woops'))
  161. })
  162. it('should not render the opt-in page', function () {
  163. this.BetaProgramController.optInPage(this.req, this.res, this.next)
  164. this.res.render.callCount.should.equal(0)
  165. })
  166. it('should produce an error', function (done) {
  167. this.BetaProgramController.optInPage(this.req, this.res, error => {
  168. expect(error).to.exist
  169. expect(error).to.be.instanceof(Error)
  170. done()
  171. })
  172. })
  173. })
  174. })
  175. })