ReferalAllocatorTests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const SandboxedModule = require('sandboxed-module')
  2. require('chai').should()
  3. const sinon = require('sinon')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../../app/src/Features/Referal/ReferalAllocator.js'
  7. )
  8. describe('ReferalAllocator', function() {
  9. beforeEach(function() {
  10. this.ReferalAllocator = SandboxedModule.require(modulePath, {
  11. globals: {
  12. console: console
  13. },
  14. requires: {
  15. '../../models/User': {
  16. User: (this.User = {})
  17. },
  18. '../Subscription/FeaturesUpdater': (this.FeaturesUpdater = {}),
  19. 'settings-sharelatex': (this.Settings = {}),
  20. 'logger-sharelatex': {
  21. log() {},
  22. err() {}
  23. }
  24. }
  25. })
  26. this.callback = sinon.stub()
  27. this.referal_id = 'referal-id-123'
  28. this.referal_medium = 'twitter'
  29. this.user_id = 'user-id-123'
  30. this.new_user_id = 'new-user-id-123'
  31. this.FeaturesUpdater.refreshFeatures = sinon.stub().yields()
  32. this.User.updateOne = sinon.stub().callsArgWith(3, null)
  33. this.User.findOne = sinon
  34. .stub()
  35. .callsArgWith(2, null, { _id: this.user_id })
  36. })
  37. describe('allocate', function() {
  38. describe('when the referal was a bonus referal', function() {
  39. beforeEach(function() {
  40. this.referal_source = 'bonus'
  41. this.ReferalAllocator.allocate(
  42. this.referal_id,
  43. this.new_user_id,
  44. this.referal_source,
  45. this.referal_medium,
  46. this.callback
  47. )
  48. })
  49. it('should update the referring user with the refered users id', function() {
  50. this.User.updateOne
  51. .calledWith(
  52. {
  53. referal_id: this.referal_id
  54. },
  55. {
  56. $push: {
  57. refered_users: this.new_user_id
  58. },
  59. $inc: {
  60. refered_user_count: 1
  61. }
  62. }
  63. )
  64. .should.equal(true)
  65. })
  66. it('find the referring users id', function() {
  67. this.User.findOne
  68. .calledWith({ referal_id: this.referal_id })
  69. .should.equal(true)
  70. })
  71. it("should refresh the user's subscription", function() {
  72. this.FeaturesUpdater.refreshFeatures
  73. .calledWith(this.user_id)
  74. .should.equal(true)
  75. })
  76. it('should call the callback', function() {
  77. this.callback.called.should.equal(true)
  78. })
  79. })
  80. describe('when there is no user for the referal id', function() {
  81. beforeEach(function() {
  82. this.referal_source = 'bonus'
  83. this.referal_id = 'wombat'
  84. this.User.findOne = sinon.stub().callsArgWith(2, null, null)
  85. this.ReferalAllocator.allocate(
  86. this.referal_id,
  87. this.new_user_id,
  88. this.referal_source,
  89. this.referal_medium,
  90. this.callback
  91. )
  92. })
  93. it('should find the referring users id', function() {
  94. this.User.findOne
  95. .calledWith({ referal_id: this.referal_id })
  96. .should.equal(true)
  97. })
  98. it('should not update the referring user with the refered users id', function() {
  99. this.User.updateOne.called.should.equal(false)
  100. })
  101. it('should not assign the user a bonus', function() {
  102. this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
  103. })
  104. it('should call the callback', function() {
  105. this.callback.called.should.equal(true)
  106. })
  107. })
  108. describe('when the referal is not a bonus referal', function() {
  109. beforeEach(function() {
  110. this.referal_source = 'public_share'
  111. this.ReferalAllocator.allocate(
  112. this.referal_id,
  113. this.new_user_id,
  114. this.referal_source,
  115. this.referal_medium,
  116. this.callback
  117. )
  118. })
  119. it('should not update the referring user with the refered users id', function() {
  120. this.User.updateOne.called.should.equal(false)
  121. })
  122. it('find the referring users id', function() {
  123. this.User.findOne
  124. .calledWith({ referal_id: this.referal_id })
  125. .should.equal(true)
  126. })
  127. it('should not assign the user a bonus', function() {
  128. this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
  129. })
  130. it('should call the callback', function() {
  131. this.callback.called.should.equal(true)
  132. })
  133. })
  134. })
  135. })