TeamInvitesHandlerTests.js 12 KB

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