FeaturesUpdaterTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const should = require('chai').should()
  15. const { expect } = require('chai')
  16. const sinon = require('sinon')
  17. const modulePath = '../../../../app/src/Features/Subscription/FeaturesUpdater'
  18. const { assert } = require('chai')
  19. const { ObjectId } = require('mongodb')
  20. describe('FeaturesUpdater', function() {
  21. beforeEach(function() {
  22. this.user_id = ObjectId().toString()
  23. return (this.FeaturesUpdater = SandboxedModule.require(modulePath, {
  24. globals: {
  25. console: console
  26. },
  27. requires: {
  28. './UserFeaturesUpdater': (this.UserFeaturesUpdater = {}),
  29. './SubscriptionLocator': (this.SubscriptionLocator = {}),
  30. './PlansLocator': (this.PlansLocator = {}),
  31. 'logger-sharelatex': {
  32. log() {},
  33. warn() {}
  34. },
  35. 'settings-sharelatex': (this.Settings = {}),
  36. '../Referal/ReferalFeatures': (this.ReferalFeatures = {}),
  37. './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
  38. '../Institutions/InstitutionsFeatures': (this.InstitutionsFeatures = {}),
  39. '../User/UserGetter': (this.UserGetter = {}),
  40. '../../infrastructure/Modules': (this.Modules = {
  41. hooks: { fire: sinon.stub() }
  42. })
  43. }
  44. }))
  45. })
  46. describe('refreshFeatures', function() {
  47. beforeEach(function() {
  48. this.user = {
  49. _id: this.user_id,
  50. features: {}
  51. }
  52. this.UserFeaturesUpdater.updateFeatures = sinon.stub().yields()
  53. this.FeaturesUpdater._getIndividualFeatures = sinon
  54. .stub()
  55. .yields(null, { individual: 'features' })
  56. this.FeaturesUpdater._getGroupFeatureSets = sinon
  57. .stub()
  58. .yields(null, [{ group: 'features' }, { group: 'features2' }])
  59. this.InstitutionsFeatures.getInstitutionsFeatures = sinon
  60. .stub()
  61. .yields(null, { institutions: 'features' })
  62. this.FeaturesUpdater._getV1Features = sinon
  63. .stub()
  64. .yields(null, { v1: 'features' })
  65. this.ReferalFeatures.getBonusFeatures = sinon
  66. .stub()
  67. .yields(null, { bonus: 'features' })
  68. this.FeaturesUpdater._mergeFeatures = sinon
  69. .stub()
  70. .returns({ merged: 'features' })
  71. this.UserGetter.getUser = sinon.stub().yields(null, this.user)
  72. return (this.callback = sinon.stub())
  73. })
  74. describe('normally', function() {
  75. beforeEach(function() {
  76. return this.FeaturesUpdater.refreshFeatures(this.user_id, this.callback)
  77. })
  78. it('should get the individual features', function() {
  79. return this.FeaturesUpdater._getIndividualFeatures
  80. .calledWith(this.user_id)
  81. .should.equal(true)
  82. })
  83. it('should get the group features', function() {
  84. return this.FeaturesUpdater._getGroupFeatureSets
  85. .calledWith(this.user_id)
  86. .should.equal(true)
  87. })
  88. it('should get the institution features', function() {
  89. return this.InstitutionsFeatures.getInstitutionsFeatures
  90. .calledWith(this.user_id)
  91. .should.equal(true)
  92. })
  93. it('should get the v1 features', function() {
  94. return this.FeaturesUpdater._getV1Features
  95. .calledWith(this.user_id)
  96. .should.equal(true)
  97. })
  98. it('should get the bonus features', function() {
  99. return this.ReferalFeatures.getBonusFeatures
  100. .calledWith(this.user_id)
  101. .should.equal(true)
  102. })
  103. it('should merge from the default features', function() {
  104. return this.FeaturesUpdater._mergeFeatures
  105. .calledWith(this.Settings.defaultFeatures)
  106. .should.equal(true)
  107. })
  108. it('should merge the individual features', function() {
  109. return this.FeaturesUpdater._mergeFeatures
  110. .calledWith(sinon.match.any, { individual: 'features' })
  111. .should.equal(true)
  112. })
  113. it('should merge the group features', function() {
  114. this.FeaturesUpdater._mergeFeatures
  115. .calledWith(sinon.match.any, { group: 'features' })
  116. .should.equal(true)
  117. return this.FeaturesUpdater._mergeFeatures
  118. .calledWith(sinon.match.any, { group: 'features2' })
  119. .should.equal(true)
  120. })
  121. it('should merge the institutions features', function() {
  122. return this.FeaturesUpdater._mergeFeatures
  123. .calledWith(sinon.match.any, { institutions: 'features' })
  124. .should.equal(true)
  125. })
  126. it('should merge the v1 features', function() {
  127. return this.FeaturesUpdater._mergeFeatures
  128. .calledWith(sinon.match.any, { v1: 'features' })
  129. .should.equal(true)
  130. })
  131. it('should merge the bonus features', function() {
  132. return this.FeaturesUpdater._mergeFeatures
  133. .calledWith(sinon.match.any, { bonus: 'features' })
  134. .should.equal(true)
  135. })
  136. it('should update the user with the merged features', function() {
  137. return this.UserFeaturesUpdater.updateFeatures
  138. .calledWith(this.user_id, { merged: 'features' })
  139. .should.equal(true)
  140. })
  141. })
  142. describe('when losing dropbox feature', function() {
  143. beforeEach(function() {
  144. this.user = {
  145. _id: this.user_id,
  146. features: { dropbox: true }
  147. }
  148. this.UserGetter.getUser = sinon.stub().yields(null, this.user)
  149. this.FeaturesUpdater._mergeFeatures = sinon
  150. .stub()
  151. .returns({ dropbox: false })
  152. return this.FeaturesUpdater.refreshFeatures(this.user_id, this.callback)
  153. })
  154. it('should fire module hook to unlink dropbox', function() {
  155. this.Modules.hooks.fire
  156. .calledWith('removeDropbox', this.user._id)
  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. return 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. return 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. return 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. return 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. return (this.call = cb => {
  340. return this.FeaturesUpdater.doSyncFromV1(this.v1UserId, cb)
  341. })
  342. })
  343. describe('when all goes well', function() {
  344. it('should call getUser', function(done) {
  345. return 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. return done()
  351. })
  352. })
  353. it('should call refreshFeatures', function(done) {
  354. return 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. return done()
  360. })
  361. })
  362. it('should not produce an error', function(done) {
  363. return this.call(err => {
  364. expect(err).to.not.exist
  365. return done()
  366. })
  367. })
  368. })
  369. describe('when getUser produces an error', function() {
  370. beforeEach(function() {
  371. return (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. return this.call(err => {
  380. expect(err).to.exist
  381. return done()
  382. })
  383. })
  384. })
  385. describe('when getUser does not find a user', function() {
  386. beforeEach(function() {
  387. return (this.UserGetter.getUser = sinon
  388. .stub()
  389. .callsArgWith(2, null, null))
  390. })
  391. it('should not call refreshFeatures', function(done) {
  392. return this.call(() => {
  393. expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(0)
  394. return done()
  395. })
  396. })
  397. it('should not produce an error', function(done) {
  398. return this.call(err => {
  399. expect(err).to.not.exist
  400. return done()
  401. })
  402. })
  403. })
  404. })
  405. })