BetaProgramControllerTests.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/UserLocator": @UserLocator = {
  25. findById: 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. @res =
  36. send: sinon.stub()
  37. redirect: sinon.stub()
  38. render: sinon.stub()
  39. @next = sinon.stub()
  40. describe "optIn", ->
  41. beforeEach ->
  42. @BetaProgramHandler.optIn.callsArgWith(1, null)
  43. it "should redirect to '/beta/participate'", () ->
  44. @BetaProgramController.optIn @req, @res, @next
  45. @res.redirect.callCount.should.equal 1
  46. @res.redirect.firstCall.args[0].should.equal "/beta/participate"
  47. it "should not call next with an error", () ->
  48. @BetaProgramController.optIn @req, @res, @next
  49. @next.callCount.should.equal 0
  50. it "should not call next with an error", () ->
  51. @BetaProgramController.optIn @req, @res, @next
  52. @next.callCount.should.equal 0
  53. it "should call BetaProgramHandler.optIn", () ->
  54. @BetaProgramController.optIn @req, @res, @next
  55. @BetaProgramHandler.optIn.callCount.should.equal 1
  56. describe "when BetaProgramHandler.opIn produces an error", ->
  57. beforeEach ->
  58. @BetaProgramHandler.optIn.callsArgWith(1, new Error('woops'))
  59. it "should not redirect to '/beta/participate'", () ->
  60. @BetaProgramController.optIn @req, @res, @next
  61. @res.redirect.callCount.should.equal 0
  62. it "should produce an error", () ->
  63. @BetaProgramController.optIn @req, @res, @next
  64. @next.callCount.should.equal 1
  65. @next.firstCall.args[0].should.be.instanceof Error
  66. describe "optOut", ->
  67. beforeEach ->
  68. @BetaProgramHandler.optOut.callsArgWith(1, null)
  69. it "should redirect to '/beta/participate'", () ->
  70. @BetaProgramController.optOut @req, @res, @next
  71. @res.redirect.callCount.should.equal 1
  72. @res.redirect.firstCall.args[0].should.equal "/beta/participate"
  73. it "should not call next with an error", () ->
  74. @BetaProgramController.optOut @req, @res, @next
  75. @next.callCount.should.equal 0
  76. it "should not call next with an error", () ->
  77. @BetaProgramController.optOut @req, @res, @next
  78. @next.callCount.should.equal 0
  79. it "should call BetaProgramHandler.optOut", () ->
  80. @BetaProgramController.optOut @req, @res, @next
  81. @BetaProgramHandler.optOut.callCount.should.equal 1
  82. describe "when BetaProgramHandler.optOut produces an error", ->
  83. beforeEach ->
  84. @BetaProgramHandler.optOut.callsArgWith(1, new Error('woops'))
  85. it "should not redirect to '/beta/participate'", () ->
  86. @BetaProgramController.optOut @req, @res, @next
  87. @res.redirect.callCount.should.equal 0
  88. it "should produce an error", () ->
  89. @BetaProgramController.optOut @req, @res, @next
  90. @next.callCount.should.equal 1
  91. @next.firstCall.args[0].should.be.instanceof Error
  92. describe "optInPage", ->
  93. beforeEach ->
  94. @UserLocator.findById.callsArgWith(1, null, @user)
  95. it "should render the opt-in page", () ->
  96. @BetaProgramController.optInPage @req, @res, @next
  97. @res.render.callCount.should.equal 1
  98. args = @res.render.firstCall.args
  99. args[0].should.equal 'beta_program/opt_in'
  100. describe "when UserLocator.findById produces an error", ->
  101. beforeEach ->
  102. @UserLocator.findById.callsArgWith(1, new Error('woops'))
  103. it "should not render the opt-in page", () ->
  104. @BetaProgramController.optInPage @req, @res, @next
  105. @res.render.callCount.should.equal 0
  106. it "should produce an error", () ->
  107. @BetaProgramController.optInPage @req, @res, @next
  108. @next.callCount.should.equal 1
  109. @next.firstCall.args[0].should.be.instanceof Error