Merge remote-tracking branch 'origin/master' into afc-email-tokens

This commit is contained in:
Alberto Fernández Capel
2018-06-08 12:21:17 +01:00
22 changed files with 470 additions and 134 deletions
@@ -27,6 +27,11 @@ describe 'ExportsHandler', ->
@project_history_id = 987
@user_id = "user-id-456"
@brand_variation_id = 789
@export_params = {
project_id: @project_id,
brand_variation_id: @brand_variation_id,
user_id: @user_id
}
@callback = sinon.stub()
describe 'exportProject', ->
@@ -35,13 +40,13 @@ describe 'ExportsHandler', ->
@response_body = {iAmAResponseBody: true}
@ExportsHandler._buildExport = sinon.stub().yields(null, @export_data)
@ExportsHandler._requestExport = sinon.stub().yields(null, @response_body)
@ExportsHandler.exportProject @project_id, @user_id, @brand_variation_id, (error, export_data) =>
@ExportsHandler.exportProject @export_params, (error, export_data) =>
@callback(error, export_data)
done()
it "should build the export", ->
@ExportsHandler._buildExport
.calledWith(@project_id, @user_id, @brand_variation_id)
.calledWith(@export_params)
.should.equal true
it "should request the export", ->
@@ -76,7 +81,7 @@ describe 'ExportsHandler', ->
describe "when all goes well", ->
beforeEach (done) ->
@ExportsHandler._buildExport @project_id, @user_id, @brand_variation_id, (error, export_data) =>
@ExportsHandler._buildExport @export_params, (error, export_data) =>
@callback(error, export_data)
done()
@@ -104,10 +109,40 @@ describe 'ExportsHandler', ->
@callback.calledWith(null, expected_export_data)
.should.equal true
describe "when we send replacement user first and last name", ->
beforeEach (done) ->
@custom_first_name = "FIRST"
@custom_last_name = "LAST"
@export_params.first_name = @custom_first_name
@export_params.last_name = @custom_last_name
@ExportsHandler._buildExport @export_params, (error, export_data) =>
@callback(error, export_data)
done()
it "should send the data from the user input", ->
expected_export_data =
project:
id: @project_id
rootDocPath: @rootDocPath
historyId: @project_history_id
historyVersion: @historyVersion
user:
id: @user_id
firstName: @custom_first_name
lastName: @custom_last_name
email: @user.email
orcidId: null
destination:
brandVariationId: @brand_variation_id
options:
callbackUrl: null
@callback.calledWith(null, expected_export_data)
.should.equal true
describe "when project is not found", ->
beforeEach (done) ->
@ProjectGetter.getProject = sinon.stub().yields(new Error("project not found"))
@ExportsHandler._buildExport @project_id, @user_id, @brand_variation_id, (error, export_data) =>
@ExportsHandler._buildExport @export_params, (error, export_data) =>
@callback(error, export_data)
done()
@@ -118,7 +153,7 @@ describe 'ExportsHandler', ->
describe "when project has no root doc", ->
beforeEach (done) ->
@ProjectLocator.findRootDoc = sinon.stub().yields(null, [null, null])
@ExportsHandler._buildExport @project_id, @user_id, @brand_variation_id, (error, export_data) =>
@ExportsHandler._buildExport @export_params, (error, export_data) =>
@callback(error, export_data)
done()
@@ -129,7 +164,7 @@ describe 'ExportsHandler', ->
describe "when user is not found", ->
beforeEach (done) ->
@UserGetter.getUser = sinon.stub().yields(new Error("user not found"))
@ExportsHandler._buildExport @project_id, @user_id, @brand_variation_id, (error, export_data) =>
@ExportsHandler._buildExport @export_params, (error, export_data) =>
@callback(error, export_data)
done()
@@ -140,7 +175,7 @@ describe 'ExportsHandler', ->
describe "when project history request fails", ->
beforeEach (done) ->
@ExportsHandler._requestVersion = sinon.stub().yields(new Error("project history call failed"))
@ExportsHandler._buildExport @project_id, @user_id, @brand_variation_id, (error, export_data) =>
@ExportsHandler._buildExport @export_params, (error, export_data) =>
@callback(error, export_data)
done()
@@ -22,6 +22,7 @@ describe "FeaturesUpdater", ->
describe "refreshFeatures", ->
beforeEach ->
@V1SubscriptionManager.notifyV1OfFeaturesChange = sinon.stub().yields()
@UserFeaturesUpdater.updateFeatures = sinon.stub().yields()
@FeaturesUpdater._getIndividualFeatures = sinon.stub().yields(null, { 'individual': 'features' })
@FeaturesUpdater._getGroupFeatureSets = sinon.stub().yields(null, [{ 'group': 'features' }, { 'group': 'features2' }])
@@ -29,48 +30,63 @@ describe "FeaturesUpdater", ->
@ReferalFeatures.getBonusFeatures = sinon.stub().yields(null, { 'bonus': 'features' })
@FeaturesUpdater._mergeFeatures = sinon.stub().returns({'merged': 'features'})
@callback = sinon.stub()
@FeaturesUpdater.refreshFeatures @user_id, @callback
it "should get the individual features", ->
@FeaturesUpdater._getIndividualFeatures
.calledWith(@user_id)
.should.equal true
describe "normally", ->
beforeEach ->
@FeaturesUpdater.refreshFeatures @user_id, @callback
it "should get the group features", ->
@FeaturesUpdater._getGroupFeatureSets
.calledWith(@user_id)
.should.equal true
it "should get the individual features", ->
@FeaturesUpdater._getIndividualFeatures
.calledWith(@user_id)
.should.equal true
it "should get the v1 features", ->
@FeaturesUpdater._getV1Features
.calledWith(@user_id)
.should.equal true
it "should get the group features", ->
@FeaturesUpdater._getGroupFeatureSets
.calledWith(@user_id)
.should.equal true
it "should get the bonus features", ->
@ReferalFeatures.getBonusFeatures
.calledWith(@user_id)
.should.equal true
it "should get the v1 features", ->
@FeaturesUpdater._getV1Features
.calledWith(@user_id)
.should.equal true
it "should merge from the default features", ->
@FeaturesUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).should.equal true
it "should get the bonus features", ->
@ReferalFeatures.getBonusFeatures
.calledWith(@user_id)
.should.equal true
it "should merge the individual features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': 'features' }).should.equal true
it "should merge from the default features", ->
@FeaturesUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).should.equal true
it "should merge the group features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features' }).should.equal true
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features2' }).should.equal true
it "should merge the individual features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': 'features' }).should.equal true
it "should merge the v1 features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'v1': 'features' }).should.equal true
it "should merge the group features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features' }).should.equal true
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features2' }).should.equal true
it "should merge the bonus features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'bonus': 'features' }).should.equal true
it "should merge the v1 features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'v1': 'features' }).should.equal true
it "should update the user with the merged features", ->
@UserFeaturesUpdater.updateFeatures
.calledWith(@user_id, {'merged': 'features'})
.should.equal true
it "should merge the bonus features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'bonus': 'features' }).should.equal true
it "should update the user with the merged features", ->
@UserFeaturesUpdater.updateFeatures
.calledWith(@user_id, {'merged': 'features'})
.should.equal true
it "should notify v1", ->
@V1SubscriptionManager.notifyV1OfFeaturesChange
.called.should.equal true
describe "with notifyV1 == false", ->
beforeEach ->
@FeaturesUpdater.refreshFeatures @user_id, false, @callback
it "should not notify v1", ->
@V1SubscriptionManager.notifyV1OfFeaturesChange
.called.should.equal false
describe "_mergeFeatures", ->
it "should prefer priority over standard for compileGroup", ->
@@ -24,6 +24,9 @@ describe "SubscriptionGroupHandler", ->
getSubscriptionByMemberIdAndId: sinon.stub()
getSubscription: sinon.stub()
@UserCreator =
getUserOrCreateHoldingAccount: sinon.stub().callsArgWith(1, null, @user)
@SubscriptionUpdater =
addUserToGroup: sinon.stub().callsArgWith(2)
removeUserFromGroup: sinon.stub().callsArgWith(2)
@@ -45,6 +48,9 @@ describe "SubscriptionGroupHandler", ->
@EmailHandler =
sendEmail:sinon.stub()
@Subscription =
update: sinon.stub().yields()
@settings =
siteUrl:"http://www.sharelatex.com"
@@ -58,6 +64,7 @@ describe "SubscriptionGroupHandler", ->
"./SubscriptionUpdater": @SubscriptionUpdater
"./TeamInvitesHandler": @TeamInvitesHandler
"./SubscriptionLocator": @SubscriptionLocator
"../../models/Subscription": Subscription: @Subscription
"../User/UserGetter": @UserGetter
"./LimitationsManager": @LimitationsManager
"../Security/OneTimeTokenHandler":@OneTimeTokenHandler
@@ -116,6 +123,35 @@ describe "SubscriptionGroupHandler", ->
@SubscriptionUpdater.removeUserFromGroup.calledWith(@adminUser_id, @user._id).should.equal true
done()
describe "replaceUserReferencesInGroups", ->
beforeEach ->
@oldId = "ba5eba11"
@newId = "5ca1ab1e"
it "replaces the admin_id", (done) ->
@Handler.replaceUserReferencesInGroups @oldId, @newId, (err) =>
@Subscription.update.calledWith(
{ admin_id: @oldId },
{ admin_id: @newId }
).should.equal true
done()
it "replaces the member ids", (done) ->
@Handler.replaceUserReferencesInGroups @oldId, @newId, (err) =>
@Subscription.update.calledWith(
{ member_ids: @oldId },
{ $addToSet: { member_ids: @newId } }
).should.equal true
@Subscription.update.calledWith(
{ member_ids: @oldId },
{ $pull: { member_ids: @oldId } }
).should.equal true
done()
describe "getPopulatedListOfMembers", ->
beforeEach ->
@@ -16,10 +16,10 @@ describe 'V1SubscriptionManager', ->
err: sinon.stub()
warn: sinon.stub()
"settings-sharelatex":
overleaf:
host: @host = "http://overleaf.example.com"
apis:
v1:
host: @host = "http://overleaf.example.com"
"request": @request = sinon.stub()
@V1SubscriptionManager._v1PlanRequest = sinon.stub()
@userId = 'abcd'
@v1UserId = 42
@user =
@@ -33,33 +33,20 @@ describe 'V1SubscriptionManager', ->
@responseBody =
id: 32,
plan_name: 'pro'
@UserGetter.getUser = sinon.stub()
.yields(null, @user)
@V1SubscriptionManager._v1PlanRequest = sinon.stub()
@V1SubscriptionManager._v1Request = sinon.stub()
.yields(null, @responseBody)
@call = (cb) =>
@V1SubscriptionManager.getPlanCodeFromV1 @userId, cb
describe 'when all goes well', ->
it 'should call getUser', (done) ->
it 'should call _v1Request', (done) ->
@call (err, planCode) =>
expect(
@UserGetter.getUser.callCount
@V1SubscriptionManager._v1Request.callCount
).to.equal 1
expect(
@UserGetter.getUser.calledWith(@userId)
).to.equal true
done()
it 'should call _v1PlanRequest', (done) ->
@call (err, planCode) =>
expect(
@V1SubscriptionManager._v1PlanRequest.callCount
).to.equal 1
expect(
@V1SubscriptionManager._v1PlanRequest.calledWith(
@v1UserId
@V1SubscriptionManager._v1Request.calledWith(
@userId
)
).to.equal true
done()
@@ -80,49 +67,56 @@ describe 'V1SubscriptionManager', ->
expect(planCode).to.equal null
done()
describe '_v1Request', ->
beforeEach ->
@UserGetter.getUser = sinon.stub()
.yields(null, @user)
describe 'when getUser produces an error', ->
beforeEach ->
@UserGetter.getUser = sinon.stub()
.yields(new Error('woops'))
@call = (cb) =>
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
it 'should not call _v1PlanRequest', (done) ->
it 'should not call request', (done) ->
@call (err, planCode) =>
expect(
@V1SubscriptionManager._v1PlanRequest.callCount
@request.callCount
).to.equal 0
done()
it 'should produce an error', (done) ->
@call (err, planCode) =>
expect(err).to.exist
expect(planCode).to.not.exist
done()
describe 'when getUser does not find a user', ->
beforeEach ->
@UserGetter.getUser = sinon.stub()
.yields(null, null)
@call = (cb) =>
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
it 'should not call _v1PlanRequest', (done) ->
it 'should not call request', (done) ->
@call (err, planCode) =>
expect(
@V1SubscriptionManager._v1PlanRequest.callCount
@request.callCount
).to.equal 0
done()
it 'should produce a null plan-code, without error', (done) ->
@call (err, planCode) =>
it 'should not error', (done) ->
@call (err) =>
expect(err).to.not.exist
expect(planCode).to.not.exist
done()
describe 'when the request to v1 fails', ->
beforeEach ->
@V1SubscriptionManager._v1PlanRequest = sinon.stub()
.yields(new Error('woops'))
@request.yields(new Error('woops'))
@call = (cb) =>
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
it 'should produce an error', (done) ->
@call (err, planCode) =>
@call (err) =>
expect(err).to.exist
expect(planCode).to.not.exist
done()