ReferalAllocatorTests.coffee 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. sinon = require('sinon')
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/Referal/ReferalAllocator.js'
  6. describe 'Referalallocator', ->
  7. beforeEach ->
  8. @ReferalAllocator = SandboxedModule.require modulePath, requires:
  9. '../../models/User': User: @User = {}
  10. "../Subscription/SubscriptionLocator": @SubscriptionLocator = {}
  11. "settings-sharelatex": @Settings = {}
  12. 'logger-sharelatex':
  13. log:->
  14. err:->
  15. @callback = sinon.stub()
  16. @referal_id = "referal-id-123"
  17. @referal_medium = "twitter"
  18. @user_id = "user-id-123"
  19. @new_user_id = "new-user-id-123"
  20. describe "allocate", ->
  21. describe "when the referal was a bonus referal", ->
  22. beforeEach ->
  23. @referal_source = "bonus"
  24. @User.update = sinon.stub().callsArgWith 3, null
  25. @User.findOne = sinon.stub().callsArgWith 1, null, { _id: @user_id }
  26. @ReferalAllocator.assignBonus = sinon.stub().callsArg 1
  27. @ReferalAllocator.allocate @referal_id, @new_user_id, @referal_source, @referal_medium, @callback
  28. it 'should update the referring user with the refered users id', ->
  29. @User.update.calledWith({
  30. "referal_id":@referal_id
  31. }, {
  32. $push:
  33. refered_users: @new_user_id
  34. $inc:
  35. refered_user_count: 1
  36. }).should.equal true
  37. it "find the referring users id", ->
  38. @User.findOne
  39. .calledWith( referal_id: @referal_id )
  40. .should.equal true
  41. it "shoudl assign the user their bonus", ->
  42. @ReferalAllocator.assignBonus
  43. .calledWith(@user_id)
  44. .should.equal true
  45. it "should call the callback", ->
  46. @callback.called.should.equal true
  47. describe "when the referal is not a bonus referal", ->
  48. beforeEach ->
  49. @referal_source = "public_share"
  50. @User.update = sinon.stub().callsArgWith 3, null
  51. @User.findOne = sinon.stub().callsArgWith 1, null, { _id: @user_id }
  52. @ReferalAllocator.assignBonus = sinon.stub().callsArg 1
  53. @ReferalAllocator.allocate @referal_id, @new_user_id, @referal_source, @referal_medium, @callback
  54. it 'should not update the referring user with the refered users id', ->
  55. @User.update.called.should.equal false
  56. it "find the referring users id", ->
  57. @User.findOne
  58. .calledWith( referal_id: @referal_id )
  59. .should.equal true
  60. it "should not assign the user a bonus", ->
  61. @ReferalAllocator.assignBonus.called.should.equal false
  62. it "should call the callback", ->
  63. @callback.called.should.equal true
  64. describe "assignBonus", ->
  65. beforeEach ->
  66. @refered_user_count = 3
  67. @Settings.bonus_features =
  68. "3":
  69. collaborators: 3
  70. dropbox: false
  71. versioning: false
  72. stubbedUser = {
  73. refered_user_count: @refered_user_count,
  74. features:{collaborators:1, dropbox:false, versioning:false}
  75. }
  76. @User.findOne = sinon.stub().callsArgWith 1, null, stubbedUser
  77. @User.update = sinon.stub().callsArgWith 2, null
  78. @ReferalAllocator.assignBonus @user_id, @callback
  79. it "should get the users number of refered user", ->
  80. @User.findOne
  81. .calledWith(_id: @user_id)
  82. .should.equal true
  83. it "should update the user to bonus features", ->
  84. @User.update
  85. .calledWith({
  86. _id: @user_id
  87. }, {
  88. $set:
  89. features:
  90. @Settings.bonus_features[@refered_user_count.toString()]
  91. })
  92. .should.equal true
  93. it "should call the callback", ->
  94. @callback.called.should.equal true
  95. describe "when there is nothing to assign", ->
  96. beforeEach ->
  97. @ReferalAllocator._calculateBonuses = sinon.stub().returns({})
  98. @stubbedUser =
  99. refered_user_count:4
  100. features:{collaborators:3, versioning:true, dropbox:false}
  101. @Settings.bonus_features =
  102. "4":
  103. collaborators:3
  104. versioning:true
  105. dropbox:false
  106. @User.findOne = sinon.stub().callsArgWith 1, null, @stubbedUser
  107. @User.update = sinon.stub().callsArgWith 2, null
  108. it "should not call update if there are no bonuses to apply", (done)->
  109. @ReferalAllocator.assignBonus @user_id, (err)=>
  110. @User.update.called.should.equal false
  111. done()
  112. describe "when the user has better features already", ->
  113. beforeEach ->
  114. @refered_user_count = 3
  115. @stubbedUser =
  116. refered_user_count:4
  117. features:
  118. collaborators:3
  119. dropbox:false
  120. versioning:false
  121. @Settings.bonus_features =
  122. "4":
  123. collaborators: 10
  124. dropbox: true
  125. versioning: false
  126. @User.findOne = sinon.stub().callsArgWith 1, null, @stubbedUser
  127. @User.update = sinon.stub().callsArgWith 2, null
  128. it "should not set in in mongo when the feature is better", (done)->
  129. @ReferalAllocator.assignBonus @user_id, =>
  130. @User.update.calledWith({_id: @user_id }, {$set: features:{dropbox:true, versioning:false, collaborators:10} }).should.equal true
  131. done()
  132. it "should not overright if the user has -1 users", (done)->
  133. @stubbedUser.features.collaborators = -1
  134. @ReferalAllocator.assignBonus @user_id, =>
  135. @User.update.calledWith({_id: @user_id }, {$set: features:{dropbox:true, versioning:false, collaborators:-1} }).should.equal true
  136. done()
  137. describe "when the user is not at a bonus level", ->
  138. beforeEach ->
  139. @refered_user_count = 0
  140. @Settings.bonus_features =
  141. "1":
  142. collaborators: 3
  143. dropbox: false
  144. versioning: false
  145. @User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
  146. @User.update = sinon.stub().callsArgWith 2, null
  147. @ReferalAllocator.assignBonus @user_id, @callback
  148. it "should get the users number of refered user", ->
  149. @User.findOne
  150. .calledWith(_id: @user_id)
  151. .should.equal true
  152. it "should not update the user to bonus features", ->
  153. @User.update.called.should.equal false
  154. it "should call the callback", ->
  155. @callback.called.should.equal true