TeamInvitesHandlerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const modulePath =
  5. '../../../../app/src/Features/Subscription/TeamInvitesHandler'
  6. const { ObjectId } = require('mongodb')
  7. const Errors = require('../../../../app/src/Features/Errors/Errors')
  8. describe('TeamInvitesHandler', function () {
  9. beforeEach(function () {
  10. this.manager = {
  11. _id: '666666',
  12. first_name: 'Daenerys',
  13. last_name: 'Targaryen',
  14. email: 'daenerys@example.com',
  15. emails: [{ email: 'daenerys@example.com' }],
  16. }
  17. this.token = 'aaaaaaaaaaaaaaaaaaaaaa'
  18. this.teamInvite = {
  19. email: 'jorah@example.com',
  20. token: this.token,
  21. }
  22. // ensure teamInvite can be converted from Document to Object
  23. this.teamInvite.toObject = () => this.teamInvite
  24. this.subscription = {
  25. id: '55153a8014829a865bbf700d',
  26. _id: new ObjectId('55153a8014829a865bbf700d'),
  27. admin_id: this.manager._id,
  28. groupPlan: true,
  29. member_ids: [],
  30. teamInvites: [this.teamInvite],
  31. save: sinon.stub().yields(null),
  32. }
  33. this.SubscriptionLocator = {
  34. getUsersSubscription: sinon.stub(),
  35. getSubscription: sinon.stub().yields(null, this.subscription),
  36. }
  37. this.UserGetter = {
  38. getUser: sinon.stub().yields(),
  39. getUserByAnyEmail: sinon.stub().yields(),
  40. }
  41. this.SubscriptionUpdater = {
  42. addUserToGroup: sinon.stub().yields(),
  43. }
  44. this.LimitationsManager = {
  45. teamHasReachedMemberLimit: sinon.stub().returns(false),
  46. }
  47. this.Subscription = {
  48. findOne: sinon.stub().yields(),
  49. updateOne: sinon.stub().yields(),
  50. }
  51. this.EmailHandler = {
  52. sendEmail: sinon.stub().yields(null),
  53. }
  54. this.newToken = 'bbbbbbbbb'
  55. this.crypto = {
  56. randomBytes: () => {
  57. return { toString: sinon.stub().returns(this.newToken) }
  58. },
  59. }
  60. this.UserGetter.getUser
  61. .withArgs(this.manager._id)
  62. .yields(null, this.manager)
  63. this.UserGetter.getUserByAnyEmail
  64. .withArgs(this.manager.email)
  65. .yields(null, this.manager)
  66. this.SubscriptionLocator.getUsersSubscription.yields(
  67. null,
  68. this.subscription
  69. )
  70. this.Subscription.findOne.yields(null, this.subscription)
  71. this.TeamInvitesHandler = SandboxedModule.require(modulePath, {
  72. requires: {
  73. mongodb: { ObjectId },
  74. crypto: this.crypto,
  75. '@overleaf/settings': { siteUrl: 'http://example.com' },
  76. '../../models/TeamInvite': { TeamInvite: (this.TeamInvite = {}) },
  77. '../../models/Subscription': { Subscription: this.Subscription },
  78. '../User/UserGetter': this.UserGetter,
  79. './SubscriptionLocator': this.SubscriptionLocator,
  80. './SubscriptionUpdater': this.SubscriptionUpdater,
  81. './LimitationsManager': this.LimitationsManager,
  82. '../Email/EmailHandler': this.EmailHandler,
  83. },
  84. })
  85. })
  86. describe('getInvite', function () {
  87. it("returns the invite if there's one", function (done) {
  88. this.TeamInvitesHandler.getInvite(
  89. this.token,
  90. (err, invite, subscription) => {
  91. expect(err).to.eq(null)
  92. expect(invite).to.deep.eq(this.teamInvite)
  93. expect(subscription).to.deep.eq(this.subscription)
  94. done()
  95. }
  96. )
  97. })
  98. it("returns teamNotFound if there's none", function (done) {
  99. this.Subscription.findOne = sinon.stub().yields(null, null)
  100. this.TeamInvitesHandler.getInvite(
  101. this.token,
  102. (err, invite, subscription) => {
  103. expect(err).to.be.instanceof(Errors.NotFoundError)
  104. done()
  105. }
  106. )
  107. })
  108. })
  109. describe('createInvite', function () {
  110. it('adds the team invite to the subscription', function (done) {
  111. this.TeamInvitesHandler.createInvite(
  112. this.manager._id,
  113. this.subscription,
  114. 'John.Snow@example.com',
  115. (err, invite) => {
  116. expect(err).to.eq(null)
  117. expect(invite.token).to.eq(this.newToken)
  118. expect(invite.email).to.eq('john.snow@example.com')
  119. expect(invite.inviterName).to.eq(
  120. 'Daenerys Targaryen (daenerys@example.com)'
  121. )
  122. expect(invite.invite).to.be.true
  123. expect(this.subscription.teamInvites).to.deep.include(invite)
  124. done()
  125. }
  126. )
  127. })
  128. it('sends an email', function (done) {
  129. this.TeamInvitesHandler.createInvite(
  130. this.manager._id,
  131. this.subscription,
  132. 'John.Snow@example.com',
  133. (err, invite) => {
  134. this.EmailHandler.sendEmail
  135. .calledWith(
  136. 'verifyEmailToJoinTeam',
  137. sinon.match({
  138. to: 'john.snow@example.com',
  139. inviter: this.manager,
  140. acceptInviteUrl: `http://example.com/subscription/invites/${this.newToken}/`,
  141. })
  142. )
  143. .should.equal(true)
  144. done(err)
  145. }
  146. )
  147. })
  148. it('refreshes the existing invite if the email has already been invited', function (done) {
  149. const originalInvite = Object.assign({}, this.teamInvite)
  150. this.TeamInvitesHandler.createInvite(
  151. this.manager._id,
  152. this.subscription,
  153. originalInvite.email,
  154. (err, invite) => {
  155. expect(err).to.eq(null)
  156. expect(invite).to.exist
  157. expect(this.subscription.teamInvites.length).to.eq(1)
  158. expect(this.subscription.teamInvites).to.deep.include(invite)
  159. expect(invite.email).to.eq(originalInvite.email)
  160. this.subscription.save.calledOnce.should.eq(true)
  161. done()
  162. }
  163. )
  164. })
  165. it('removes any legacy invite from the subscription', function (done) {
  166. this.TeamInvitesHandler.createInvite(
  167. this.manager._id,
  168. this.subscription,
  169. 'John.Snow@example.com',
  170. (err, invite) => {
  171. this.Subscription.updateOne
  172. .calledWith(
  173. { _id: new ObjectId('55153a8014829a865bbf700d') },
  174. { $pull: { invited_emails: 'john.snow@example.com' } }
  175. )
  176. .should.eq(true)
  177. done(err)
  178. }
  179. )
  180. })
  181. it('add user to subscription if inviting self', function (done) {
  182. this.TeamInvitesHandler.createInvite(
  183. this.manager._id,
  184. this.subscription,
  185. this.manager.email,
  186. (err, invite) => {
  187. sinon.assert.calledWith(
  188. this.SubscriptionUpdater.addUserToGroup,
  189. this.subscription._id,
  190. this.manager._id
  191. )
  192. sinon.assert.notCalled(this.subscription.save)
  193. expect(invite.token).to.not.exist
  194. expect(invite.email).to.eq(this.manager.email)
  195. expect(invite.first_name).to.eq(this.manager.first_name)
  196. expect(invite.last_name).to.eq(this.manager.last_name)
  197. expect(invite.invite).to.be.false
  198. done(err)
  199. }
  200. )
  201. })
  202. })
  203. describe('importInvite', function () {
  204. beforeEach(function () {
  205. this.sentAt = new Date()
  206. })
  207. it('can imports an invite from v1', function () {
  208. this.TeamInvitesHandler.importInvite(
  209. this.subscription,
  210. 'A-Team',
  211. 'hannibal@a-team.org',
  212. 'secret',
  213. this.sentAt,
  214. error => {
  215. expect(error).not.to.exist
  216. this.subscription.save.calledOnce.should.eq(true)
  217. const invite = this.subscription.teamInvites.find(
  218. i => i.email === 'hannibal@a-team.org'
  219. )
  220. expect(invite.token).to.eq('secret')
  221. expect(invite.sentAt).to.eq(this.sentAt)
  222. }
  223. )
  224. })
  225. })
  226. describe('acceptInvite', function () {
  227. beforeEach(function () {
  228. this.user = {
  229. id: '123456789',
  230. first_name: 'Tyrion',
  231. last_name: 'Lannister',
  232. email: 'tyrion@example.com',
  233. }
  234. this.UserGetter.getUserByAnyEmail
  235. .withArgs(this.user.email)
  236. .yields(null, this.user)
  237. this.subscription.teamInvites.push({
  238. email: 'john.snow@example.com',
  239. token: 'dddddddd',
  240. inviterName: 'Daenerys Targaryen (daenerys@example.com)',
  241. })
  242. })
  243. it('adds the user to the team', function (done) {
  244. this.TeamInvitesHandler.acceptInvite('dddddddd', this.user.id, () => {
  245. this.SubscriptionUpdater.addUserToGroup
  246. .calledWith(this.subscription._id, this.user.id)
  247. .should.eq(true)
  248. done()
  249. })
  250. })
  251. it('removes the invite from the subscription', function (done) {
  252. this.TeamInvitesHandler.acceptInvite('dddddddd', this.user.id, () => {
  253. this.Subscription.updateOne
  254. .calledWith(
  255. { _id: new ObjectId('55153a8014829a865bbf700d') },
  256. { $pull: { teamInvites: { email: 'john.snow@example.com' } } }
  257. )
  258. .should.eq(true)
  259. done()
  260. })
  261. })
  262. })
  263. describe('revokeInvite', function () {
  264. it('removes the team invite from the subscription', function (done) {
  265. this.TeamInvitesHandler.revokeInvite(
  266. this.manager._id,
  267. this.subscription,
  268. 'jorah@example.com',
  269. () => {
  270. this.Subscription.updateOne
  271. .calledWith(
  272. { _id: new ObjectId('55153a8014829a865bbf700d') },
  273. { $pull: { teamInvites: { email: 'jorah@example.com' } } }
  274. )
  275. .should.eq(true)
  276. this.Subscription.updateOne
  277. .calledWith(
  278. { _id: new ObjectId('55153a8014829a865bbf700d') },
  279. { $pull: { invited_emails: 'jorah@example.com' } }
  280. )
  281. .should.eq(true)
  282. done()
  283. }
  284. )
  285. })
  286. })
  287. describe('createTeamInvitesForLegacyInvitedEmail', function (done) {
  288. beforeEach(function () {
  289. this.subscription.invited_emails = [
  290. 'eddard@example.com',
  291. 'robert@example.com',
  292. ]
  293. this.TeamInvitesHandler.createInvite = sinon.stub().yields(null)
  294. this.SubscriptionLocator.getGroupsWithEmailInvite = sinon
  295. .stub()
  296. .yields(null, [this.subscription])
  297. })
  298. it('sends an invitation email to addresses in the legacy invited_emails field', function (done) {
  299. this.TeamInvitesHandler.createTeamInvitesForLegacyInvitedEmail(
  300. 'eddard@example.com',
  301. (err, invite) => {
  302. expect(err).not.to.exist
  303. this.TeamInvitesHandler.createInvite
  304. .calledWith(
  305. this.subscription.admin_id,
  306. this.subscription,
  307. 'eddard@example.com'
  308. )
  309. .should.eq(true)
  310. this.TeamInvitesHandler.createInvite.callCount.should.eq(1)
  311. done()
  312. }
  313. )
  314. })
  315. })
  316. describe('validation', function () {
  317. it("doesn't create an invite if the team limit has been reached", function (done) {
  318. this.LimitationsManager.teamHasReachedMemberLimit = sinon
  319. .stub()
  320. .returns(true)
  321. this.TeamInvitesHandler.createInvite(
  322. this.manager._id,
  323. this.subscription,
  324. 'John.Snow@example.com',
  325. (err, invite) => {
  326. expect(err).to.deep.equal({ limitReached: true })
  327. done()
  328. }
  329. )
  330. })
  331. it("doesn't create an invite if the subscription is not in a group plan", function (done) {
  332. this.subscription.groupPlan = false
  333. this.TeamInvitesHandler.createInvite(
  334. this.manager._id,
  335. this.subscription,
  336. 'John.Snow@example.com',
  337. (err, invite) => {
  338. expect(err).to.deep.equal({ wrongPlan: true })
  339. done()
  340. }
  341. )
  342. })
  343. it("doesn't create an invite if the user is already part of the team", function (done) {
  344. const member = {
  345. id: '1a2b',
  346. _id: '1a2b',
  347. email: 'tyrion@example.com',
  348. }
  349. this.subscription.member_ids = [member.id]
  350. this.UserGetter.getUserByAnyEmail
  351. .withArgs(member.email)
  352. .yields(null, member)
  353. this.TeamInvitesHandler.createInvite(
  354. this.manager._id,
  355. this.subscription,
  356. 'tyrion@example.com',
  357. (err, invite) => {
  358. expect(err).to.deep.equal({ alreadyInTeam: true })
  359. expect(invite).not.to.exist
  360. done()
  361. }
  362. )
  363. })
  364. })
  365. })