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

103 lines
2.9 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()
},
"../User/UserLocator": @UserLocator = {
findById: sinon.stub()
},
"settings-sharelatex": @settings = {
languages: {}
}
"logger-sharelatex": @logger = {
log: sinon.stub()
err: sinon.stub()
error: sinon.stub()
}
@res =
send: sinon.stub()
redirect: sinon.stub()
render: sinon.stub()
@next = sinon.stub()
describe "optIn", ->
beforeEach ->
@BetaProgramHandler.optIn.callsArgWith(1, null)
2016-06-08 06:04:44 -04:00
it "should redirect to '/beta/opt-in'", () ->
2016-06-07 06:15:56 -04:00
@BetaProgramController.optIn @req, @res, @next
@res.redirect.callCount.should.equal 1
2016-06-08 06:04:44 -04:00
@res.redirect.firstCall.args[0].should.equal "/beta/opt-in"
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'))
it "should not redirect to '/'", () ->
@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
describe "optInPage", ->
beforeEach ->
@UserLocator.findById.callsArgWith(1, null, @user)
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 UserLocator.findById produces an error", ->
beforeEach ->
@UserLocator.findById.callsArgWith(1, new Error('woops'))
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