SubscriptionGroupHandlerTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-dupe-keys,
  5. no-return-assign,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const SandboxedModule = require('sandboxed-module')
  16. const sinon = require('sinon')
  17. const { assert, expect } = require('chai')
  18. const modulePath =
  19. '../../../../app/src/Features/Subscription/SubscriptionGroupHandler'
  20. describe('SubscriptionGroupHandler', function () {
  21. beforeEach(function () {
  22. this.adminUser_id = '12321'
  23. this.newEmail = 'bob@smith.com'
  24. this.user_id = '3121321'
  25. this.email = 'jim@example.com'
  26. this.user = { _id: this.user_id, email: this.newEmail }
  27. this.subscription_id = '31DSd1123D'
  28. this.subscription = {
  29. admin_id: this.adminUser_id,
  30. manager_ids: [this.adminUser_id],
  31. _id: this.subscription_id,
  32. }
  33. this.SubscriptionLocator = {
  34. getUsersSubscription: sinon.stub(),
  35. getSubscriptionByMemberIdAndId: sinon.stub(),
  36. getSubscription: sinon.stub().callsArgWith(1, null, this.subscription),
  37. }
  38. this.UserCreator = {
  39. getUserOrCreateHoldingAccount: sinon
  40. .stub()
  41. .callsArgWith(1, null, this.user),
  42. }
  43. this.SubscriptionUpdater = {
  44. removeUserFromGroup: sinon.stub().callsArgWith(2),
  45. getSubscription: sinon.stub().callsArgWith(2),
  46. }
  47. this.TeamInvitesHandler = { createInvite: sinon.stub().callsArgWith(2) }
  48. this.UserGetter = {
  49. getUser: sinon.stub(),
  50. getUserByAnyEmail: sinon.stub(),
  51. }
  52. this.LimitationsManager = { hasGroupMembersLimitReached: sinon.stub() }
  53. this.OneTimeTokenHandler = {
  54. getValueFromTokenAndExpire: sinon.stub(),
  55. getNewToken: sinon.stub(),
  56. }
  57. this.EmailHandler = { sendEmail: sinon.stub() }
  58. this.Subscription = {
  59. updateOne: sinon.stub().yields(),
  60. updateMany: sinon.stub().yields(),
  61. findOne: sinon.stub().yields(),
  62. }
  63. this.settings = { siteUrl: 'http://www.sharelatex.com' }
  64. this.readStub = sinon.stub()
  65. this.NotificationsBuilder = {
  66. groupPlan: sinon.stub().returns({ read: this.readStub }),
  67. }
  68. this.UserMembershipViewModel = {
  69. build(email) {
  70. return { email }
  71. },
  72. }
  73. return (this.Handler = SandboxedModule.require(modulePath, {
  74. requires: {
  75. '../User/UserCreator': this.UserCreator,
  76. './SubscriptionUpdater': this.SubscriptionUpdater,
  77. './SubscriptionLocator': this.SubscriptionLocator,
  78. '../../models/Subscription': {
  79. Subscription: this.Subscription,
  80. },
  81. '../User/UserGetter': this.UserGetter,
  82. './LimitationsManager': this.LimitationsManager,
  83. '../Security/OneTimeTokenHandler': this.OneTimeTokenHandler,
  84. '../Email/EmailHandler': this.EmailHandler,
  85. '@overleaf/settings': this.settings,
  86. '../Notifications/NotificationsBuilder': this.NotificationsBuilder,
  87. '../UserMembership/UserMembershipViewModel': this
  88. .UserMembershipViewModel,
  89. },
  90. }))
  91. })
  92. describe('removeUserFromGroup', function () {
  93. it('should call the subscription updater to remove the user', function (done) {
  94. return this.Handler.removeUserFromGroup(
  95. this.adminUser_id,
  96. this.user._id,
  97. err => {
  98. this.SubscriptionUpdater.removeUserFromGroup
  99. .calledWith(this.adminUser_id, this.user._id)
  100. .should.equal(true)
  101. return done()
  102. }
  103. )
  104. })
  105. })
  106. describe('replaceUserReferencesInGroups', function () {
  107. beforeEach(function (done) {
  108. this.oldId = 'ba5eba11'
  109. this.newId = '5ca1ab1e'
  110. return this.Handler.replaceUserReferencesInGroups(
  111. this.oldId,
  112. this.newId,
  113. () => done()
  114. )
  115. })
  116. it('replaces the admin_id', function () {
  117. return this.Subscription.updateOne
  118. .calledWith({ admin_id: this.oldId }, { admin_id: this.newId })
  119. .should.equal(true)
  120. })
  121. it('replaces the manager_ids', function () {
  122. this.Subscription.updateMany
  123. .calledWith(
  124. { manager_ids: 'ba5eba11' },
  125. { $addToSet: { manager_ids: '5ca1ab1e' } }
  126. )
  127. .should.equal(true)
  128. return this.Subscription.updateMany
  129. .calledWith(
  130. { manager_ids: 'ba5eba11' },
  131. { $pull: { manager_ids: 'ba5eba11' } }
  132. )
  133. .should.equal(true)
  134. })
  135. it('replaces the member ids', function () {
  136. this.Subscription.updateMany
  137. .calledWith(
  138. { member_ids: this.oldId },
  139. { $addToSet: { member_ids: this.newId } }
  140. )
  141. .should.equal(true)
  142. return this.Subscription.updateMany
  143. .calledWith(
  144. { member_ids: this.oldId },
  145. { $pull: { member_ids: this.oldId } }
  146. )
  147. .should.equal(true)
  148. })
  149. })
  150. describe('isUserPartOfGroup', function () {
  151. beforeEach(function () {
  152. return (this.subscription_id = '123ed13123')
  153. })
  154. it('should return true when user is part of subscription', function (done) {
  155. this.SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(
  156. 2,
  157. null,
  158. { _id: this.subscription_id }
  159. )
  160. return this.Handler.isUserPartOfGroup(
  161. this.user_id,
  162. this.subscription_id,
  163. (err, partOfGroup) => {
  164. partOfGroup.should.equal(true)
  165. return done()
  166. }
  167. )
  168. })
  169. it('should return false when no subscription is found', function (done) {
  170. this.SubscriptionLocator.getSubscriptionByMemberIdAndId.callsArgWith(
  171. 2,
  172. null
  173. )
  174. return this.Handler.isUserPartOfGroup(
  175. this.user_id,
  176. this.subscription_id,
  177. (err, partOfGroup) => {
  178. partOfGroup.should.equal(false)
  179. return done()
  180. }
  181. )
  182. })
  183. })
  184. describe('getTotalConfirmedUsersInGroup', function () {
  185. describe('for existing subscriptions', function () {
  186. beforeEach(function () {
  187. return (this.subscription.member_ids = ['12321', '3121321'])
  188. })
  189. it('should call the subscription locator and return 2 users', function (done) {
  190. return this.Handler.getTotalConfirmedUsersInGroup(
  191. this.subscription_id,
  192. (err, count) => {
  193. this.SubscriptionLocator.getSubscription
  194. .calledWith(this.subscription_id)
  195. .should.equal(true)
  196. count.should.equal(2)
  197. return done()
  198. }
  199. )
  200. })
  201. })
  202. describe('for nonexistent subscriptions', function () {
  203. it('should return undefined', function (done) {
  204. return this.Handler.getTotalConfirmedUsersInGroup(
  205. 'fake-id',
  206. (err, count) => {
  207. expect(count).not.to.exist
  208. return done()
  209. }
  210. )
  211. })
  212. })
  213. })
  214. })