FeatureUpdaterTests.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. return Subscription.create({
  91. admin_id: ObjectId(),
  92. member_ids: [this.user._id],
  93. groupAccount: true,
  94. planCode: 'collaborator',
  95. customAccount: true
  96. })
  97. }) // returns a promise
  98. it('should set their features to the upgraded set', function(done) {
  99. return syncUserAndGetFeatures(this.user, (error, features) => {
  100. if (error != null) {
  101. throw error
  102. }
  103. const plan = settings.plans.find(
  104. plan => plan.planCode === 'collaborator'
  105. )
  106. expect(features).to.deep.equal(plan.features)
  107. return done()
  108. })
  109. })
  110. })
  111. describe('when the user has bonus features', function() {
  112. beforeEach(function() {
  113. return User.update(
  114. {
  115. _id: this.user._id
  116. },
  117. {
  118. refered_user_count: 10
  119. }
  120. )
  121. }) // returns a promise
  122. it('should set their features to the bonus set', function(done) {
  123. return syncUserAndGetFeatures(this.user, (error, features) => {
  124. if (error != null) {
  125. throw error
  126. }
  127. expect(features).to.deep.equal(
  128. Object.assign(
  129. {},
  130. settings.defaultFeatures,
  131. settings.bonus_features[9]
  132. )
  133. )
  134. return done()
  135. })
  136. })
  137. })
  138. describe('when the user has affiliations', function() {
  139. beforeEach(function() {
  140. this.institutionPlan = settings.plans.find(
  141. plan => plan.planCode === settings.institutionPlanCode
  142. )
  143. this.email = this.user.emails[0].email
  144. return (this.affiliationData = {
  145. email: this.email,
  146. institution: { licence: 'pro_plus', confirmed: true }
  147. })
  148. })
  149. it('should not set their features if email is not confirmed', function(done) {
  150. MockV1Api.setAffiliations([this.affiliationData])
  151. return syncUserAndGetFeatures(this.user, (error, features) => {
  152. expect(features).to.deep.equal(settings.defaultFeatures)
  153. return done()
  154. })
  155. })
  156. it('should set their features if email is confirmed', function(done) {
  157. MockV1Api.setAffiliations([this.affiliationData])
  158. return this.user.confirmEmail(this.email, error => {
  159. return syncUserAndGetFeatures(this.user, (error, features) => {
  160. expect(features).to.deep.equal(this.institutionPlan.features)
  161. return done()
  162. })
  163. })
  164. })
  165. it('should not set their features if institution is not confirmed', function(done) {
  166. this.affiliationData.institution.confirmed = false
  167. MockV1Api.setAffiliations([this.affiliationData])
  168. return this.user.confirmEmail(this.email, error => {
  169. return syncUserAndGetFeatures(this.user, (error, features) => {
  170. expect(features).to.deep.equal(settings.defaultFeatures)
  171. return done()
  172. })
  173. })
  174. })
  175. })
  176. describe('when the user is due bonus features and has extra features that no longer apply', function() {
  177. beforeEach(function() {
  178. return User.update(
  179. {
  180. _id: this.user._id
  181. },
  182. {
  183. refered_user_count: 10,
  184. 'features.github': true
  185. }
  186. )
  187. }) // returns a promise
  188. it('should set their features to the bonus set and downgrade the extras', function(done) {
  189. return syncUserAndGetFeatures(this.user, (error, features) => {
  190. if (error != null) {
  191. throw error
  192. }
  193. expect(features).to.deep.equal(
  194. Object.assign(
  195. {},
  196. settings.defaultFeatures,
  197. settings.bonus_features[9]
  198. )
  199. )
  200. return done()
  201. })
  202. })
  203. })
  204. describe('when the user has a v1 plan', function() {
  205. beforeEach(function() {
  206. MockV1Api.setUser(42, { plan_name: 'free' })
  207. return User.update(
  208. {
  209. _id: this.user._id
  210. },
  211. {
  212. overleaf: {
  213. id: 42
  214. }
  215. }
  216. )
  217. }) // returns a promise
  218. it('should set their features to the v1 plan', function(done) {
  219. return syncUserAndGetFeatures(this.user, (error, features) => {
  220. if (error != null) {
  221. throw error
  222. }
  223. const plan = settings.plans.find(plan => plan.planCode === 'v1_free')
  224. expect(features).to.deep.equal(plan.features)
  225. return done()
  226. })
  227. })
  228. })
  229. describe('when the user has a v1 plan and bonus features', function() {
  230. beforeEach(function() {
  231. MockV1Api.setUser(42, { plan_name: 'free' })
  232. return User.update(
  233. {
  234. _id: this.user._id
  235. },
  236. {
  237. overleaf: {
  238. id: 42
  239. },
  240. refered_user_count: 10
  241. }
  242. )
  243. }) // returns a promise
  244. it('should set their features to the best of the v1 plan and bonus features', function(done) {
  245. return syncUserAndGetFeatures(this.user, (error, features) => {
  246. if (error != null) {
  247. throw error
  248. }
  249. const v1plan = settings.plans.find(plan => plan.planCode === 'v1_free')
  250. const expectedFeatures = Object.assign(
  251. {},
  252. v1plan.features,
  253. settings.bonus_features[9]
  254. )
  255. expect(features).to.deep.equal(expectedFeatures)
  256. return done()
  257. })
  258. })
  259. })
  260. describe('when the user has a group and personal subscription', function() {
  261. beforeEach(function(done) {
  262. Subscription.create(
  263. {
  264. admin_id: this.user._id,
  265. manager_ids: [this.user._id],
  266. planCode: 'professional',
  267. customAccount: true
  268. },
  269. error => {
  270. if (error != null) {
  271. throw error
  272. }
  273. return Subscription.create(
  274. {
  275. admin_id: ObjectId(),
  276. member_ids: [this.user._id],
  277. groupAccount: true,
  278. planCode: 'collaborator',
  279. customAccount: true
  280. },
  281. done
  282. )
  283. }
  284. )
  285. })
  286. it('should set their features to the best set', function(done) {
  287. return syncUserAndGetFeatures(this.user, (error, features) => {
  288. if (error != null) {
  289. throw error
  290. }
  291. const plan = settings.plans.find(
  292. plan => plan.planCode === 'professional'
  293. )
  294. expect(features).to.deep.equal(plan.features)
  295. return done()
  296. })
  297. })
  298. })
  299. describe('when the notifyV1Flag is passed', function() {
  300. beforeEach(function() {
  301. return User.update(
  302. {
  303. _id: this.user._id
  304. },
  305. {
  306. overleaf: {
  307. id: 42
  308. }
  309. }
  310. )
  311. }) // returns a promise
  312. })
  313. })