FeaturesUpdaterTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const sinon = require('sinon')
  4. const modulePath = '../../../../app/src/Features/Subscription/FeaturesUpdater'
  5. const { ObjectId } = require('mongodb')
  6. describe('FeaturesUpdater', function () {
  7. beforeEach(function () {
  8. this.user_id = ObjectId().toString()
  9. this.FeaturesUpdater = SandboxedModule.require(modulePath, {
  10. requires: {
  11. './UserFeaturesUpdater': (this.UserFeaturesUpdater = {}),
  12. './SubscriptionLocator': (this.SubscriptionLocator = {}),
  13. './PlansLocator': (this.PlansLocator = {}),
  14. 'settings-sharelatex': (this.Settings = {}),
  15. '../Referal/ReferalFeatures': (this.ReferalFeatures = {}),
  16. './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
  17. '../Institutions/InstitutionsFeatures': (this.InstitutionsFeatures = {}),
  18. '../User/UserGetter': (this.UserGetter = {}),
  19. '../../infrastructure/Modules': (this.Modules = {
  20. hooks: { fire: sinon.stub() },
  21. }),
  22. },
  23. })
  24. })
  25. describe('refreshFeatures', function () {
  26. beforeEach(function () {
  27. this.user = {
  28. _id: this.user_id,
  29. features: {},
  30. }
  31. this.UserFeaturesUpdater.updateFeatures = sinon
  32. .stub()
  33. .yields(null, { some: 'features' }, true)
  34. this.FeaturesUpdater._getIndividualFeatures = sinon
  35. .stub()
  36. .yields(null, { individual: 'features' })
  37. this.FeaturesUpdater._getGroupFeatureSets = sinon
  38. .stub()
  39. .yields(null, [{ group: 'features' }, { group: 'features2' }])
  40. this.InstitutionsFeatures.getInstitutionsFeatures = sinon
  41. .stub()
  42. .yields(null, { institutions: 'features' })
  43. this.FeaturesUpdater._getV1Features = sinon
  44. .stub()
  45. .yields(null, { v1: 'features' })
  46. this.ReferalFeatures.getBonusFeatures = sinon
  47. .stub()
  48. .yields(null, { bonus: 'features' })
  49. this.FeaturesUpdater._mergeFeatures = sinon
  50. .stub()
  51. .returns({ merged: 'features' })
  52. this.UserGetter.getUser = sinon.stub().yields(null, this.user)
  53. this.callback = sinon.stub()
  54. })
  55. it('should return features and featuresChanged', function () {
  56. this.FeaturesUpdater.refreshFeatures(
  57. this.user_id,
  58. 'test',
  59. (err, features, featuresChanged) => {
  60. expect(err).to.not.exist
  61. expect(features).to.exist
  62. expect(featuresChanged).to.exist
  63. }
  64. )
  65. })
  66. describe('normally', function () {
  67. beforeEach(function () {
  68. this.FeaturesUpdater.refreshFeatures(
  69. this.user_id,
  70. 'test',
  71. this.callback
  72. )
  73. })
  74. it('should get the individual features', function () {
  75. this.FeaturesUpdater._getIndividualFeatures
  76. .calledWith(this.user_id)
  77. .should.equal(true)
  78. })
  79. it('should get the group features', function () {
  80. this.FeaturesUpdater._getGroupFeatureSets
  81. .calledWith(this.user_id)
  82. .should.equal(true)
  83. })
  84. it('should get the institution features', function () {
  85. this.InstitutionsFeatures.getInstitutionsFeatures
  86. .calledWith(this.user_id)
  87. .should.equal(true)
  88. })
  89. it('should get the v1 features', function () {
  90. this.FeaturesUpdater._getV1Features
  91. .calledWith(this.user_id)
  92. .should.equal(true)
  93. })
  94. it('should get the bonus features', function () {
  95. this.ReferalFeatures.getBonusFeatures
  96. .calledWith(this.user_id)
  97. .should.equal(true)
  98. })
  99. it('should merge from the default features', function () {
  100. this.FeaturesUpdater._mergeFeatures
  101. .calledWith(this.Settings.defaultFeatures)
  102. .should.equal(true)
  103. })
  104. it('should merge the individual features', function () {
  105. this.FeaturesUpdater._mergeFeatures
  106. .calledWith(sinon.match.any, { individual: 'features' })
  107. .should.equal(true)
  108. })
  109. it('should merge the group features', function () {
  110. this.FeaturesUpdater._mergeFeatures
  111. .calledWith(sinon.match.any, { group: 'features' })
  112. .should.equal(true)
  113. this.FeaturesUpdater._mergeFeatures
  114. .calledWith(sinon.match.any, { group: 'features2' })
  115. .should.equal(true)
  116. })
  117. it('should merge the institutions features', function () {
  118. this.FeaturesUpdater._mergeFeatures
  119. .calledWith(sinon.match.any, { institutions: 'features' })
  120. .should.equal(true)
  121. })
  122. it('should merge the v1 features', function () {
  123. this.FeaturesUpdater._mergeFeatures
  124. .calledWith(sinon.match.any, { v1: 'features' })
  125. .should.equal(true)
  126. })
  127. it('should merge the bonus features', function () {
  128. this.FeaturesUpdater._mergeFeatures
  129. .calledWith(sinon.match.any, { bonus: 'features' })
  130. .should.equal(true)
  131. })
  132. it('should update the user with the merged features', function () {
  133. this.UserFeaturesUpdater.updateFeatures
  134. .calledWith(this.user_id, { merged: 'features' })
  135. .should.equal(true)
  136. })
  137. })
  138. describe('when losing dropbox feature', function () {
  139. beforeEach(function () {
  140. this.user = {
  141. _id: this.user_id,
  142. features: { dropbox: true },
  143. }
  144. this.UserGetter.getUser = sinon.stub().yields(null, this.user)
  145. this.FeaturesUpdater._mergeFeatures = sinon
  146. .stub()
  147. .returns({ dropbox: false })
  148. this.FeaturesUpdater.refreshFeatures(
  149. this.user_id,
  150. 'test',
  151. this.callback
  152. )
  153. })
  154. it('should fire module hook to unlink dropbox', function () {
  155. this.Modules.hooks.fire
  156. .calledWith('removeDropbox', this.user._id, 'test')
  157. .should.equal(true)
  158. })
  159. })
  160. })
  161. describe('_mergeFeatures', function () {
  162. it('should prefer priority over standard for compileGroup', function () {
  163. expect(
  164. this.FeaturesUpdater._mergeFeatures(
  165. {
  166. compileGroup: 'priority',
  167. },
  168. {
  169. compileGroup: 'standard',
  170. }
  171. )
  172. ).to.deep.equal({
  173. compileGroup: 'priority',
  174. })
  175. expect(
  176. this.FeaturesUpdater._mergeFeatures(
  177. {
  178. compileGroup: 'standard',
  179. },
  180. {
  181. compileGroup: 'priority',
  182. }
  183. )
  184. ).to.deep.equal({
  185. compileGroup: 'priority',
  186. })
  187. expect(
  188. this.FeaturesUpdater._mergeFeatures(
  189. {
  190. compileGroup: 'priority',
  191. },
  192. {
  193. compileGroup: 'priority',
  194. }
  195. )
  196. ).to.deep.equal({
  197. compileGroup: 'priority',
  198. })
  199. expect(
  200. this.FeaturesUpdater._mergeFeatures(
  201. {
  202. compileGroup: 'standard',
  203. },
  204. {
  205. compileGroup: 'standard',
  206. }
  207. )
  208. ).to.deep.equal({
  209. compileGroup: 'standard',
  210. })
  211. })
  212. it('should prefer -1 over any other for collaborators', function () {
  213. expect(
  214. this.FeaturesUpdater._mergeFeatures(
  215. {
  216. collaborators: -1,
  217. },
  218. {
  219. collaborators: 10,
  220. }
  221. )
  222. ).to.deep.equal({
  223. collaborators: -1,
  224. })
  225. expect(
  226. this.FeaturesUpdater._mergeFeatures(
  227. {
  228. collaborators: 10,
  229. },
  230. {
  231. collaborators: -1,
  232. }
  233. )
  234. ).to.deep.equal({
  235. collaborators: -1,
  236. })
  237. expect(
  238. this.FeaturesUpdater._mergeFeatures(
  239. {
  240. collaborators: 4,
  241. },
  242. {
  243. collaborators: 10,
  244. }
  245. )
  246. ).to.deep.equal({
  247. collaborators: 10,
  248. })
  249. })
  250. it('should prefer the higher of compileTimeout', function () {
  251. expect(
  252. this.FeaturesUpdater._mergeFeatures(
  253. {
  254. compileTimeout: 20,
  255. },
  256. {
  257. compileTimeout: 10,
  258. }
  259. )
  260. ).to.deep.equal({
  261. compileTimeout: 20,
  262. })
  263. expect(
  264. this.FeaturesUpdater._mergeFeatures(
  265. {
  266. compileTimeout: 10,
  267. },
  268. {
  269. compileTimeout: 20,
  270. }
  271. )
  272. ).to.deep.equal({
  273. compileTimeout: 20,
  274. })
  275. })
  276. it('should prefer the true over false for other keys', function () {
  277. expect(
  278. this.FeaturesUpdater._mergeFeatures(
  279. {
  280. github: true,
  281. },
  282. {
  283. github: false,
  284. }
  285. )
  286. ).to.deep.equal({
  287. github: true,
  288. })
  289. expect(
  290. this.FeaturesUpdater._mergeFeatures(
  291. {
  292. github: false,
  293. },
  294. {
  295. github: true,
  296. }
  297. )
  298. ).to.deep.equal({
  299. github: true,
  300. })
  301. expect(
  302. this.FeaturesUpdater._mergeFeatures(
  303. {
  304. github: true,
  305. },
  306. {
  307. github: true,
  308. }
  309. )
  310. ).to.deep.equal({
  311. github: true,
  312. })
  313. expect(
  314. this.FeaturesUpdater._mergeFeatures(
  315. {
  316. github: false,
  317. },
  318. {
  319. github: false,
  320. }
  321. )
  322. ).to.deep.equal({
  323. github: false,
  324. })
  325. })
  326. })
  327. describe('doSyncFromV1', function () {
  328. beforeEach(function () {
  329. this.v1UserId = 1
  330. this.user = {
  331. _id: this.user_id,
  332. email: 'user@example.com',
  333. overleaf: {
  334. id: this.v1UserId,
  335. },
  336. }
  337. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  338. this.FeaturesUpdater.refreshFeatures = sinon.stub().yields(null)
  339. this.call = cb => {
  340. this.FeaturesUpdater.doSyncFromV1(this.v1UserId, cb)
  341. }
  342. })
  343. describe('when all goes well', function () {
  344. it('should call getUser', function (done) {
  345. this.call(() => {
  346. expect(this.UserGetter.getUser.callCount).to.equal(1)
  347. expect(
  348. this.UserGetter.getUser.calledWith({ 'overleaf.id': this.v1UserId })
  349. ).to.equal(true)
  350. done()
  351. })
  352. })
  353. it('should call refreshFeatures', function (done) {
  354. this.call(() => {
  355. expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(1)
  356. expect(
  357. this.FeaturesUpdater.refreshFeatures.calledWith(this.user_id)
  358. ).to.equal(true)
  359. done()
  360. })
  361. })
  362. it('should not produce an error', function (done) {
  363. this.call(err => {
  364. expect(err).to.not.exist
  365. done()
  366. })
  367. })
  368. })
  369. describe('when getUser produces an error', function () {
  370. beforeEach(function () {
  371. this.UserGetter.getUser = sinon
  372. .stub()
  373. .callsArgWith(2, new Error('woops'))
  374. })
  375. it('should not call refreshFeatures', function () {
  376. expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(0)
  377. })
  378. it('should produce an error', function (done) {
  379. this.call(err => {
  380. expect(err).to.exist
  381. done()
  382. })
  383. })
  384. })
  385. describe('when getUser does not find a user', function () {
  386. beforeEach(function () {
  387. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  388. })
  389. it('should not call refreshFeatures', function (done) {
  390. this.call(() => {
  391. expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(0)
  392. done()
  393. })
  394. })
  395. it('should not produce an error', function (done) {
  396. this.call(err => {
  397. expect(err).to.not.exist
  398. done()
  399. })
  400. })
  401. })
  402. })
  403. })