ProjectInviteTests.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. const { expect } = require('chai')
  2. const Async = require('async')
  3. const User = require('./helpers/User')
  4. const settings = require('@overleaf/settings')
  5. const CollaboratorsEmailHandler = require('../../../app/src/Features/Collaborators/CollaboratorsEmailHandler')
  6. const Features = require('../../../app/src/infrastructure/Features')
  7. const createInvite = (sendingUser, projectId, email, callback) => {
  8. sendingUser.getCsrfToken(err => {
  9. if (err) {
  10. return callback(err)
  11. }
  12. sendingUser.request.post(
  13. {
  14. uri: `/project/${projectId}/invite`,
  15. json: {
  16. email,
  17. privileges: 'readAndWrite',
  18. },
  19. },
  20. (err, response, body) => {
  21. if (err) {
  22. return callback(err)
  23. }
  24. expect(response.statusCode).to.equal(200)
  25. expect(body.error).to.not.exist
  26. expect(body.invite).to.exist
  27. callback(null, body.invite)
  28. }
  29. )
  30. })
  31. }
  32. const createProject = (owner, projectName, callback) => {
  33. owner.createProject(projectName, (err, projectId) => {
  34. if (err) {
  35. throw err
  36. }
  37. const fakeProject = {
  38. _id: projectId,
  39. name: projectName,
  40. owner_ref: owner,
  41. }
  42. callback(err, projectId, fakeProject)
  43. })
  44. }
  45. const createProjectAndInvite = (owner, projectName, email, callback) => {
  46. createProject(owner, projectName, (err, projectId, project) => {
  47. if (err) {
  48. return callback(err)
  49. }
  50. createInvite(owner, projectId, email, (err, invite) => {
  51. if (err) {
  52. return callback(err)
  53. }
  54. const link = CollaboratorsEmailHandler._buildInviteUrl(project, invite)
  55. callback(null, project, invite, link)
  56. })
  57. })
  58. }
  59. const revokeInvite = (sendingUser, projectId, inviteId, callback) => {
  60. sendingUser.getCsrfToken(err => {
  61. if (err) {
  62. return callback(err)
  63. }
  64. sendingUser.request.delete(
  65. {
  66. uri: `/project/${projectId}/invite/${inviteId}`,
  67. },
  68. err => {
  69. if (err) {
  70. return callback(err)
  71. }
  72. callback()
  73. }
  74. )
  75. })
  76. }
  77. // Actions
  78. const tryFollowInviteLink = (user, link, callback) => {
  79. user.request.get(
  80. {
  81. uri: link,
  82. baseUrl: null,
  83. },
  84. callback
  85. )
  86. }
  87. const tryAcceptInvite = (user, invite, callback) => {
  88. user.getCsrfToken(err => {
  89. if (err) {
  90. return callback(err)
  91. }
  92. user.request.post(
  93. {
  94. uri: `/project/${invite.projectId}/invite/token/${invite.token}/accept`,
  95. json: {
  96. token: invite.token,
  97. },
  98. },
  99. callback
  100. )
  101. })
  102. }
  103. const tryRegisterUser = (user, email, callback) => {
  104. user.getCsrfToken(error => {
  105. if (error != null) {
  106. return callback(error)
  107. }
  108. user.request.post(
  109. {
  110. url: '/register',
  111. json: {
  112. email,
  113. password: 'some_weird_password',
  114. },
  115. },
  116. callback
  117. )
  118. })
  119. }
  120. const tryFollowLoginLink = (user, loginLink, callback) => {
  121. user.getCsrfToken(error => {
  122. if (error != null) {
  123. return callback(error)
  124. }
  125. user.request.get(loginLink, callback)
  126. })
  127. }
  128. const tryLoginUser = (user, callback) => {
  129. user.getCsrfToken(error => {
  130. if (error != null) {
  131. return callback(error)
  132. }
  133. user.request.post(
  134. {
  135. url: '/login',
  136. json: {
  137. email: user.email,
  138. password: user.password,
  139. 'g-recaptcha-response': 'valid',
  140. },
  141. },
  142. callback
  143. )
  144. })
  145. }
  146. const tryGetInviteList = (user, projectId, callback) => {
  147. user.getCsrfToken(error => {
  148. if (error != null) {
  149. return callback(error)
  150. }
  151. user.request.get(
  152. {
  153. url: `/project/${projectId}/invites`,
  154. json: true,
  155. },
  156. callback
  157. )
  158. })
  159. }
  160. const tryJoinProject = (user, projectId, callback) => {
  161. return user.getCsrfToken(error => {
  162. if (error != null) {
  163. return callback(error)
  164. }
  165. user.request.post(
  166. {
  167. url: `/project/${projectId}/join`,
  168. auth: {
  169. user: settings.apis.web.user,
  170. pass: settings.apis.web.pass,
  171. sendImmediately: true,
  172. },
  173. json: { userId: user._id },
  174. jar: false,
  175. },
  176. callback
  177. )
  178. })
  179. }
  180. // Expectations
  181. const expectProjectAccess = (user, projectId, callback) => {
  182. // should have access to project
  183. user.openProject(projectId, err => {
  184. expect(err).not.to.exist
  185. callback()
  186. })
  187. }
  188. const expectNoProjectAccess = (user, projectId, callback) => {
  189. // should not have access to project page
  190. user.openProject(projectId, err => {
  191. expect(err).to.be.instanceof(Error)
  192. callback()
  193. })
  194. }
  195. const expectInvitePage = (user, link, callback) => {
  196. // view invite
  197. tryFollowInviteLink(user, link, (err, response, body) => {
  198. expect(err).not.to.exist
  199. expect(response.statusCode).to.equal(200)
  200. expect(body).to.match(/<title>Project Invite - .*<\/title>/)
  201. callback()
  202. })
  203. }
  204. const expectInvalidInvitePage = (user, link, callback) => {
  205. // view invalid invite
  206. tryFollowInviteLink(user, link, (err, response, body) => {
  207. expect(err).not.to.exist
  208. expect(response.statusCode).to.equal(200)
  209. expect(body).to.match(/<title>Invalid Invite - .*<\/title>/)
  210. callback()
  211. })
  212. }
  213. const expectInviteRedirectToRegister = (user, link, callback) => {
  214. // view invite, redirect to `/register`
  215. tryFollowInviteLink(user, link, (err, response) => {
  216. expect(err).not.to.exist
  217. expect(response.statusCode).to.equal(302)
  218. expect(response.headers.location).to.match(/^\/register.*$/)
  219. callback()
  220. })
  221. }
  222. const expectLoginPage = (user, callback) => {
  223. tryFollowLoginLink(user, '/login', (err, response, body) => {
  224. expect(err).not.to.exist
  225. expect(response.statusCode).to.equal(200)
  226. expect(body).to.match(/<title>(Login|Log in to Overleaf) - .*<\/title>/)
  227. callback()
  228. })
  229. }
  230. const expectLoginRedirectToInvite = (user, link, callback) => {
  231. tryLoginUser(user, (err, response) => {
  232. expect(err).not.to.exist
  233. expect(response.statusCode).to.equal(200)
  234. callback()
  235. })
  236. }
  237. const expectRegistrationRedirectToInvite = (user, email, link, callback) => {
  238. tryRegisterUser(user, email, (err, response) => {
  239. expect(err).not.to.exist
  240. expect(response.statusCode).to.equal(200)
  241. callback()
  242. })
  243. }
  244. const expectInviteRedirectToProject = (user, link, invite, callback) => {
  245. // view invite, redirect straight to project
  246. tryFollowInviteLink(user, link, (err, response) => {
  247. expect(err).not.to.exist
  248. expect(response.statusCode).to.equal(302)
  249. expect(response.headers.location).to.equal(`/project/${invite.projectId}`)
  250. callback()
  251. })
  252. }
  253. const expectAcceptInviteAndRedirect = (user, invite, callback) => {
  254. // should accept the invite and redirect to project
  255. tryAcceptInvite(user, invite, (err, response) => {
  256. expect(err).not.to.exist
  257. expect(response.statusCode).to.equal(302)
  258. expect(response.headers.location).to.equal(`/project/${invite.projectId}`)
  259. callback()
  260. })
  261. }
  262. const expectInviteListCount = (user, projectId, count, callback) => {
  263. tryGetInviteList(user, projectId, (err, response, body) => {
  264. expect(err).not.to.exist
  265. expect(response.statusCode).to.equal(200)
  266. expect(body).to.have.all.keys(['invites'])
  267. expect(body.invites.length).to.equal(count)
  268. callback()
  269. })
  270. }
  271. const expectInvitesInJoinProjectCount = (user, projectId, count, callback) => {
  272. tryJoinProject(user, projectId, (err, response, body) => {
  273. expect(err).not.to.exist
  274. expect(response.statusCode).to.equal(200)
  275. expect(body.project).to.contain.keys(['invites'])
  276. expect(body.project.invites.length).to.equal(count)
  277. callback()
  278. })
  279. }
  280. // eslint-disable-next-line mocha/no-skipped-tests
  281. describe.skip('ProjectInviteTests', function () {
  282. beforeEach(function (done) {
  283. this.sendingUser = new User()
  284. this.user = new User()
  285. this.site_admin = new User({ email: 'admin@example.com' })
  286. this.email = 'smoketestuser@example.com'
  287. this.projectName = 'sharing test'
  288. Async.series(
  289. [
  290. cb => this.user.ensureUserExists(cb),
  291. cb => this.sendingUser.login(cb),
  292. cb => this.sendingUser.setFeatures({ collaborators: 10 }, cb),
  293. cb =>
  294. this.sendingUser.setFeaturesOverride(
  295. {
  296. note: 'ProjectInviteTests acceptance tests',
  297. features: { collaborators: 10 },
  298. },
  299. cb
  300. ),
  301. ],
  302. done
  303. )
  304. })
  305. describe('creating invites', function () {
  306. beforeEach(function () {
  307. this.projectName = 'wat'
  308. })
  309. describe('creating two invites', function () {
  310. beforeEach(function (done) {
  311. createProject(
  312. this.sendingUser,
  313. this.projectName,
  314. (err, projectId, project) => {
  315. expect(err).not.to.exist
  316. this.projectId = projectId
  317. this.fakeProject = project
  318. done()
  319. }
  320. )
  321. })
  322. it('should allow the project owner to create and remove invites', function (done) {
  323. Async.series(
  324. [
  325. cb => expectProjectAccess(this.sendingUser, this.projectId, cb),
  326. cb =>
  327. expectInviteListCount(this.sendingUser, this.projectId, 0, cb),
  328. // create invite, check invite list count
  329. cb => {
  330. createInvite(
  331. this.sendingUser,
  332. this.projectId,
  333. this.email,
  334. (err, invite) => {
  335. if (err) {
  336. return cb(err)
  337. }
  338. this.invite = invite
  339. cb()
  340. }
  341. )
  342. },
  343. cb =>
  344. expectInviteListCount(this.sendingUser, this.projectId, 1, cb),
  345. cb =>
  346. revokeInvite(
  347. this.sendingUser,
  348. this.projectId,
  349. this.invite._id,
  350. cb
  351. ),
  352. cb =>
  353. expectInviteListCount(this.sendingUser, this.projectId, 0, cb),
  354. // and a second time
  355. cb => {
  356. createInvite(
  357. this.sendingUser,
  358. this.projectId,
  359. this.email,
  360. (err, invite) => {
  361. if (err) {
  362. return cb(err)
  363. }
  364. this.invite = invite
  365. cb()
  366. }
  367. )
  368. },
  369. cb =>
  370. expectInviteListCount(this.sendingUser, this.projectId, 1, cb),
  371. // check the joinProject view
  372. cb =>
  373. expectInvitesInJoinProjectCount(
  374. this.sendingUser,
  375. this.projectId,
  376. 1,
  377. cb
  378. ),
  379. // revoke invite
  380. cb =>
  381. revokeInvite(
  382. this.sendingUser,
  383. this.projectId,
  384. this.invite._id,
  385. cb
  386. ),
  387. cb =>
  388. expectInviteListCount(this.sendingUser, this.projectId, 0, cb),
  389. cb =>
  390. expectInvitesInJoinProjectCount(
  391. this.sendingUser,
  392. this.projectId,
  393. 0,
  394. cb
  395. ),
  396. ],
  397. done
  398. )
  399. })
  400. it('should allow the project owner to create many invites at once', function (done) {
  401. Async.series(
  402. [
  403. cb => expectProjectAccess(this.sendingUser, this.projectId, cb),
  404. cb =>
  405. expectInviteListCount(this.sendingUser, this.projectId, 0, cb),
  406. // create first invite
  407. cb => {
  408. createInvite(
  409. this.sendingUser,
  410. this.projectId,
  411. this.email,
  412. (err, invite) => {
  413. if (err) {
  414. return cb(err)
  415. }
  416. this.inviteOne = invite
  417. cb()
  418. }
  419. )
  420. },
  421. cb =>
  422. expectInviteListCount(this.sendingUser, this.projectId, 1, cb),
  423. // and a second
  424. cb => {
  425. createInvite(
  426. this.sendingUser,
  427. this.projectId,
  428. this.email,
  429. (err, invite) => {
  430. if (err) {
  431. return cb(err)
  432. }
  433. this.inviteTwo = invite
  434. cb()
  435. }
  436. )
  437. },
  438. // should have two
  439. cb =>
  440. expectInviteListCount(this.sendingUser, this.projectId, 2, cb),
  441. cb =>
  442. expectInvitesInJoinProjectCount(
  443. this.sendingUser,
  444. this.projectId,
  445. 2,
  446. cb
  447. ),
  448. // revoke first
  449. cb =>
  450. revokeInvite(
  451. this.sendingUser,
  452. this.projectId,
  453. this.inviteOne._id,
  454. cb
  455. ),
  456. cb =>
  457. expectInviteListCount(this.sendingUser, this.projectId, 1, cb),
  458. // revoke second
  459. cb =>
  460. revokeInvite(
  461. this.sendingUser,
  462. this.projectId,
  463. this.inviteTwo._id,
  464. cb
  465. ),
  466. cb =>
  467. expectInviteListCount(this.sendingUser, this.projectId, 0, cb),
  468. ],
  469. done
  470. )
  471. })
  472. })
  473. })
  474. describe('clicking the invite link', function () {
  475. beforeEach(function (done) {
  476. createProjectAndInvite(
  477. this.sendingUser,
  478. this.projectName,
  479. this.email,
  480. (err, project, invite, link) => {
  481. expect(err).not.to.exist
  482. this.projectId = project._id
  483. this.fakeProject = project
  484. this.invite = invite
  485. this.link = link
  486. done()
  487. }
  488. )
  489. })
  490. describe('user is logged in already', function () {
  491. beforeEach(function (done) {
  492. this.user.login(done)
  493. })
  494. describe('user is already a member of the project', function () {
  495. beforeEach(function (done) {
  496. Async.series(
  497. [
  498. cb => expectInvitePage(this.user, this.link, cb),
  499. cb => expectAcceptInviteAndRedirect(this.user, this.invite, cb),
  500. cb => expectProjectAccess(this.user, this.invite.projectId, cb),
  501. ],
  502. done
  503. )
  504. })
  505. describe('when user clicks on the invite a second time', function () {
  506. it('should just redirect to the project page', function (done) {
  507. Async.series(
  508. [
  509. cb => expectProjectAccess(this.user, this.invite.projectId, cb),
  510. cb =>
  511. expectInviteRedirectToProject(
  512. this.user,
  513. this.link,
  514. this.invite,
  515. cb
  516. ),
  517. cb => expectProjectAccess(this.user, this.invite.projectId, cb),
  518. ],
  519. done
  520. )
  521. })
  522. describe('when the user recieves another invite to the same project', function () {
  523. it('should redirect to the project page', function (done) {
  524. Async.series(
  525. [
  526. cb => {
  527. createInvite(
  528. this.sendingUser,
  529. this.projectId,
  530. this.email,
  531. (err, invite) => {
  532. if (err) {
  533. throw err
  534. }
  535. this.secondInvite = invite
  536. this.secondLink =
  537. CollaboratorsEmailHandler._buildInviteUrl(
  538. this.fakeProject,
  539. invite
  540. )
  541. cb()
  542. }
  543. )
  544. },
  545. cb =>
  546. expectInviteRedirectToProject(
  547. this.user,
  548. this.secondLink,
  549. this.secondInvite,
  550. cb
  551. ),
  552. cb =>
  553. expectProjectAccess(this.user, this.invite.projectId, cb),
  554. cb =>
  555. revokeInvite(
  556. this.sendingUser,
  557. this.projectId,
  558. this.secondInvite._id,
  559. cb
  560. ),
  561. ],
  562. done
  563. )
  564. })
  565. })
  566. })
  567. })
  568. describe('user is not a member of the project', function () {
  569. it('should not grant access if the user does not accept the invite', function (done) {
  570. Async.series(
  571. [
  572. cb => expectInvitePage(this.user, this.link, cb),
  573. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  574. ],
  575. done
  576. )
  577. })
  578. it('should render the invalid-invite page if the token is invalid', function (done) {
  579. Async.series(
  580. [
  581. cb => {
  582. const link = this.link.replace(
  583. this.invite.token,
  584. 'not_a_real_token'
  585. )
  586. expectInvalidInvitePage(this.user, link, cb)
  587. },
  588. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  589. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  590. ],
  591. done
  592. )
  593. })
  594. it('should allow the user to accept the invite and access the project', function (done) {
  595. Async.series(
  596. [
  597. cb => expectInvitePage(this.user, this.link, cb),
  598. cb => expectAcceptInviteAndRedirect(this.user, this.invite, cb),
  599. cb => expectProjectAccess(this.user, this.invite.projectId, cb),
  600. ],
  601. done
  602. )
  603. })
  604. })
  605. })
  606. describe('user is not logged in initially', function () {
  607. describe('registration prompt workflow with valid token', function () {
  608. before(function () {
  609. if (!Features.hasFeature('registration')) {
  610. this.skip()
  611. }
  612. })
  613. it('should redirect to the register page', function (done) {
  614. expectInviteRedirectToRegister(this.user, this.link, done)
  615. })
  616. it('should allow user to accept the invite if the user registers a new account', function (done) {
  617. Async.series(
  618. [
  619. cb => expectInviteRedirectToRegister(this.user, this.link, cb),
  620. cb =>
  621. expectRegistrationRedirectToInvite(
  622. this.user,
  623. 'some_email@example.com',
  624. this.link,
  625. cb
  626. ),
  627. cb => expectInvitePage(this.user, this.link, cb),
  628. cb => expectAcceptInviteAndRedirect(this.user, this.invite, cb),
  629. cb => expectProjectAccess(this.user, this.invite.projectId, cb),
  630. ],
  631. done
  632. )
  633. })
  634. })
  635. describe('registration prompt workflow with non-valid token', function () {
  636. before(function () {
  637. if (!Features.hasFeature('registration')) {
  638. this.skip()
  639. }
  640. })
  641. it('should redirect to the register page', function (done) {
  642. Async.series(
  643. [
  644. cb => expectInviteRedirectToRegister(this.user, this.link, cb),
  645. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  646. ],
  647. done
  648. )
  649. })
  650. it('should display invalid-invite if the user registers a new account', function (done) {
  651. const badLink = this.link.replace(
  652. this.invite.token,
  653. 'not_a_real_token'
  654. )
  655. Async.series(
  656. [
  657. cb => expectInviteRedirectToRegister(this.user, badLink, cb),
  658. cb =>
  659. expectRegistrationRedirectToInvite(
  660. this.user,
  661. 'some_email@example.com',
  662. badLink,
  663. cb
  664. ),
  665. cb => expectInvalidInvitePage(this.user, badLink, cb),
  666. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  667. ],
  668. done
  669. )
  670. })
  671. })
  672. describe('login workflow with valid token', function () {
  673. it('should redirect to the register page', function (done) {
  674. Async.series(
  675. [
  676. cb => expectInviteRedirectToRegister(this.user, this.link, cb),
  677. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  678. ],
  679. done
  680. )
  681. })
  682. it('should allow the user to login to view the invite', function (done) {
  683. Async.series(
  684. [
  685. cb => expectInviteRedirectToRegister(this.user, this.link, cb),
  686. cb => expectLoginPage(this.user, cb),
  687. cb => expectLoginRedirectToInvite(this.user, this.link, cb),
  688. cb => expectInvitePage(this.user, this.link, cb),
  689. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  690. ],
  691. done
  692. )
  693. })
  694. it('should allow user to accept the invite if the user logs in', function (done) {
  695. Async.series(
  696. [
  697. cb => expectInviteRedirectToRegister(this.user, this.link, cb),
  698. cb => expectLoginPage(this.user, cb),
  699. cb => expectLoginRedirectToInvite(this.user, this.link, cb),
  700. cb => expectInvitePage(this.user, this.link, cb),
  701. cb => expectAcceptInviteAndRedirect(this.user, this.invite, cb),
  702. cb => expectProjectAccess(this.user, this.invite.projectId, cb),
  703. ],
  704. done
  705. )
  706. })
  707. })
  708. describe('login workflow with non-valid token', function () {
  709. it('should redirect to the register page', function (done) {
  710. Async.series(
  711. [
  712. cb => expectInviteRedirectToRegister(this.user, this.link, cb),
  713. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  714. ],
  715. done
  716. )
  717. })
  718. it('should show the invalid-invite page once the user has logged in', function (done) {
  719. const badLink = this.link.replace(
  720. this.invite.token,
  721. 'not_a_real_token'
  722. )
  723. Async.series(
  724. [
  725. cb => {
  726. return expectInviteRedirectToRegister(this.user, badLink, cb)
  727. },
  728. cb => {
  729. return expectLoginPage(this.user, cb)
  730. },
  731. cb => expectLoginRedirectToInvite(this.user, badLink, cb),
  732. cb => expectInvalidInvitePage(this.user, badLink, cb),
  733. cb => expectNoProjectAccess(this.user, this.invite.projectId, cb),
  734. ],
  735. done
  736. )
  737. })
  738. })
  739. })
  740. })
  741. })