UserFeaturesUpdaterTests.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const sinon = require('sinon')
  4. const modulePath =
  5. '../../../../app/src/Features/Subscription/UserFeaturesUpdater'
  6. describe('UserFeaturesUpdater', function () {
  7. beforeEach(function () {
  8. this.features = {
  9. collaborators: 6,
  10. dropbox: true,
  11. github: true,
  12. gitBridge: true,
  13. versioning: true,
  14. compileTimeout: 180,
  15. compileGroup: 'priority',
  16. references: true,
  17. templates: true,
  18. trackChanges: true,
  19. referencesSearch: true,
  20. zotero: true,
  21. mendeley: true,
  22. symbolPalette: true,
  23. }
  24. this.User = {
  25. findByIdAndUpdate: sinon.stub().returns({
  26. exec: sinon.stub().resolves({ features: this.features }),
  27. }),
  28. }
  29. this.UserFeaturesUpdater = SandboxedModule.require(modulePath, {
  30. requires: {
  31. '../../models/User': {
  32. User: this.User,
  33. },
  34. '@overleaf/settings': (this.Settings = {}),
  35. },
  36. })
  37. })
  38. describe('updateFeatures', function () {
  39. it('should send the users features', async function () {
  40. const userId = '5208dd34438842e2db000005'
  41. const update = {
  42. versioning: true,
  43. collaborators: 10,
  44. }
  45. const { features } =
  46. await this.UserFeaturesUpdater.promises.updateFeatures(userId, update)
  47. const updateArgs = this.User.findByIdAndUpdate.lastCall.args
  48. expect(updateArgs[0]).to.deep.equal(userId)
  49. expect(Object.keys(updateArgs[1]).length).to.equal(3)
  50. expect(updateArgs[1]['features.versioning']).to.equal(update.versioning)
  51. expect(updateArgs[1]['features.collaborators']).to.equal(
  52. update.collaborators
  53. )
  54. expect(updateArgs[1].featuresUpdatedAt instanceof Date).to.be.true
  55. features.should.deep.equal(update)
  56. expect(updateArgs[1].featuresEpoch).to.be.undefined
  57. })
  58. it('should set the featuresEpoch when present', async function () {
  59. const userId = '5208dd34438842e2db000005'
  60. const update = {
  61. versioning: true,
  62. }
  63. this.Settings.featuresEpoch = 'epoch-1'
  64. const { features } =
  65. await this.UserFeaturesUpdater.promises.updateFeatures(userId, update)
  66. const updateArgs = this.User.findByIdAndUpdate.lastCall.args
  67. expect(updateArgs[0]).to.deep.equal(userId)
  68. expect(Object.keys(updateArgs[1]).length).to.equal(3)
  69. expect(updateArgs[1]['features.versioning']).to.equal(update.versioning)
  70. expect(updateArgs[1].featuresUpdatedAt instanceof Date).to.be.true
  71. features.should.deep.equal(update)
  72. expect(updateArgs[1].featuresEpoch).to.equal('epoch-1')
  73. })
  74. })
  75. describe('overrideFeatures', function () {
  76. it('should send the users features', async function () {
  77. const userId = '5208dd34438842e2db000005'
  78. const update = Object.assign({}, { mendeley: !this.features.mendeley })
  79. const featuresChanged =
  80. await this.UserFeaturesUpdater.promises.overrideFeatures(userId, update)
  81. const updateArgs = this.User.findByIdAndUpdate.lastCall.args
  82. expect(updateArgs[0]).to.equal(userId)
  83. expect(Object.keys(updateArgs[1]).length).to.equal(2)
  84. expect(updateArgs[1].features).to.deep.equal(update)
  85. expect(updateArgs[1].featuresUpdatedAt instanceof Date).to.be.true
  86. featuresChanged.should.equal(true)
  87. })
  88. })
  89. })