FeatureUpdaterTests.js 9.4 KB

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