FeatureUpdaterTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. licence: 'pro_plus',
  149. institution: { confirmed: true }
  150. })
  151. })
  152. it('should not set their features if email is not confirmed', function(done) {
  153. MockV1Api.setAffiliations([this.affiliationData])
  154. return syncUserAndGetFeatures(this.user, (error, features) => {
  155. expect(features).to.deep.equal(settings.defaultFeatures)
  156. return done()
  157. })
  158. })
  159. it('should set their features if email is confirmed', function(done) {
  160. MockV1Api.setAffiliations([this.affiliationData])
  161. return this.user.confirmEmail(this.email, error => {
  162. return syncUserAndGetFeatures(this.user, (error, features) => {
  163. expect(features).to.deep.equal(this.institutionPlan.features)
  164. return done()
  165. })
  166. })
  167. })
  168. it('should not set their features if institution is not confirmed', function(done) {
  169. this.affiliationData.institution.confirmed = false
  170. MockV1Api.setAffiliations([this.affiliationData])
  171. return this.user.confirmEmail(this.email, error => {
  172. return syncUserAndGetFeatures(this.user, (error, features) => {
  173. expect(features).to.deep.equal(settings.defaultFeatures)
  174. return done()
  175. })
  176. })
  177. })
  178. })
  179. describe('when the user is due bonus features and has extra features that no longer apply', function() {
  180. beforeEach(function() {
  181. return User.update(
  182. {
  183. _id: this.user._id
  184. },
  185. {
  186. refered_user_count: 10,
  187. 'features.github': true
  188. }
  189. )
  190. }) // returns a promise
  191. it('should set their features to the bonus set and downgrade the extras', function(done) {
  192. return syncUserAndGetFeatures(this.user, (error, features) => {
  193. if (error != null) {
  194. throw error
  195. }
  196. expect(features).to.deep.equal(
  197. Object.assign(
  198. {},
  199. settings.defaultFeatures,
  200. settings.bonus_features[9]
  201. )
  202. )
  203. return done()
  204. })
  205. })
  206. })
  207. describe('when the user has a v1 plan', function() {
  208. beforeEach(function() {
  209. MockV1Api.setUser(42, { plan_name: 'free' })
  210. return User.update(
  211. {
  212. _id: this.user._id
  213. },
  214. {
  215. overleaf: {
  216. id: 42
  217. }
  218. }
  219. )
  220. }) // returns a promise
  221. it('should set their features to the v1 plan', function(done) {
  222. return syncUserAndGetFeatures(this.user, (error, features) => {
  223. if (error != null) {
  224. throw error
  225. }
  226. const plan = settings.plans.find(plan => plan.planCode === 'v1_free')
  227. expect(features).to.deep.equal(plan.features)
  228. return done()
  229. })
  230. })
  231. })
  232. describe('when the user has a v1 plan and bonus features', function() {
  233. beforeEach(function() {
  234. MockV1Api.setUser(42, { plan_name: 'free' })
  235. return User.update(
  236. {
  237. _id: this.user._id
  238. },
  239. {
  240. overleaf: {
  241. id: 42
  242. },
  243. refered_user_count: 10
  244. }
  245. )
  246. }) // returns a promise
  247. it('should set their features to the best of the v1 plan and bonus features', function(done) {
  248. return syncUserAndGetFeatures(this.user, (error, features) => {
  249. if (error != null) {
  250. throw error
  251. }
  252. const v1plan = settings.plans.find(plan => plan.planCode === 'v1_free')
  253. const expectedFeatures = Object.assign(
  254. {},
  255. v1plan.features,
  256. settings.bonus_features[9]
  257. )
  258. expect(features).to.deep.equal(expectedFeatures)
  259. return done()
  260. })
  261. })
  262. })
  263. describe('when the user has a group and personal subscription', function() {
  264. beforeEach(function(done) {
  265. const groupAdminId = ObjectId()
  266. Subscription.create(
  267. {
  268. admin_id: this.user._id,
  269. manager_ids: [this.user._id],
  270. planCode: 'professional',
  271. customAccount: true
  272. },
  273. error => {
  274. if (error != null) {
  275. throw error
  276. }
  277. return Subscription.create(
  278. {
  279. admin_id: groupAdminId,
  280. manager_ids: [groupAdminId],
  281. member_ids: [this.user._id],
  282. groupAccount: true,
  283. planCode: 'collaborator',
  284. customAccount: true
  285. },
  286. done
  287. )
  288. }
  289. )
  290. })
  291. it('should set their features to the best set', function(done) {
  292. return syncUserAndGetFeatures(this.user, (error, features) => {
  293. if (error != null) {
  294. throw error
  295. }
  296. const plan = settings.plans.find(
  297. plan => plan.planCode === 'professional'
  298. )
  299. expect(features).to.deep.equal(plan.features)
  300. return done()
  301. })
  302. })
  303. })
  304. describe('when the notifyV1Flag is passed', function() {
  305. beforeEach(function() {
  306. return User.update(
  307. {
  308. _id: this.user._id
  309. },
  310. {
  311. overleaf: {
  312. id: 42
  313. }
  314. }
  315. )
  316. }) // returns a promise
  317. })
  318. describe('when the user has features overrides', function() {
  319. beforeEach(function() {
  320. const futureDate = new Date()
  321. futureDate.setDate(futureDate.getDate() + 1)
  322. return User.update(
  323. {
  324. _id: this.user._id
  325. },
  326. {
  327. featuresOverrides: [
  328. {
  329. features: {
  330. github: true
  331. }
  332. },
  333. {
  334. features: {
  335. dropbox: true
  336. },
  337. expiresAt: new Date(1990, 12, 25)
  338. },
  339. {
  340. features: {
  341. trackChanges: true
  342. },
  343. expiresAt: futureDate
  344. }
  345. ]
  346. }
  347. )
  348. }) // returns a promise
  349. it('should set their features to the overridden set', function(done) {
  350. return syncUserAndGetFeatures(this.user, (error, features) => {
  351. if (error != null) {
  352. throw error
  353. }
  354. let expectedFeatures = Object.assign(settings.defaultFeatures, {
  355. github: true,
  356. trackChanges: true
  357. })
  358. expect(features).to.deep.equal(expectedFeatures)
  359. return done()
  360. })
  361. })
  362. })
  363. })