FeatureUpdaterTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const { expect } = require('chai')
  16. const async = require('async')
  17. const UserClient = require('./helpers/User')
  18. const request = require('./helpers/request')
  19. const settings = require('settings-sharelatex')
  20. const { ObjectId } = require('../../../app/src/infrastructure/mongojs')
  21. const { Subscription } = require('../../../app/src/models/Subscription')
  22. const { User } = require('../../../app/src/models/User')
  23. const FeaturesUpdater = require('../../../app/src/Features/Subscription/FeaturesUpdater')
  24. const MockV1Api = require('./helpers/MockV1Api')
  25. const logger = require('logger-sharelatex')
  26. logger.logger.level('error')
  27. const syncUserAndGetFeatures = function(user, callback) {
  28. if (callback == null) {
  29. callback = function(error, features) {}
  30. }
  31. return FeaturesUpdater.refreshFeatures(user._id, error => {
  32. if (error != null) {
  33. return callback(error)
  34. }
  35. return User.findById(user._id, (error, user) => {
  36. if (error != null) {
  37. return callback(error)
  38. }
  39. const { features } = user.toObject()
  40. delete features.$init // mongoose internals
  41. return callback(null, features)
  42. })
  43. })
  44. }
  45. describe('FeatureUpdater.refreshFeatures', function() {
  46. beforeEach(function(done) {
  47. this.user = new UserClient()
  48. return this.user.ensureUserExists(error => {
  49. if (error != null) {
  50. throw error
  51. }
  52. return done()
  53. })
  54. })
  55. describe('when user has no subscriptions', function() {
  56. it('should set their features to the basic set', function(done) {
  57. return syncUserAndGetFeatures(this.user, (error, features) => {
  58. if (error != null) {
  59. throw error
  60. }
  61. expect(features).to.deep.equal(settings.defaultFeatures)
  62. return done()
  63. })
  64. })
  65. })
  66. describe('when the user has an individual subscription', function() {
  67. beforeEach(function() {
  68. return Subscription.create({
  69. admin_id: this.user._id,
  70. manager_ids: [this.user._id],
  71. planCode: 'collaborator',
  72. customAccount: true
  73. })
  74. }) // returns a promise
  75. it('should set their features to the upgraded set', function(done) {
  76. return syncUserAndGetFeatures(this.user, (error, features) => {
  77. if (error != null) {
  78. throw error
  79. }
  80. const plan = settings.plans.find(
  81. plan => plan.planCode === 'collaborator'
  82. )
  83. expect(features).to.deep.equal(plan.features)
  84. return done()
  85. })
  86. })
  87. })
  88. describe('when the user is in a group subscription', function() {
  89. beforeEach(function() {
  90. const groupAdminId = ObjectId()
  91. return Subscription.create({
  92. admin_id: groupAdminId,
  93. manager_ids: [groupAdminId],
  94. member_ids: [this.user._id],
  95. groupAccount: true,
  96. planCode: 'collaborator',
  97. customAccount: true
  98. })
  99. }) // returns a promise
  100. it('should set their features to the upgraded set', function(done) {
  101. return syncUserAndGetFeatures(this.user, (error, features) => {
  102. if (error != null) {
  103. throw error
  104. }
  105. const plan = settings.plans.find(
  106. plan => plan.planCode === 'collaborator'
  107. )
  108. expect(features).to.deep.equal(plan.features)
  109. return done()
  110. })
  111. })
  112. })
  113. describe('when the user has bonus features', function() {
  114. beforeEach(function() {
  115. return User.update(
  116. {
  117. _id: this.user._id
  118. },
  119. {
  120. refered_user_count: 10
  121. }
  122. )
  123. }) // returns a promise
  124. it('should set their features to the bonus set', function(done) {
  125. return syncUserAndGetFeatures(this.user, (error, features) => {
  126. if (error != null) {
  127. throw error
  128. }
  129. expect(features).to.deep.equal(
  130. Object.assign(
  131. {},
  132. settings.defaultFeatures,
  133. settings.bonus_features[9]
  134. )
  135. )
  136. return done()
  137. })
  138. })
  139. })
  140. describe('when the user has affiliations', function() {
  141. beforeEach(function() {
  142. this.institutionPlan = settings.plans.find(
  143. plan => plan.planCode === settings.institutionPlanCode
  144. )
  145. this.email = this.user.emails[0].email
  146. return (this.affiliationData = {
  147. email: this.email,
  148. institution: { licence: 'pro_plus', confirmed: true }
  149. })
  150. })
  151. it('should not set their features if email is not confirmed', function(done) {
  152. MockV1Api.setAffiliations([this.affiliationData])
  153. return syncUserAndGetFeatures(this.user, (error, features) => {
  154. expect(features).to.deep.equal(settings.defaultFeatures)
  155. return done()
  156. })
  157. })
  158. it('should set their features if email is confirmed', function(done) {
  159. MockV1Api.setAffiliations([this.affiliationData])
  160. return this.user.confirmEmail(this.email, error => {
  161. return syncUserAndGetFeatures(this.user, (error, features) => {
  162. expect(features).to.deep.equal(this.institutionPlan.features)
  163. return done()
  164. })
  165. })
  166. })
  167. it('should not set their features if institution is not confirmed', function(done) {
  168. this.affiliationData.institution.confirmed = false
  169. MockV1Api.setAffiliations([this.affiliationData])
  170. return this.user.confirmEmail(this.email, error => {
  171. return syncUserAndGetFeatures(this.user, (error, features) => {
  172. expect(features).to.deep.equal(settings.defaultFeatures)
  173. return done()
  174. })
  175. })
  176. })
  177. })
  178. describe('when the user is due bonus features and has extra features that no longer apply', function() {
  179. beforeEach(function() {
  180. return User.update(
  181. {
  182. _id: this.user._id
  183. },
  184. {
  185. refered_user_count: 10,
  186. 'features.github': true
  187. }
  188. )
  189. }) // returns a promise
  190. it('should set their features to the bonus set and downgrade the extras', function(done) {
  191. return syncUserAndGetFeatures(this.user, (error, features) => {
  192. if (error != null) {
  193. throw error
  194. }
  195. expect(features).to.deep.equal(
  196. Object.assign(
  197. {},
  198. settings.defaultFeatures,
  199. settings.bonus_features[9]
  200. )
  201. )
  202. return done()
  203. })
  204. })
  205. })
  206. describe('when the user has a v1 plan', function() {
  207. beforeEach(function() {
  208. MockV1Api.setUser(42, { plan_name: 'free' })
  209. return User.update(
  210. {
  211. _id: this.user._id
  212. },
  213. {
  214. overleaf: {
  215. id: 42
  216. }
  217. }
  218. )
  219. }) // returns a promise
  220. it('should set their features to the v1 plan', function(done) {
  221. return syncUserAndGetFeatures(this.user, (error, features) => {
  222. if (error != null) {
  223. throw error
  224. }
  225. const plan = settings.plans.find(plan => plan.planCode === 'v1_free')
  226. expect(features).to.deep.equal(plan.features)
  227. return done()
  228. })
  229. })
  230. })
  231. describe('when the user has a v1 plan and bonus features', function() {
  232. beforeEach(function() {
  233. MockV1Api.setUser(42, { plan_name: 'free' })
  234. return User.update(
  235. {
  236. _id: this.user._id
  237. },
  238. {
  239. overleaf: {
  240. id: 42
  241. },
  242. refered_user_count: 10
  243. }
  244. )
  245. }) // returns a promise
  246. it('should set their features to the best of the v1 plan and bonus features', function(done) {
  247. return syncUserAndGetFeatures(this.user, (error, features) => {
  248. if (error != null) {
  249. throw error
  250. }
  251. const v1plan = settings.plans.find(plan => plan.planCode === 'v1_free')
  252. const expectedFeatures = Object.assign(
  253. {},
  254. v1plan.features,
  255. settings.bonus_features[9]
  256. )
  257. expect(features).to.deep.equal(expectedFeatures)
  258. return done()
  259. })
  260. })
  261. })
  262. describe('when the user has a group and personal subscription', function() {
  263. beforeEach(function(done) {
  264. const groupAdminId = ObjectId()
  265. Subscription.create(
  266. {
  267. admin_id: this.user._id,
  268. manager_ids: [this.user._id],
  269. planCode: 'professional',
  270. customAccount: true
  271. },
  272. error => {
  273. if (error != null) {
  274. throw error
  275. }
  276. return Subscription.create(
  277. {
  278. admin_id: groupAdminId,
  279. manager_ids: [groupAdminId],
  280. member_ids: [this.user._id],
  281. groupAccount: true,
  282. planCode: 'collaborator',
  283. customAccount: true
  284. },
  285. done
  286. )
  287. }
  288. )
  289. })
  290. it('should set their features to the best set', function(done) {
  291. return syncUserAndGetFeatures(this.user, (error, features) => {
  292. if (error != null) {
  293. throw error
  294. }
  295. const plan = settings.plans.find(
  296. plan => plan.planCode === 'professional'
  297. )
  298. expect(features).to.deep.equal(plan.features)
  299. return done()
  300. })
  301. })
  302. })
  303. describe('when the notifyV1Flag is passed', function() {
  304. beforeEach(function() {
  305. return User.update(
  306. {
  307. _id: this.user._id
  308. },
  309. {
  310. overleaf: {
  311. id: 42
  312. }
  313. }
  314. )
  315. }) // returns a promise
  316. })
  317. describe('when the user has features overrides', function() {
  318. beforeEach(function() {
  319. const futureDate = new Date()
  320. futureDate.setDate(futureDate.getDate() + 1)
  321. return User.update(
  322. {
  323. _id: this.user._id
  324. },
  325. {
  326. featuresOverrides: [
  327. {
  328. features: {
  329. github: true
  330. }
  331. },
  332. {
  333. features: {
  334. dropbox: true
  335. },
  336. expiresAt: new Date(1990, 12, 25)
  337. },
  338. {
  339. features: {
  340. trackChanges: true
  341. },
  342. expiresAt: futureDate
  343. }
  344. ]
  345. }
  346. )
  347. }) // returns a promise
  348. it('should set their features to the overridden set', function(done) {
  349. return syncUserAndGetFeatures(this.user, (error, features) => {
  350. if (error != null) {
  351. throw error
  352. }
  353. let expectedFeatures = Object.assign(settings.defaultFeatures, {
  354. github: true,
  355. trackChanges: true
  356. })
  357. expect(features).to.deep.equal(expectedFeatures)
  358. return done()
  359. })
  360. })
  361. })
  362. })