BetaProgramControllerTests.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 '/beta/opt-in'", () ->
  43. @BetaProgramController.optIn @req, @res, @next
  44. @res.redirect.callCount.should.equal 1
  45. @res.redirect.firstCall.args[0].should.equal "/beta/opt-in"
  46. it "should not call next with an error", () ->
  47. @BetaProgramController.optIn @req, @res, @next
  48. @next.callCount.should.equal 0
  49. it "should not call next with an error", () ->
  50. @BetaProgramController.optIn @req, @res, @next
  51. @next.callCount.should.equal 0
  52. it "should call BetaProgramHandler.optIn", () ->
  53. @BetaProgramController.optIn @req, @res, @next
  54. @BetaProgramHandler.optIn.callCount.should.equal 1
  55. describe "when BetaProgramHandler.opIn produces an error", ->
  56. beforeEach ->
  57. @BetaProgramHandler.optIn.callsArgWith(1, new Error('woops'))
  58. it "should not redirect to '/'", () ->
  59. @BetaProgramController.optIn @req, @res, @next
  60. @res.redirect.callCount.should.equal 0
  61. it "should produce an error", () ->
  62. @BetaProgramController.optIn @req, @res, @next
  63. @next.callCount.should.equal 1
  64. @next.firstCall.args[0].should.be.instanceof Error
  65. describe "optInPage", ->
  66. beforeEach ->
  67. @UserLocator.findById.callsArgWith(1, null, @user)
  68. it "should render the opt-in page", () ->
  69. @BetaProgramController.optInPage @req, @res, @next
  70. @res.render.callCount.should.equal 1
  71. args = @res.render.firstCall.args
  72. args[0].should.equal 'beta_program/opt_in'
  73. describe "when UserLocator.findById produces an error", ->
  74. beforeEach ->
  75. @UserLocator.findById.callsArgWith(1, new Error('woops'))
  76. it "should not render the opt-in page", () ->
  77. @BetaProgramController.optInPage @req, @res, @next
  78. @res.render.callCount.should.equal 0
  79. it "should produce an error", () ->
  80. @BetaProgramController.optInPage @req, @res, @next
  81. @next.callCount.should.equal 1
  82. @next.firstCall.args[0].should.be.instanceof Error