SubscriptionGroupHandlerTests.coffee 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. sinon = require 'sinon'
  4. assert = require("chai").assert
  5. modulePath = "../../../../app/js/Features/Subscription/SubscriptionGroupHandler"
  6. describe "Subscription Group Handler", ->
  7. beforeEach ->
  8. @adminUser_id = "12321"
  9. @newEmail = "bob@smith.com"
  10. @user_id = "3121321"
  11. @user = {_id:@user_id, email:@newEmail}
  12. @SubscriptionLocator =
  13. getUsersSubscription: sinon.stub()
  14. getSubscriptionByMemberIdAndId: sinon.stub()
  15. @UserCreator =
  16. getUserOrCreateHoldingAccount: sinon.stub().callsArgWith(1, null, @user)
  17. @SubscriptionUpdater =
  18. addUserToGroup: sinon.stub().callsArgWith(2)
  19. removeUserFromGroup: sinon.stub().callsArgWith(2)
  20. @UserLocator =
  21. findById: sinon.stub()
  22. @LimitationsManager =
  23. hasGroupMembersLimitReached: sinon.stub()
  24. @Handler = SandboxedModule.require modulePath, requires:
  25. "logger-sharelatex": log:->
  26. "../User/UserCreator": @UserCreator
  27. "./SubscriptionUpdater": @SubscriptionUpdater
  28. "./SubscriptionLocator": @SubscriptionLocator
  29. "../User/UserLocator": @UserLocator
  30. "./LimitationsManager": @LimitationsManager
  31. describe "addUserToGroup", ->
  32. it "should find or create the user", (done)->
  33. @LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, false)
  34. @Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
  35. @UserCreator.getUserOrCreateHoldingAccount.calledWith(@newEmail).should.equal true
  36. done()
  37. it "should add the user to the group", (done)->
  38. @LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, false)
  39. @Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
  40. @SubscriptionUpdater.addUserToGroup.calledWith(@adminUser_id, @user._id).should.equal true
  41. done()
  42. it "should not add the user to the group if the limit has been reached", (done)->
  43. @LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, true)
  44. @Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
  45. @SubscriptionUpdater.addUserToGroup.called.should.equal false
  46. done()
  47. it "should return error that limit has been reached", (done)->
  48. @LimitationsManager.hasGroupMembersLimitReached.callsArgWith(1, null, true)
  49. @Handler.addUserToGroup @adminUser_id, @newEmail, (err)=>
  50. err.limitReached.should.equal true
  51. done()
  52. describe "removeUserFromGroup", ->
  53. it "should call the subscription updater to remove the user", (done)->
  54. @Handler.removeUserFromGroup @adminUser_id, @user._id, (err)=>
  55. @SubscriptionUpdater.removeUserFromGroup.calledWith(@adminUser_id, @user._id).should.equal true
  56. done()
  57. describe "getPopulatedListOfMembers", ->
  58. beforeEach ->
  59. @subscription = {}
  60. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  61. @UserLocator.findById.callsArgWith(1, null, {_id:"31232"})
  62. it "should locate the subscription", (done)->
  63. @UserLocator.findById.callsArgWith(1, null, {_id:"31232"})
  64. @Handler.getPopulatedListOfMembers @adminUser_id, (err, users)=>
  65. @SubscriptionLocator.getUsersSubscription.calledWith(@adminUser_id).should.equal true
  66. done()
  67. it "should get the users by id", (done)->
  68. @UserLocator.findById.callsArgWith(1, null, {_id:"31232"})
  69. @subscription.member_ids = ["1234", "342432", "312312"]
  70. @Handler.getPopulatedListOfMembers @adminUser_id, (err, users)=>
  71. @UserLocator.findById.calledWith(@subscription.member_ids[0]).should.equal true
  72. @UserLocator.findById.calledWith(@subscription.member_ids[1]).should.equal true
  73. @UserLocator.findById.calledWith(@subscription.member_ids[2]).should.equal true
  74. users.length.should.equal @subscription.member_ids.length
  75. done()
  76. it "should just return the id if the user can not be found as they may have deleted their account", (done)->
  77. @UserLocator.findById.callsArgWith(1)
  78. @subscription.member_ids = ["1234", "342432", "312312"]
  79. @Handler.getPopulatedListOfMembers @adminUser_id, (err, users)=>
  80. assert.deepEqual users[0], {_id:@subscription.member_ids[0]}
  81. assert.deepEqual users[1], {_id:@subscription.member_ids[1]}
  82. assert.deepEqual users[2], {_id:@subscription.member_ids[2]}
  83. done()
  84. describe "isUserPartOfGroup", ->
  85. beforeEach ->
  86. @subscription_id = "123ed13123"
  87. it "should return true when user is part of subscription", (done)->
  88. @SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(2, null, {_id:@subscription_id})
  89. @Handler.isUserPartOfGroup @user_id, @subscription_id, (err, partOfGroup)->
  90. partOfGroup.should.equal true
  91. done()
  92. it "should return false when no subscription is found", (done)->
  93. @SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(2, null)
  94. @Handler.isUserPartOfGroup @user_id, @subscription_id, (err, partOfGroup)->
  95. partOfGroup.should.equal false
  96. done()