LimitationsManagerTests.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath = require('path').join(
  4. __dirname,
  5. '../../../../app/src/Features/Subscription/LimitationsManager'
  6. )
  7. describe('LimitationsManager', function () {
  8. beforeEach(function () {
  9. this.project = { _id: (this.projectId = 'project-id') }
  10. this.user = { _id: (this.userId = 'user-id'), features: {} }
  11. this.ProjectGetter = {
  12. getProject: (projectId, fields, callback) => {
  13. if (projectId === this.projectId) {
  14. callback(null, this.project)
  15. } else {
  16. callback(null, null)
  17. }
  18. },
  19. }
  20. this.UserGetter = {
  21. getUser: (userId, filter, callback) => {
  22. if (userId === this.userId) {
  23. callback(null, this.user)
  24. } else {
  25. callback(null, null)
  26. }
  27. },
  28. }
  29. this.SubscriptionLocator = {
  30. getUsersSubscription: sinon.stub(),
  31. getSubscription: sinon.stub(),
  32. }
  33. this.LimitationsManager = SandboxedModule.require(modulePath, {
  34. requires: {
  35. '../Project/ProjectGetter': this.ProjectGetter,
  36. '../User/UserGetter': this.UserGetter,
  37. './SubscriptionLocator': this.SubscriptionLocator,
  38. 'settings-sharelatex': (this.Settings = {}),
  39. '../Collaborators/CollaboratorsGetter': (this.CollaboratorsGetter = {}),
  40. '../Collaborators/CollaboratorsInviteHandler': (this.CollaboratorsInviteHandler = {}),
  41. './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
  42. },
  43. })
  44. })
  45. describe('allowedNumberOfCollaboratorsInProject', function () {
  46. describe('when the project is owned by a user without a subscription', function () {
  47. beforeEach(function () {
  48. this.Settings.defaultFeatures = { collaborators: 23 }
  49. this.project.owner_ref = this.userId
  50. delete this.user.features
  51. this.callback = sinon.stub()
  52. this.LimitationsManager.allowedNumberOfCollaboratorsInProject(
  53. this.projectId,
  54. this.callback
  55. )
  56. })
  57. it('should return the default number', function () {
  58. this.callback
  59. .calledWith(null, this.Settings.defaultFeatures.collaborators)
  60. .should.equal(true)
  61. })
  62. })
  63. describe('when the project is owned by a user with a subscription', function () {
  64. beforeEach(function () {
  65. this.project.owner_ref = this.userId
  66. this.user.features = { collaborators: 21 }
  67. this.callback = sinon.stub()
  68. this.LimitationsManager.allowedNumberOfCollaboratorsInProject(
  69. this.projectId,
  70. this.callback
  71. )
  72. })
  73. it('should return the number of collaborators the user is allowed', function () {
  74. this.callback
  75. .calledWith(null, this.user.features.collaborators)
  76. .should.equal(true)
  77. })
  78. })
  79. })
  80. describe('allowedNumberOfCollaboratorsForUser', function () {
  81. describe('when the user has no features', function () {
  82. beforeEach(function () {
  83. this.Settings.defaultFeatures = { collaborators: 23 }
  84. delete this.user.features
  85. this.callback = sinon.stub()
  86. this.LimitationsManager.allowedNumberOfCollaboratorsForUser(
  87. this.userId,
  88. this.callback
  89. )
  90. })
  91. it('should return the default number', function () {
  92. this.callback
  93. .calledWith(null, this.Settings.defaultFeatures.collaborators)
  94. .should.equal(true)
  95. })
  96. })
  97. describe('when the user has features', function () {
  98. beforeEach(function () {
  99. this.user.features = { collaborators: 21 }
  100. this.callback = sinon.stub()
  101. this.LimitationsManager.allowedNumberOfCollaboratorsForUser(
  102. this.userId,
  103. this.callback
  104. )
  105. })
  106. it('should return the number of collaborators the user is allowed', function () {
  107. this.callback
  108. .calledWith(null, this.user.features.collaborators)
  109. .should.equal(true)
  110. })
  111. })
  112. })
  113. describe('canAddXCollaborators', function () {
  114. describe('when the project has fewer collaborators than allowed', function () {
  115. beforeEach(function () {
  116. this.current_number = 1
  117. this.allowed_number = 2
  118. this.invite_count = 0
  119. this.CollaboratorsGetter.getInvitedCollaboratorCount = (
  120. projectId,
  121. callback
  122. ) => callback(null, this.current_number)
  123. this.CollaboratorsInviteHandler.getInviteCount = (
  124. projectId,
  125. callback
  126. ) => callback(null, this.invite_count)
  127. sinon
  128. .stub(
  129. this.LimitationsManager,
  130. 'allowedNumberOfCollaboratorsInProject'
  131. )
  132. .callsFake((projectId, callback) => {
  133. callback(null, this.allowed_number)
  134. })
  135. this.callback = sinon.stub()
  136. this.LimitationsManager.canAddXCollaborators(
  137. this.projectId,
  138. 1,
  139. this.callback
  140. )
  141. })
  142. it('should return true', function () {
  143. this.callback.calledWith(null, true).should.equal(true)
  144. })
  145. })
  146. describe('when the project has fewer collaborators and invites than allowed', function () {
  147. beforeEach(function () {
  148. this.current_number = 1
  149. this.allowed_number = 4
  150. this.invite_count = 1
  151. this.CollaboratorsGetter.getInvitedCollaboratorCount = (
  152. projectId,
  153. callback
  154. ) => callback(null, this.current_number)
  155. this.CollaboratorsInviteHandler.getInviteCount = (
  156. projectId,
  157. callback
  158. ) => callback(null, this.invite_count)
  159. sinon
  160. .stub(
  161. this.LimitationsManager,
  162. 'allowedNumberOfCollaboratorsInProject'
  163. )
  164. .callsFake((projectId, callback) => {
  165. callback(null, this.allowed_number)
  166. })
  167. this.callback = sinon.stub()
  168. this.LimitationsManager.canAddXCollaborators(
  169. this.projectId,
  170. 1,
  171. this.callback
  172. )
  173. })
  174. it('should return true', function () {
  175. this.callback.calledWith(null, true).should.equal(true)
  176. })
  177. })
  178. describe('when the project has fewer collaborators than allowed but I want to add more than allowed', function () {
  179. beforeEach(function () {
  180. this.current_number = 1
  181. this.allowed_number = 2
  182. this.invite_count = 0
  183. this.CollaboratorsGetter.getInvitedCollaboratorCount = (
  184. projectId,
  185. callback
  186. ) => callback(null, this.current_number)
  187. this.CollaboratorsInviteHandler.getInviteCount = (
  188. projectId,
  189. callback
  190. ) => callback(null, this.invite_count)
  191. sinon
  192. .stub(
  193. this.LimitationsManager,
  194. 'allowedNumberOfCollaboratorsInProject'
  195. )
  196. .callsFake((projectId, callback) => {
  197. callback(null, this.allowed_number)
  198. })
  199. this.callback = sinon.stub()
  200. this.LimitationsManager.canAddXCollaborators(
  201. this.projectId,
  202. 2,
  203. this.callback
  204. )
  205. })
  206. it('should return false', function () {
  207. this.callback.calledWith(null, false).should.equal(true)
  208. })
  209. })
  210. describe('when the project has more collaborators than allowed', function () {
  211. beforeEach(function () {
  212. this.current_number = 3
  213. this.allowed_number = 2
  214. this.invite_count = 0
  215. this.CollaboratorsGetter.getInvitedCollaboratorCount = (
  216. projectId,
  217. callback
  218. ) => callback(null, this.current_number)
  219. this.CollaboratorsInviteHandler.getInviteCount = (
  220. projectId,
  221. callback
  222. ) => callback(null, this.invite_count)
  223. sinon
  224. .stub(
  225. this.LimitationsManager,
  226. 'allowedNumberOfCollaboratorsInProject'
  227. )
  228. .callsFake((projectId, callback) => {
  229. callback(null, this.allowed_number)
  230. })
  231. this.callback = sinon.stub()
  232. this.LimitationsManager.canAddXCollaborators(
  233. this.projectId,
  234. 1,
  235. this.callback
  236. )
  237. })
  238. it('should return false', function () {
  239. this.callback.calledWith(null, false).should.equal(true)
  240. })
  241. })
  242. describe('when the project has infinite collaborators', function () {
  243. beforeEach(function () {
  244. this.current_number = 100
  245. this.allowed_number = -1
  246. this.invite_count = 0
  247. this.CollaboratorsGetter.getInvitedCollaboratorCount = (
  248. projectId,
  249. callback
  250. ) => callback(null, this.current_number)
  251. this.CollaboratorsInviteHandler.getInviteCount = (
  252. projectId,
  253. callback
  254. ) => callback(null, this.invite_count)
  255. sinon
  256. .stub(
  257. this.LimitationsManager,
  258. 'allowedNumberOfCollaboratorsInProject'
  259. )
  260. .callsFake((projectId, callback) => {
  261. callback(null, this.allowed_number)
  262. })
  263. this.callback = sinon.stub()
  264. this.LimitationsManager.canAddXCollaborators(
  265. this.projectId,
  266. 1,
  267. this.callback
  268. )
  269. })
  270. it('should return true', function () {
  271. this.callback.calledWith(null, true).should.equal(true)
  272. })
  273. })
  274. describe('when the project has more invites than allowed', function () {
  275. beforeEach(function () {
  276. this.current_number = 0
  277. this.allowed_number = 2
  278. this.invite_count = 2
  279. this.CollaboratorsGetter.getInvitedCollaboratorCount = (
  280. projectId,
  281. callback
  282. ) => callback(null, this.current_number)
  283. this.CollaboratorsInviteHandler.getInviteCount = (
  284. projectId,
  285. callback
  286. ) => callback(null, this.invite_count)
  287. sinon
  288. .stub(
  289. this.LimitationsManager,
  290. 'allowedNumberOfCollaboratorsInProject'
  291. )
  292. .callsFake((projectId, callback) => {
  293. callback(null, this.allowed_number)
  294. })
  295. this.callback = sinon.stub()
  296. this.LimitationsManager.canAddXCollaborators(
  297. this.projectId,
  298. 1,
  299. this.callback
  300. )
  301. })
  302. it('should return false', function () {
  303. this.callback.calledWith(null, false).should.equal(true)
  304. })
  305. })
  306. describe('when the project has more invites and collaborators than allowed', function () {
  307. beforeEach(function () {
  308. this.current_number = 1
  309. this.allowed_number = 2
  310. this.invite_count = 1
  311. this.CollaboratorsGetter.getInvitedCollaboratorCount = (
  312. projectId,
  313. callback
  314. ) => callback(null, this.current_number)
  315. this.CollaboratorsInviteHandler.getInviteCount = (
  316. projectId,
  317. callback
  318. ) => callback(null, this.invite_count)
  319. sinon
  320. .stub(
  321. this.LimitationsManager,
  322. 'allowedNumberOfCollaboratorsInProject'
  323. )
  324. .callsFake((projectId, callback) => {
  325. callback(null, this.allowed_number)
  326. })
  327. this.callback = sinon.stub()
  328. this.LimitationsManager.canAddXCollaborators(
  329. this.projectId,
  330. 1,
  331. this.callback
  332. )
  333. })
  334. it('should return false', function () {
  335. this.callback.calledWith(null, false).should.equal(true)
  336. })
  337. })
  338. })
  339. describe('userHasV2Subscription', function () {
  340. beforeEach(function () {
  341. this.SubscriptionLocator.getUsersSubscription = sinon.stub()
  342. })
  343. it('should return true if the recurly token is set', function (done) {
  344. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {
  345. recurlySubscription_id: '1234',
  346. })
  347. this.LimitationsManager.userHasV2Subscription(
  348. this.user,
  349. (err, hasSubscription) => {
  350. hasSubscription.should.equal(true)
  351. done()
  352. }
  353. )
  354. })
  355. it('should return false if the recurly token is not set', function (done) {
  356. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  357. this.subscription = {}
  358. this.LimitationsManager.userHasV2Subscription(
  359. this.user,
  360. (err, hasSubscription) => {
  361. hasSubscription.should.equal(false)
  362. done()
  363. }
  364. )
  365. })
  366. it('should return false if the subscription is undefined', function (done) {
  367. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1)
  368. this.LimitationsManager.userHasV2Subscription(
  369. this.user,
  370. (err, hasSubscription) => {
  371. hasSubscription.should.equal(false)
  372. done()
  373. }
  374. )
  375. })
  376. it('should return the subscription', function (done) {
  377. const stubbedSubscription = { freeTrial: {}, token: '' }
  378. this.SubscriptionLocator.getUsersSubscription.callsArgWith(
  379. 1,
  380. null,
  381. stubbedSubscription
  382. )
  383. this.LimitationsManager.userHasV2Subscription(
  384. this.user,
  385. (err, hasSubOrIsGroupMember, subscription) => {
  386. subscription.should.deep.equal(stubbedSubscription)
  387. done()
  388. }
  389. )
  390. })
  391. describe('when user has a custom account', function () {
  392. beforeEach(function () {
  393. this.fakeSubscription = { customAccount: true }
  394. this.SubscriptionLocator.getUsersSubscription.callsArgWith(
  395. 1,
  396. null,
  397. this.fakeSubscription
  398. )
  399. })
  400. it('should return true', function (done) {
  401. this.LimitationsManager.userHasV2Subscription(
  402. this.user,
  403. (err, hasSubscription, subscription) => {
  404. hasSubscription.should.equal(true)
  405. done()
  406. }
  407. )
  408. })
  409. it('should return the subscription', function (done) {
  410. this.LimitationsManager.userHasV2Subscription(
  411. this.user,
  412. (err, hasSubscription, subscription) => {
  413. subscription.should.deep.equal(this.fakeSubscription)
  414. done()
  415. }
  416. )
  417. })
  418. })
  419. })
  420. describe('userIsMemberOfGroupSubscription', function () {
  421. beforeEach(function () {
  422. this.SubscriptionLocator.getMemberSubscriptions = sinon.stub()
  423. })
  424. it('should return false if there are no groups subcriptions', function (done) {
  425. this.SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, [])
  426. this.LimitationsManager.userIsMemberOfGroupSubscription(
  427. this.user,
  428. (err, isMember) => {
  429. isMember.should.equal(false)
  430. done()
  431. }
  432. )
  433. })
  434. it('should return true if there are no groups subcriptions', function (done) {
  435. let subscriptions
  436. this.SubscriptionLocator.getMemberSubscriptions.callsArgWith(
  437. 1,
  438. null,
  439. (subscriptions = ['mock-subscription'])
  440. )
  441. this.LimitationsManager.userIsMemberOfGroupSubscription(
  442. this.user,
  443. (err, isMember, retSubscriptions) => {
  444. isMember.should.equal(true)
  445. retSubscriptions.should.equal(subscriptions)
  446. done()
  447. }
  448. )
  449. })
  450. })
  451. describe('hasPaidSubscription', function () {
  452. beforeEach(function () {
  453. this.LimitationsManager.userIsMemberOfGroupSubscription = sinon
  454. .stub()
  455. .yields(null, false)
  456. this.LimitationsManager.userHasV2Subscription = sinon
  457. .stub()
  458. .yields(null, false)
  459. this.LimitationsManager.userHasV1Subscription = sinon
  460. .stub()
  461. .yields(null, false)
  462. })
  463. it('should return true if userIsMemberOfGroupSubscription', function (done) {
  464. this.LimitationsManager.userIsMemberOfGroupSubscription = sinon
  465. .stub()
  466. .yields(null, true)
  467. this.LimitationsManager.hasPaidSubscription(
  468. this.user,
  469. (err, hasSubOrIsGroupMember) => {
  470. hasSubOrIsGroupMember.should.equal(true)
  471. done()
  472. }
  473. )
  474. })
  475. it('should return true if userHasV2Subscription', function (done) {
  476. this.LimitationsManager.userHasV2Subscription = sinon
  477. .stub()
  478. .yields(null, true)
  479. this.LimitationsManager.hasPaidSubscription(
  480. this.user,
  481. (err, hasSubOrIsGroupMember) => {
  482. hasSubOrIsGroupMember.should.equal(true)
  483. done()
  484. }
  485. )
  486. })
  487. it('should return true if userHasV1Subscription', function (done) {
  488. this.LimitationsManager.userHasV1Subscription = sinon
  489. .stub()
  490. .yields(null, true)
  491. this.LimitationsManager.hasPaidSubscription(
  492. this.user,
  493. (err, hasSubOrIsGroupMember) => {
  494. hasSubOrIsGroupMember.should.equal(true)
  495. done()
  496. }
  497. )
  498. })
  499. it('should return false if none are true', function (done) {
  500. this.LimitationsManager.hasPaidSubscription(
  501. this.user,
  502. (err, hasSubOrIsGroupMember) => {
  503. hasSubOrIsGroupMember.should.equal(false)
  504. done()
  505. }
  506. )
  507. })
  508. it('should have userHasSubscriptionOrIsGroupMember alias', function (done) {
  509. this.LimitationsManager.userHasSubscriptionOrIsGroupMember(
  510. this.user,
  511. (err, hasSubOrIsGroupMember) => {
  512. hasSubOrIsGroupMember.should.equal(false)
  513. done()
  514. }
  515. )
  516. })
  517. })
  518. describe('userHasV1OrV2Subscription', function () {
  519. beforeEach(function () {
  520. this.LimitationsManager.userHasV2Subscription = sinon
  521. .stub()
  522. .yields(null, false)
  523. this.LimitationsManager.userHasV1Subscription = sinon
  524. .stub()
  525. .yields(null, false)
  526. })
  527. it('should return true if userHasV2Subscription', function (done) {
  528. this.LimitationsManager.userHasV2Subscription = sinon
  529. .stub()
  530. .yields(null, true)
  531. this.LimitationsManager.userHasV1OrV2Subscription(
  532. this.user,
  533. (err, hasSub) => {
  534. hasSub.should.equal(true)
  535. done()
  536. }
  537. )
  538. })
  539. it('should return true if userHasV1Subscription', function (done) {
  540. this.LimitationsManager.userHasV1Subscription = sinon
  541. .stub()
  542. .yields(null, true)
  543. this.LimitationsManager.userHasV1OrV2Subscription(
  544. this.user,
  545. (err, hasSub) => {
  546. hasSub.should.equal(true)
  547. done()
  548. }
  549. )
  550. })
  551. it('should return false if none are true', function (done) {
  552. this.LimitationsManager.userHasV1OrV2Subscription(
  553. this.user,
  554. (err, hasSub) => {
  555. hasSub.should.equal(false)
  556. done()
  557. }
  558. )
  559. })
  560. })
  561. describe('hasGroupMembersLimitReached', function () {
  562. beforeEach(function () {
  563. this.subscriptionId = '12312'
  564. this.subscription = {
  565. membersLimit: 3,
  566. member_ids: ['', ''],
  567. teamInvites: [
  568. { email: 'bob@example.com', sentAt: new Date(), token: 'hey' },
  569. ],
  570. }
  571. })
  572. it('should return true if the limit is hit (including members and invites)', function (done) {
  573. this.SubscriptionLocator.getSubscription.callsArgWith(
  574. 1,
  575. null,
  576. this.subscription
  577. )
  578. this.LimitationsManager.hasGroupMembersLimitReached(
  579. this.subscriptionId,
  580. (err, limitReached) => {
  581. limitReached.should.equal(true)
  582. done()
  583. }
  584. )
  585. })
  586. it('should return false if the limit is not hit (including members and invites)', function (done) {
  587. this.subscription.membersLimit = 4
  588. this.SubscriptionLocator.getSubscription.callsArgWith(
  589. 1,
  590. null,
  591. this.subscription
  592. )
  593. this.LimitationsManager.hasGroupMembersLimitReached(
  594. this.subscriptionId,
  595. (err, limitReached) => {
  596. limitReached.should.equal(false)
  597. done()
  598. }
  599. )
  600. })
  601. it('should return true if the limit has been exceded (including members and invites)', function (done) {
  602. this.subscription.membersLimit = 2
  603. this.SubscriptionLocator.getSubscription.callsArgWith(
  604. 1,
  605. null,
  606. this.subscription
  607. )
  608. this.LimitationsManager.hasGroupMembersLimitReached(
  609. this.subscriptionId,
  610. (err, limitReached) => {
  611. limitReached.should.equal(true)
  612. done()
  613. }
  614. )
  615. })
  616. })
  617. describe('userHasV1Subscription', function () {
  618. it('should return true if v1 returns has_subscription = true', function (done) {
  619. this.V1SubscriptionManager.getSubscriptionsFromV1 = sinon
  620. .stub()
  621. .yields(null, { has_subscription: true })
  622. this.LimitationsManager.userHasV1Subscription(
  623. this.user,
  624. (error, result) => {
  625. this.V1SubscriptionManager.getSubscriptionsFromV1
  626. .calledWith(this.userId)
  627. .should.equal(true)
  628. result.should.equal(true)
  629. done()
  630. }
  631. )
  632. })
  633. it('should return false if v1 returns has_subscription = false', function (done) {
  634. this.V1SubscriptionManager.getSubscriptionsFromV1 = sinon
  635. .stub()
  636. .yields(null, { has_subscription: false })
  637. this.LimitationsManager.userHasV1Subscription(
  638. this.user,
  639. (error, result) => {
  640. this.V1SubscriptionManager.getSubscriptionsFromV1
  641. .calledWith(this.userId)
  642. .should.equal(true)
  643. result.should.equal(false)
  644. done()
  645. }
  646. )
  647. })
  648. it('should return false if v1 returns nothing', function (done) {
  649. this.V1SubscriptionManager.getSubscriptionsFromV1 = sinon
  650. .stub()
  651. .yields(null, null)
  652. this.LimitationsManager.userHasV1Subscription(
  653. this.user,
  654. (error, result) => {
  655. this.V1SubscriptionManager.getSubscriptionsFromV1
  656. .calledWith(this.userId)
  657. .should.equal(true)
  658. result.should.equal(false)
  659. done()
  660. }
  661. )
  662. })
  663. })
  664. })