SubscriptionGroupHandlerTests.js 7.1 KB

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