TeamInvitesHandlerTests.js 12 KB

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