TeamInvitesHandlerTests.js 12 KB

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