BetaProgramControllerTests.coffee 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. },
  23. "../User/UserLocator": @UserLocator = {
  24. findById: sinon.stub()
  25. },
  26. "settings-sharelatex": @settings = {
  27. languages: {}
  28. }
  29. "logger-sharelatex": @logger = {
  30. log: sinon.stub()
  31. err: sinon.stub()
  32. error: sinon.stub()
  33. }
  34. @res =
  35. send: sinon.stub()
  36. redirect: sinon.stub()
  37. render: sinon.stub()
  38. @next = sinon.stub()
  39. describe "optIn", ->
  40. beforeEach ->
  41. @BetaProgramHandler.optIn.callsArgWith(1, null)
  42. it "should redirect to '/'", () ->
  43. @BetaProgramController.optIn @req, @res, @next
  44. @res.redirect.callCount.should.equal 1
  45. it "should not call next with an error", () ->
  46. @BetaProgramController.optIn @req, @res, @next
  47. @next.callCount.should.equal 0
  48. it "should not call next with an error", () ->
  49. @BetaProgramController.optIn @req, @res, @next
  50. @next.callCount.should.equal 0
  51. it "should call BetaProgramHandler.optIn", () ->
  52. @BetaProgramController.optIn @req, @res, @next
  53. @BetaProgramHandler.optIn.callCount.should.equal 1
  54. describe "when BetaProgramHandler.opIn produces an error", ->
  55. beforeEach ->
  56. @BetaProgramHandler.optIn.callsArgWith(1, new Error('woops'))
  57. it "should not redirect to '/'", () ->
  58. @BetaProgramController.optIn @req, @res, @next
  59. @res.redirect.callCount.should.equal 0
  60. it "should produce an error", () ->
  61. @BetaProgramController.optIn @req, @res, @next
  62. @next.callCount.should.equal 1
  63. @next.firstCall.args[0].should.be.instanceof Error
  64. describe "optInPage", ->
  65. beforeEach ->
  66. @UserLocator.findById.callsArgWith(1, null, @user)
  67. it "should render the opt-in page", () ->
  68. @BetaProgramController.optInPage @req, @res, @next
  69. @res.render.callCount.should.equal 1
  70. args = @res.render.firstCall.args
  71. args[0].should.equal 'beta_program/opt_in'
  72. describe "when UserLocator.findById produces an error", ->
  73. beforeEach ->
  74. @UserLocator.findById.callsArgWith(1, new Error('woops'))
  75. it "should not render the opt-in page", () ->
  76. @BetaProgramController.optInPage @req, @res, @next
  77. @res.render.callCount.should.equal 0
  78. it "should produce an error", () ->
  79. @BetaProgramController.optInPage @req, @res, @next
  80. @next.callCount.should.equal 1
  81. @next.firstCall.args[0].should.be.instanceof Error