BetaProgramControllerTests.coffee 4.3 KB

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