ReferalAllocatorTests.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 'Referal allocator', ->
  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. describe "when user does not have a subscription", ->
  66. beforeEach ->
  67. @refered_user_count = 3
  68. @Settings.bonus_features =
  69. "3":
  70. collaborators: 3
  71. dropbox: false
  72. versioning: false
  73. @User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
  74. @User.update = sinon.stub().callsArgWith 2, null
  75. @SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, null
  76. @ReferalAllocator.assignBonus @user_id, @callback
  77. it "should get the users subscription", ->
  78. @SubscriptionLocator.getUsersSubscription
  79. .calledWith(@user_id)
  80. .should.equal true
  81. it "should get the users number of refered user", ->
  82. @User.findOne
  83. .calledWith(_id: @user_id)
  84. .should.equal true
  85. it "should update the user to bonus features", ->
  86. @User.update
  87. .calledWith({
  88. _id: @user_id
  89. }, {
  90. $set:
  91. features:
  92. @Settings.bonus_features[@refered_user_count.toString()]
  93. })
  94. .should.equal true
  95. it "should call the callback", ->
  96. @callback.called.should.equal true
  97. describe "when user does not have a recurlySubscription_id", ->
  98. beforeEach ->
  99. @refered_user_count = 3
  100. @Settings.bonus_features =
  101. "3":
  102. collaborators: 3
  103. dropbox: false
  104. versioning: false
  105. @User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
  106. @User.update = sinon.stub().callsArgWith 2, null
  107. @SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, {}
  108. @ReferalAllocator.assignBonus @user_id, @callback
  109. it "should get the users subscription", ->
  110. @SubscriptionLocator.getUsersSubscription
  111. .calledWith(@user_id)
  112. .should.equal true
  113. it "should get the users number of refered user", ->
  114. @User.findOne
  115. .calledWith(_id: @user_id)
  116. .should.equal true
  117. it "should update the user to bonus features", ->
  118. @User.update
  119. .calledWith({
  120. _id: @user_id
  121. }, {
  122. $set:
  123. features:
  124. @Settings.bonus_features[@refered_user_count.toString()]
  125. })
  126. .should.equal true
  127. it "should call the callback", ->
  128. @callback.called.should.equal true
  129. describe "when the user is not at a bonus level", ->
  130. beforeEach ->
  131. @refered_user_count = 3
  132. @Settings.bonus_features =
  133. "4":
  134. collaborators: 3
  135. dropbox: false
  136. versioning: false
  137. @User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
  138. @User.update = sinon.stub().callsArgWith 2, null
  139. @SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, {}
  140. @ReferalAllocator.assignBonus @user_id, @callback
  141. it "should get the users subscription", ->
  142. @SubscriptionLocator.getUsersSubscription
  143. .calledWith(@user_id)
  144. .should.equal true
  145. it "should get the users number of refered user", ->
  146. @User.findOne
  147. .calledWith(_id: @user_id)
  148. .should.equal true
  149. it "should not update the user to bonus features", ->
  150. @User.update.called.should.equal false
  151. it "should call the callback", ->
  152. @callback.called.should.equal true
  153. describe "when user has a subscription", ->
  154. beforeEach ->
  155. @refered_user_count = 3
  156. @Settings.bonus_features =
  157. "3":
  158. collaborators: 3
  159. dropbox: false
  160. versioning: false
  161. @User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
  162. @User.update = sinon.stub().callsArgWith 2, null
  163. @SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, { planCode: "collaborator" }
  164. @ReferalAllocator.assignBonus @user_id, @callback
  165. it "should get the users subscription", ->
  166. @SubscriptionLocator.getUsersSubscription
  167. .calledWith(@user_id)
  168. .should.equal true
  169. it "should not get the users number of refered user", ->
  170. @User.findOne.called.should.equal false
  171. it "should not update the user to bonus features", ->
  172. @User.update.called.should.equal false
  173. it "should call the callback", ->
  174. @callback.called.should.equal true