FeaturesUpdaterTests.js 11 KB

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