FeaturesUpdaterTests.js 12 KB

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