overleaf/services/web/test/unit/coffee/BetaProgram/BetaProgramControllerTests.coffee

144 lines
4.3 KiB
CoffeeScript
Raw Normal View History

2016-06-07 06:15:56 -04:00
should = require('chai').should()
SandboxedModule = require('sandboxed-module')
assert = require('assert')
path = require('path')
sinon = require('sinon')
modulePath = path.join __dirname, "../../../../app/js/Features/BetaProgram/BetaProgramController"
expect = require("chai").expect
describe "BetaProgramController", ->
beforeEach ->
@user =
_id: @user_id = "a_simple_id"
email: "user@example.com"
features: {}
betaProgram: false
@req =
query: {}
session:
user: @user
@BetaProgramController = SandboxedModule.require modulePath, requires:
"./BetaProgramHandler": @BetaProgramHandler = {
optIn: sinon.stub()
2016-06-08 10:11:39 -04:00
optOut: sinon.stub()
2016-06-07 06:15:56 -04:00
},
"../User/UserGetter": @UserGetter = {
getUser: sinon.stub()
2016-06-07 06:15:56 -04:00
},
"settings-sharelatex": @settings = {
languages: {}
}
"logger-sharelatex": @logger = {
log: sinon.stub()
err: sinon.stub()
error: sinon.stub()
}
2016-09-22 09:30:34 -04:00
'../Authentication/AuthenticationController': @AuthenticationController = {
getLoggedInUserId: sinon.stub().returns(@user._id)
}
2016-06-07 06:15:56 -04:00
@res =
send: sinon.stub()
redirect: sinon.stub()
render: sinon.stub()
@next = sinon.stub()
describe "optIn", ->
beforeEach ->
@BetaProgramHandler.optIn.callsArgWith(1, null)
2016-06-08 10:11:39 -04:00
it "should redirect to '/beta/participate'", () ->
2016-06-07 06:15:56 -04:00
@BetaProgramController.optIn @req, @res, @next
@res.redirect.callCount.should.equal 1
2016-06-08 10:11:39 -04:00
@res.redirect.firstCall.args[0].should.equal "/beta/participate"
2016-06-07 06:15:56 -04:00
it "should not call next with an error", () ->
@BetaProgramController.optIn @req, @res, @next
@next.callCount.should.equal 0
it "should not call next with an error", () ->
@BetaProgramController.optIn @req, @res, @next
@next.callCount.should.equal 0
it "should call BetaProgramHandler.optIn", () ->
@BetaProgramController.optIn @req, @res, @next
@BetaProgramHandler.optIn.callCount.should.equal 1
describe "when BetaProgramHandler.opIn produces an error", ->
beforeEach ->
@BetaProgramHandler.optIn.callsArgWith(1, new Error('woops'))
2016-06-08 10:11:39 -04:00
it "should not redirect to '/beta/participate'", () ->
2016-06-07 06:15:56 -04:00
@BetaProgramController.optIn @req, @res, @next
@res.redirect.callCount.should.equal 0
it "should produce an error", () ->
@BetaProgramController.optIn @req, @res, @next
@next.callCount.should.equal 1
@next.firstCall.args[0].should.be.instanceof Error
2016-06-08 10:11:39 -04:00
describe "optOut", ->
beforeEach ->
@BetaProgramHandler.optOut.callsArgWith(1, null)
it "should redirect to '/beta/participate'", () ->
@BetaProgramController.optOut @req, @res, @next
@res.redirect.callCount.should.equal 1
@res.redirect.firstCall.args[0].should.equal "/beta/participate"
it "should not call next with an error", () ->
@BetaProgramController.optOut @req, @res, @next
@next.callCount.should.equal 0
it "should not call next with an error", () ->
@BetaProgramController.optOut @req, @res, @next
@next.callCount.should.equal 0
it "should call BetaProgramHandler.optOut", () ->
@BetaProgramController.optOut @req, @res, @next
@BetaProgramHandler.optOut.callCount.should.equal 1
describe "when BetaProgramHandler.optOut produces an error", ->
beforeEach ->
@BetaProgramHandler.optOut.callsArgWith(1, new Error('woops'))
it "should not redirect to '/beta/participate'", () ->
@BetaProgramController.optOut @req, @res, @next
@res.redirect.callCount.should.equal 0
it "should produce an error", () ->
@BetaProgramController.optOut @req, @res, @next
@next.callCount.should.equal 1
@next.firstCall.args[0].should.be.instanceof Error
2016-06-07 06:15:56 -04:00
describe "optInPage", ->
beforeEach ->
@UserGetter.getUser.callsArgWith(1, null, @user)
2016-06-07 06:15:56 -04:00
it "should render the opt-in page", () ->
@BetaProgramController.optInPage @req, @res, @next
@res.render.callCount.should.equal 1
args = @res.render.firstCall.args
args[0].should.equal 'beta_program/opt_in'
describe "when UserGetter.getUser produces an error", ->
2016-06-07 06:15:56 -04:00
beforeEach ->
@UserGetter.getUser.callsArgWith(1, new Error('woops'))
2016-06-07 06:15:56 -04:00
it "should not render the opt-in page", () ->
@BetaProgramController.optInPage @req, @res, @next
@res.render.callCount.should.equal 0
it "should produce an error", () ->
@BetaProgramController.optInPage @req, @res, @next
@next.callCount.should.equal 1
@next.firstCall.args[0].should.be.instanceof Error