ReferalAllocator.test.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { vi } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/Features/Referal/ReferalAllocator.mjs'
  4. describe('ReferalAllocator', function () {
  5. beforeEach(async function (ctx) {
  6. vi.doMock('../../../../app/src/models/User.js', () => ({
  7. User: (ctx.User = {}),
  8. }))
  9. vi.doMock(
  10. '../../../../app/src/Features/Subscription/FeaturesUpdater.js',
  11. () => ({
  12. default: (ctx.FeaturesUpdater = {}),
  13. })
  14. )
  15. vi.doMock('@overleaf/settings', () => ({
  16. default: (ctx.Settings = {}),
  17. }))
  18. ctx.ReferalAllocator = (await import(modulePath)).default
  19. ctx.referal_id = 'referal-id-123'
  20. ctx.referal_medium = 'twitter'
  21. ctx.user_id = 'user-id-123'
  22. ctx.new_user_id = 'new-user-id-123'
  23. ctx.FeaturesUpdater.promises = {
  24. refreshFeatures: sinon.stub().resolves(),
  25. }
  26. ctx.User.updateOne = sinon.stub().returns({
  27. exec: sinon.stub().resolves(),
  28. })
  29. ctx.User.findOne = sinon.stub().returns({
  30. exec: sinon.stub().resolves({ _id: ctx.user_id }),
  31. })
  32. })
  33. describe('allocate', function () {
  34. describe('when the referal was a bonus referal', function () {
  35. beforeEach(async function (ctx) {
  36. ctx.referal_source = 'bonus'
  37. await ctx.ReferalAllocator.promises.allocate(
  38. ctx.referal_id,
  39. ctx.new_user_id,
  40. ctx.referal_source,
  41. ctx.referal_medium
  42. )
  43. })
  44. it('should update the referring user with the refered users id', function (ctx) {
  45. ctx.User.updateOne
  46. .calledWith(
  47. {
  48. referal_id: ctx.referal_id,
  49. },
  50. {
  51. $push: {
  52. refered_users: ctx.new_user_id,
  53. },
  54. $inc: {
  55. refered_user_count: 1,
  56. },
  57. }
  58. )
  59. .should.equal(true)
  60. })
  61. it('find the referring users id', function (ctx) {
  62. ctx.User.findOne
  63. .calledWith({ referal_id: ctx.referal_id })
  64. .should.equal(true)
  65. })
  66. it("should refresh the user's subscription", function (ctx) {
  67. ctx.FeaturesUpdater.promises.refreshFeatures
  68. .calledWith(ctx.user_id)
  69. .should.equal(true)
  70. })
  71. })
  72. describe('when there is no user for the referal id', function () {
  73. beforeEach(async function (ctx) {
  74. ctx.referal_source = 'bonus'
  75. ctx.referal_id = 'wombat'
  76. ctx.User.findOne = sinon.stub().returns({
  77. exec: sinon.stub().resolves(null),
  78. })
  79. await ctx.ReferalAllocator.promises.allocate(
  80. ctx.referal_id,
  81. ctx.new_user_id,
  82. ctx.referal_source,
  83. ctx.referal_medium
  84. )
  85. })
  86. it('should find the referring users id', function (ctx) {
  87. ctx.User.findOne
  88. .calledWith({ referal_id: ctx.referal_id })
  89. .should.equal(true)
  90. })
  91. it('should not update the referring user with the refered users id', function (ctx) {
  92. ctx.User.updateOne.called.should.equal(false)
  93. })
  94. it('should not assign the user a bonus', function (ctx) {
  95. ctx.FeaturesUpdater.promises.refreshFeatures.called.should.equal(false)
  96. })
  97. })
  98. describe('when the referal is not a bonus referal', function () {
  99. beforeEach(async function (ctx) {
  100. ctx.referal_source = 'public_share'
  101. await ctx.ReferalAllocator.promises.allocate(
  102. ctx.referal_id,
  103. ctx.new_user_id,
  104. ctx.referal_source,
  105. ctx.referal_medium
  106. )
  107. })
  108. it('should not update the referring user with the refered users id', function (ctx) {
  109. ctx.User.updateOne.called.should.equal(false)
  110. })
  111. it('find the referring users id', function (ctx) {
  112. ctx.User.findOne
  113. .calledWith({ referal_id: ctx.referal_id })
  114. .should.equal(true)
  115. })
  116. it('should not assign the user a bonus', function (ctx) {
  117. ctx.FeaturesUpdater.promises.refreshFeatures.called.should.equal(false)
  118. })
  119. })
  120. })
  121. })