Fix unit tests

This commit is contained in:
Alberto Fernández Capel 2018-06-28 15:19:41 +01:00
parent 193579070c
commit 18ded60619
3 changed files with 31 additions and 18 deletions

View file

@ -62,7 +62,7 @@ module.exports = UserGetter =
user_ids = user_ids.map (u) -> ObjectId(u.toString())
catch error
return callback error
db.users.find { _id: { $in: user_ids} }, projection, callback
getUserOrUserStubById: (user_id, projection, callback = (error, user) ->) ->

View file

@ -8,7 +8,7 @@ describe 'ReferalFeatures', ->
beforeEach ->
@ReferalFeatures = SandboxedModule.require modulePath, requires:
'../User/UserGetter': @UserGetter = {}
'../../models/User': User: @User = {}
"settings-sharelatex": @Settings = {}
'logger-sharelatex':
log:->
@ -27,17 +27,17 @@ describe 'ReferalFeatures', ->
collaborators: 3
dropbox: false
versioning: false
stubbedUser = {
refered_user_count: @refered_user_count,
features:{collaborators:1, dropbox:false, versioning:false}
stubbedUser = {
refered_user_count: @refered_user_count,
features:{collaborators:1, dropbox:false, versioning:false}
}
@UserGetter.getUserOrUserStubById = sinon.stub().yields null, stubbedUser
@User.findOne = sinon.stub().callsArgWith 1, null, stubbedUser
@ReferalFeatures.getBonusFeatures @user_id, @callback
it "should get the users number of refered user", ->
@UserGetter.getUserOrUserStubById
.calledWith(@user_id, null)
@User.findOne
.calledWith(_id: @user_id)
.should.equal true
it "should call the callback with the features", ->
@ -51,14 +51,15 @@ describe 'ReferalFeatures', ->
collaborators: 3
dropbox: false
versioning: false
@UserGetter.getUserOrUserStubById =
sinon.stub().yields null, { refered_user_count: @refered_user_count }
@User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
@ReferalFeatures.getBonusFeatures @user_id, @callback
it "should get the users number of refered user", ->
@UserGetter.getUserOrUserStubById
.calledWith(@user_id, null)
@User.findOne
.calledWith(_id: @user_id)
.should.equal true
it "should call the callback with no features", ->
@callback.calledWith(null, {}).should.equal true

View file

@ -58,6 +58,11 @@ describe "SubscriptionUpdater", ->
@PlansLocator =
findLocalPlanInSettings: sinon.stub().returns({})
@UserGetter =
getUsers: (memberIds, projection, callback) ->
users = memberIds.map (id) -> { _id: id }
callback(null, users)
@ReferalFeatures = getBonusFeatures: sinon.stub().callsArgWith(1)
@Modules = {hooks: {fire: sinon.stub().callsArgWith(2, null, null)}}
@SubscriptionUpdater = SandboxedModule.require modulePath, requires:
@ -74,7 +79,6 @@ describe "SubscriptionUpdater", ->
describe "syncSubscription", ->
beforeEach ->
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
@SubscriptionUpdater._updateSubscriptionFromRecurly = sinon.stub().callsArgWith(2)
@ -88,7 +92,6 @@ describe "SubscriptionUpdater", ->
done()
it "should not call updateFeatures with group subscription if recurly subscription is not expired", (done)->
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
@SubscriptionUpdater._updateSubscriptionFromRecurly.called.should.equal true
@ -144,7 +147,6 @@ describe "SubscriptionUpdater", ->
done()
describe "_createNewSubscription", ->
it "should create a new subscription then update the subscription", (done)->
@SubscriptionUpdater._createNewSubscription @adminUser._id, =>
@ -154,15 +156,25 @@ describe "SubscriptionUpdater", ->
done()
describe "addUserToGroup", ->
beforeEach ->
@SubscriptionUpdater.addUsersToGroup = sinon.stub().yields(null)
it "delegates to addUsersToGroup", (done)->
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
@SubscriptionUpdater.addUsersToGroup
.calledWith(@adminUser._id, [@otherUserId]).should.equal true
done()
describe "addUsersToGroup", ->
beforeEach ->
@FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
it "should add the users id to the group as a set", (done)->
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
it "should add the user ids to the group as a set", (done)->
@SubscriptionUpdater.addUsersToGroup @adminUser._id, [@otherUserId], =>
searchOps =
admin_id: @adminUser._id
insertOperation =
"$addToSet": {member_ids:@otherUserId}
{ $push: { member_ids: { $each: [@otherUserId] } } }
@findAndModifyStub.calledWith(searchOps, insertOperation).should.equal true
done()