2014-02-12 05:23:40 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
should = require('chai').should()
|
|
|
|
sinon = require 'sinon'
|
|
|
|
assert = require("chai").assert
|
|
|
|
modulePath = "../../../../app/js/Features/Subscription/SubscriptionGroupController"
|
|
|
|
|
|
|
|
|
|
|
|
describe "Subscription Group Controller", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@user = {_id:"!@312431"}
|
|
|
|
@subscription = {}
|
|
|
|
@GroupHandler =
|
|
|
|
addUserToGroup: sinon.stub().callsArgWith(2, null, @user)
|
|
|
|
removeUserFromGroup: sinon.stub().callsArgWith(2)
|
2015-05-27 15:50:16 -04:00
|
|
|
isUserPartOfGroup: sinon.stub()
|
|
|
|
sendVerificationEmail:sinon.stub()
|
|
|
|
processGroupVerification:sinon.stub()
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
@SubscriptionLocator = getUsersSubscription: sinon.stub().callsArgWith(1, null, @subscription)
|
|
|
|
|
2015-05-27 15:57:54 -04:00
|
|
|
@SubscriptionDomainHandler =
|
2015-05-27 15:50:16 -04:00
|
|
|
findDomainLicenceBySubscriptionId:sinon.stub()
|
|
|
|
|
|
|
|
@OneTimeTokenHandler =
|
|
|
|
getValueFromTokenAndExpire:sinon.stub()
|
|
|
|
|
|
|
|
|
|
|
|
@ErrorsController =
|
|
|
|
notFound:sinon.stub()
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
@Controller = SandboxedModule.require modulePath, requires:
|
|
|
|
"./SubscriptionGroupHandler":@GroupHandler
|
|
|
|
"logger-sharelatex": log:->
|
|
|
|
"./SubscriptionLocator": @SubscriptionLocator
|
2015-05-27 15:57:54 -04:00
|
|
|
"./SubscriptionDomainHandler":@SubscriptionDomainHandler
|
2015-05-27 15:50:16 -04:00
|
|
|
"../Errors/ErrorController":@ErrorsController
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
@adminUserId = "123jlkj"
|
2015-05-27 15:50:16 -04:00
|
|
|
@subscription_id = "123434325412"
|
|
|
|
@user_email = "bob@gmail.com"
|
2014-02-12 05:23:40 -05:00
|
|
|
@req =
|
|
|
|
session:
|
2015-05-27 15:50:16 -04:00
|
|
|
user:
|
|
|
|
_id: @adminUserId
|
|
|
|
email:@user_email
|
|
|
|
params:
|
|
|
|
subscription_id:@subscription_id
|
|
|
|
query:{}
|
|
|
|
|
|
|
|
@token = "super-secret-token"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
describe "addUserToGroup", ->
|
|
|
|
|
|
|
|
it "should use the admin id for the logged in user and take the email address from the body", (done)->
|
|
|
|
newEmail = "31231"
|
|
|
|
@req.body = email: newEmail
|
|
|
|
res =
|
|
|
|
json : (data)=>
|
|
|
|
@GroupHandler.addUserToGroup.calledWith(@adminUserId, newEmail).should.equal true
|
|
|
|
data.user.should.deep.equal @user
|
|
|
|
done()
|
|
|
|
@Controller.addUserToGroup @req, res
|
|
|
|
|
|
|
|
|
|
|
|
describe "removeUserFromGroup", ->
|
|
|
|
it "should use the admin id for the logged in user and take the email address from the body", (done)->
|
|
|
|
userIdToRemove = "31231"
|
|
|
|
@req.params = user_id: userIdToRemove
|
|
|
|
|
|
|
|
res =
|
|
|
|
send : =>
|
|
|
|
@GroupHandler.removeUserFromGroup.calledWith(@adminUserId, userIdToRemove).should.equal true
|
|
|
|
done()
|
|
|
|
@Controller.removeUserFromGroup @req, res
|
|
|
|
|
|
|
|
|
|
|
|
describe "renderSubscriptionGroupAdminPage", ->
|
|
|
|
it "should redirect you if you don't have a group account", (done)->
|
|
|
|
@subscription.group = false
|
|
|
|
|
|
|
|
res =
|
|
|
|
redirect : (path)=>
|
|
|
|
path.should.equal("/")
|
|
|
|
done()
|
|
|
|
@Controller.renderSubscriptionGroupAdminPage @req, res
|
2015-05-27 15:50:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe "renderGroupInvitePage", ->
|
|
|
|
describe "with a valid licence", ->
|
|
|
|
beforeEach ->
|
2015-05-27 15:57:54 -04:00
|
|
|
@SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({subscription_id:@subscription_id, adminUser_id:@adminUserId})
|
2015-05-27 15:50:16 -04:00
|
|
|
|
|
|
|
it "should render subscriptions/group/invite if not part of group", (done)->
|
|
|
|
@GroupHandler.isUserPartOfGroup.callsArgWith(2, null, false)
|
|
|
|
res =
|
|
|
|
render : (pageName)=>
|
|
|
|
pageName.should.equal "subscriptions/group/invite"
|
|
|
|
done()
|
|
|
|
@Controller.renderGroupInvitePage @req, res
|
|
|
|
|
|
|
|
it "should redirect to custom page if is already part of group", (done)->
|
|
|
|
@GroupHandler.isUserPartOfGroup.callsArgWith(2, null, true)
|
|
|
|
res =
|
|
|
|
redirect : (location)=>
|
|
|
|
location.should.equal "/user/subscription/custom_account"
|
|
|
|
done()
|
|
|
|
@Controller.renderGroupInvitePage @req, res
|
|
|
|
|
|
|
|
describe "without a valid licence", ->
|
|
|
|
beforeEach ->
|
2015-05-27 15:57:54 -04:00
|
|
|
@SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
|
2015-05-27 15:50:16 -04:00
|
|
|
|
|
|
|
it "should send a 500", (done)->
|
|
|
|
@Controller.renderGroupInvitePage @req, {}
|
|
|
|
@ErrorsController.notFound.called.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe "beginJoinGroup", ->
|
|
|
|
describe "with a valid licence", ->
|
|
|
|
beforeEach ->
|
|
|
|
@licenceName = "get amazing licence"
|
2015-05-27 15:57:54 -04:00
|
|
|
@SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
|
2015-05-27 15:50:16 -04:00
|
|
|
@GroupHandler.sendVerificationEmail.callsArgWith(3)
|
|
|
|
|
|
|
|
it "should ask the SubscriptionGroupHandler to send the verification email", (done)->
|
|
|
|
res =
|
|
|
|
send : (statusCode)=>
|
|
|
|
statusCode.should.equal 200
|
|
|
|
@GroupHandler.sendVerificationEmail.calledWith(@subscription_id, @licenceName, @user_email).should.equal true
|
|
|
|
done()
|
|
|
|
@Controller.beginJoinGroup @req, res
|
|
|
|
|
|
|
|
describe "without a valid licence", ->
|
|
|
|
beforeEach ->
|
2015-05-27 15:57:54 -04:00
|
|
|
@SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
|
2015-05-27 15:50:16 -04:00
|
|
|
|
|
|
|
it "should send a 500", (done)->
|
|
|
|
@Controller.beginJoinGroup @req, {}
|
|
|
|
@ErrorsController.notFound.called.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
describe "completeJoin", ->
|
|
|
|
describe "with a valid licence", ->
|
|
|
|
beforeEach ->
|
|
|
|
@GroupHandler.processGroupVerification.callsArgWith(3)
|
2015-05-27 15:57:54 -04:00
|
|
|
@SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
|
2015-05-27 15:50:16 -04:00
|
|
|
|
|
|
|
it "should redirect to the success page upon processGroupVerification", (done)->
|
|
|
|
@req.query.token = @token
|
|
|
|
res =
|
|
|
|
redirect : (location)=>
|
|
|
|
@GroupHandler.processGroupVerification.calledWith(@user_email, @subscription_id, @token).should.equal true
|
|
|
|
location.should.equal "/user/subscription/#{@subscription_id}/group/successful-join"
|
|
|
|
done()
|
|
|
|
@Controller.completeJoin @req, res
|
|
|
|
|
|
|
|
describe "without a valid licence", ->
|
|
|
|
beforeEach ->
|
2015-05-27 15:57:54 -04:00
|
|
|
@SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
|
2015-05-27 15:50:16 -04:00
|
|
|
|
|
|
|
it "should send a 500", (done)->
|
|
|
|
@Controller.completeJoin @req, {}
|
|
|
|
@ErrorsController.notFound.called.should.equal true
|
|
|
|
done()
|