TokenAccessTests.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. const { expect } = require('chai')
  2. const async = require('async')
  3. const User = require('./helpers/User')
  4. const request = require('./helpers/request')
  5. const settings = require('@overleaf/settings')
  6. const { db } = require('../../../app/src/infrastructure/mongodb')
  7. const expectErrorResponse = require('./helpers/expectErrorResponse')
  8. const tryEditorAccess = (user, projectId, test, callback) =>
  9. async.series(
  10. [
  11. cb =>
  12. user.request.get(`/project/${projectId}`, (error, response, body) => {
  13. if (error != null) {
  14. return cb(error)
  15. }
  16. test(response, body)
  17. cb()
  18. }),
  19. cb =>
  20. user.request.get(
  21. `/project/${projectId}/download/zip`,
  22. (error, response, body) => {
  23. if (error != null) {
  24. return cb(error)
  25. }
  26. test(response, body)
  27. cb()
  28. }
  29. ),
  30. ],
  31. callback
  32. )
  33. const tryReadOnlyTokenAccess = (
  34. user,
  35. token,
  36. testPageLoad,
  37. testFormPost,
  38. callback
  39. ) => {
  40. _doTryTokenAccess(
  41. `/read/${token}`,
  42. user,
  43. token,
  44. testPageLoad,
  45. testFormPost,
  46. callback
  47. )
  48. }
  49. const tryReadAndWriteTokenAccess = (
  50. user,
  51. token,
  52. testPageLoad,
  53. testFormPost,
  54. callback
  55. ) => {
  56. _doTryTokenAccess(
  57. `/${token}`,
  58. user,
  59. token,
  60. testPageLoad,
  61. testFormPost,
  62. callback
  63. )
  64. }
  65. const _doTryTokenAccess = (
  66. url,
  67. user,
  68. token,
  69. testPageLoad,
  70. testFormPost,
  71. callback
  72. ) => {
  73. user.request.get(url, (err, response, body) => {
  74. if (err) {
  75. return callback(err)
  76. }
  77. testPageLoad(response, body)
  78. if (!testFormPost) {
  79. return callback()
  80. }
  81. user.request.post(
  82. `${url}/grant`,
  83. { json: { token } },
  84. (err, response, body) => {
  85. if (err) {
  86. return callback(err)
  87. }
  88. testFormPost(response, body)
  89. callback()
  90. }
  91. )
  92. })
  93. }
  94. const tryReadOnlyTokenAccept = (
  95. user,
  96. token,
  97. testPageLoad,
  98. testFormPost,
  99. callback
  100. ) => {
  101. _doTryTokenAccept(
  102. `/read/${token}`,
  103. user,
  104. token,
  105. testPageLoad,
  106. testFormPost,
  107. callback
  108. )
  109. }
  110. const tryReadAndWriteTokenAccept = (
  111. user,
  112. token,
  113. testPageLoad,
  114. testFormPost,
  115. callback
  116. ) => {
  117. _doTryTokenAccept(
  118. `/${token}`,
  119. user,
  120. token,
  121. testPageLoad,
  122. testFormPost,
  123. callback
  124. )
  125. }
  126. const _doTryTokenAccept = (
  127. url,
  128. user,
  129. token,
  130. testPageLoad,
  131. testFormPost,
  132. callback
  133. ) => {
  134. user.request.get(url, (err, response, body) => {
  135. if (err) {
  136. return callback(err)
  137. }
  138. testPageLoad(response, body)
  139. if (!testFormPost) {
  140. return callback()
  141. }
  142. user.request.post(
  143. `${url}/grant`,
  144. { json: { token, confirmedByUser: true } },
  145. (err, response, body) => {
  146. if (err) {
  147. return callback(err)
  148. }
  149. testFormPost(response, body)
  150. callback()
  151. }
  152. )
  153. })
  154. }
  155. const tryContentAccess = (user, projcetId, test, callback) => {
  156. // The real-time service calls this end point to determine the user's
  157. // permissions.
  158. let userId
  159. if (user.id != null) {
  160. userId = user.id
  161. } else {
  162. userId = 'anonymous-user'
  163. }
  164. request.post(
  165. {
  166. url: `/project/${projcetId}/join`,
  167. qs: { user_id: userId },
  168. auth: {
  169. user: settings.apis.web.user,
  170. pass: settings.apis.web.pass,
  171. sendImmediately: true,
  172. },
  173. json: true,
  174. jar: false,
  175. },
  176. (error, response, body) => {
  177. if (error != null) {
  178. return callback(error)
  179. }
  180. test(response, body)
  181. callback()
  182. }
  183. )
  184. }
  185. const tryAnonContentAccess = (user, projectId, token, test, callback) => {
  186. // The real-time service calls this end point to determine the user's
  187. // permissions.
  188. let userId
  189. if (user.id != null) {
  190. userId = user.id
  191. } else {
  192. userId = 'anonymous-user'
  193. }
  194. request.post(
  195. {
  196. url: `/project/${projectId}/join`,
  197. qs: { user_id: userId },
  198. auth: {
  199. user: settings.apis.web.user,
  200. pass: settings.apis.web.pass,
  201. sendImmediately: true,
  202. },
  203. headers: {
  204. 'x-sl-anonymous-access-token': token,
  205. },
  206. json: true,
  207. jar: false,
  208. },
  209. (error, response, body) => {
  210. if (error != null) {
  211. return callback(error)
  212. }
  213. test(response, body)
  214. callback()
  215. }
  216. )
  217. }
  218. describe('TokenAccess', function () {
  219. beforeEach(function (done) {
  220. this.timeout(90000)
  221. this.owner = new User()
  222. this.other1 = new User()
  223. this.other2 = new User()
  224. this.anon = new User()
  225. this.siteAdmin = new User({ email: 'admin@example.com' })
  226. async.parallel(
  227. [
  228. cb =>
  229. this.siteAdmin.login(err => {
  230. if (err) {
  231. return cb(err)
  232. }
  233. this.siteAdmin.ensureAdmin(cb)
  234. }),
  235. cb => this.owner.login(cb),
  236. cb => this.other1.login(cb),
  237. cb => this.other2.login(cb),
  238. cb => this.anon.getCsrfToken(cb),
  239. ],
  240. done
  241. )
  242. })
  243. describe('no token-access', function () {
  244. beforeEach(function (done) {
  245. this.owner.createProject(
  246. `token-ro-test${Math.random()}`,
  247. (err, projectId) => {
  248. if (err != null) {
  249. return done(err)
  250. }
  251. this.projectId = projectId
  252. // Note, never made token-based,
  253. // thus no tokens
  254. done()
  255. }
  256. )
  257. })
  258. it('should deny access ', function (done) {
  259. async.series(
  260. [
  261. cb => {
  262. tryEditorAccess(
  263. this.other1,
  264. this.projectId,
  265. expectErrorResponse.restricted.html,
  266. cb
  267. )
  268. },
  269. cb => {
  270. tryContentAccess(
  271. this.other1,
  272. this.projectId,
  273. (response, body) => {
  274. expect(response.statusCode).to.equal(403)
  275. expect(body).to.equal('Forbidden')
  276. },
  277. cb
  278. )
  279. },
  280. ],
  281. done
  282. )
  283. })
  284. })
  285. describe('read-only token', function () {
  286. beforeEach(function (done) {
  287. this.projectName = `token-ro-test${Math.random()}`
  288. this.owner.createProject(this.projectName, (err, projectId) => {
  289. if (err != null) {
  290. return done(err)
  291. }
  292. this.projectId = projectId
  293. this.owner.makeTokenBased(this.projectId, err => {
  294. if (err != null) {
  295. return done(err)
  296. }
  297. this.owner.getProject(this.projectId, (err, project) => {
  298. if (err != null) {
  299. return done(err)
  300. }
  301. this.tokens = project.tokens
  302. done()
  303. })
  304. })
  305. })
  306. })
  307. it('allow the user read-only access to the project', function (done) {
  308. async.series(
  309. [
  310. cb => {
  311. // deny access before token is used
  312. tryEditorAccess(
  313. this.other1,
  314. this.projectId,
  315. expectErrorResponse.restricted.html,
  316. cb
  317. )
  318. },
  319. cb => {
  320. // try token
  321. tryReadOnlyTokenAccess(
  322. this.other1,
  323. this.tokens.readOnly,
  324. (response, body) => {
  325. expect(response.statusCode).to.equal(200)
  326. },
  327. (response, body) => {
  328. expect(response.statusCode).to.equal(200)
  329. expect(body.requireAccept.projectName).to.equal(
  330. this.projectName
  331. )
  332. },
  333. cb
  334. )
  335. },
  336. cb => {
  337. // deny access before token is accepted
  338. tryEditorAccess(
  339. this.other1,
  340. this.projectId,
  341. expectErrorResponse.restricted.html,
  342. cb
  343. )
  344. },
  345. cb => {
  346. // accept token
  347. tryReadOnlyTokenAccept(
  348. this.other1,
  349. this.tokens.readOnly,
  350. (response, body) => {
  351. expect(response.statusCode).to.equal(200)
  352. },
  353. (response, body) => {
  354. expect(response.statusCode).to.equal(200)
  355. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  356. expect(body.tokenAccessGranted).to.equal('readOnly')
  357. },
  358. cb
  359. )
  360. },
  361. cb => {
  362. // allow content access read-only
  363. tryContentAccess(
  364. this.other1,
  365. this.projectId,
  366. (response, body) => {
  367. expect(body.privilegeLevel).to.equal('readOnly')
  368. expect(body.isRestrictedUser).to.equal(true)
  369. expect(body.isTokenMember).to.equal(true)
  370. expect(body.isInvitedMember).to.equal(false)
  371. expect(body.project.owner).to.have.keys('_id')
  372. expect(body.project.owner).to.not.have.any.keys(
  373. 'email',
  374. 'first_name',
  375. 'last_name'
  376. )
  377. },
  378. cb
  379. )
  380. },
  381. cb => {
  382. tryEditorAccess(
  383. this.other1,
  384. this.projectId,
  385. (response, body) => {
  386. expect(response.statusCode).to.equal(200)
  387. },
  388. cb
  389. )
  390. },
  391. ],
  392. done
  393. )
  394. })
  395. it('should redirect the admin to the project (with rw access)', function (done) {
  396. async.series(
  397. [
  398. cb => {
  399. // use token
  400. tryReadOnlyTokenAccess(
  401. this.siteAdmin,
  402. this.tokens.readOnly,
  403. (response, body) => {
  404. expect(response.statusCode).to.equal(200)
  405. },
  406. (response, body) => {
  407. expect(response.statusCode).to.equal(200)
  408. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  409. },
  410. cb
  411. )
  412. },
  413. cb => {
  414. // allow content access read-and-write
  415. tryContentAccess(
  416. this.siteAdmin,
  417. this.projectId,
  418. (response, body) => {
  419. expect(body.privilegeLevel).to.equal('owner')
  420. expect(body.isRestrictedUser).to.equal(false)
  421. expect(body.isTokenMember).to.equal(false)
  422. expect(body.isInvitedMember).to.equal(false)
  423. },
  424. cb
  425. )
  426. },
  427. ],
  428. done
  429. )
  430. })
  431. describe('made private again', function () {
  432. beforeEach(function (done) {
  433. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  434. })
  435. it('should not allow the user to access the project', function (done) {
  436. async.series(
  437. [
  438. // no access before token is used
  439. cb =>
  440. tryEditorAccess(
  441. this.other1,
  442. this.projectId,
  443. expectErrorResponse.restricted.html,
  444. cb
  445. ),
  446. // token goes nowhere
  447. cb =>
  448. tryReadOnlyTokenAccess(
  449. this.other1,
  450. this.tokens.readOnly,
  451. (response, body) => {
  452. expect(response.statusCode).to.equal(200)
  453. },
  454. (response, body) => {
  455. expect(response.statusCode).to.equal(404)
  456. },
  457. cb
  458. ),
  459. // still no access
  460. cb =>
  461. tryEditorAccess(
  462. this.other1,
  463. this.projectId,
  464. expectErrorResponse.restricted.html,
  465. cb
  466. ),
  467. cb =>
  468. tryContentAccess(
  469. this.other1,
  470. this.projectId,
  471. (response, body) => {
  472. expect(response.statusCode).to.equal(403)
  473. expect(body).to.equal('Forbidden')
  474. },
  475. cb
  476. ),
  477. ],
  478. done
  479. )
  480. })
  481. })
  482. })
  483. describe('anonymous read-only token', function () {
  484. beforeEach(function (done) {
  485. this.owner.createProject(
  486. `token-anon-ro-test${Math.random()}`,
  487. (err, projectId) => {
  488. if (err != null) {
  489. return done(err)
  490. }
  491. this.projectId = projectId
  492. this.owner.makeTokenBased(this.projectId, err => {
  493. if (err != null) {
  494. return done(err)
  495. }
  496. this.owner.getProject(this.projectId, (err, project) => {
  497. if (err != null) {
  498. return done(err)
  499. }
  500. this.tokens = project.tokens
  501. done()
  502. })
  503. })
  504. }
  505. )
  506. })
  507. it('should allow the user to access project via read-only token url', function (done) {
  508. async.series(
  509. [
  510. cb =>
  511. tryEditorAccess(
  512. this.anon,
  513. this.projectId,
  514. expectErrorResponse.restricted.html,
  515. cb
  516. ),
  517. cb =>
  518. tryReadOnlyTokenAccess(
  519. this.anon,
  520. this.tokens.readOnly,
  521. (response, body) => {
  522. expect(response.statusCode).to.equal(200)
  523. },
  524. (response, body) => {
  525. expect(response.statusCode).to.equal(200)
  526. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  527. expect(body.grantAnonymousAccess).to.equal('readOnly')
  528. },
  529. cb
  530. ),
  531. cb =>
  532. tryEditorAccess(
  533. this.anon,
  534. this.projectId,
  535. (response, body) => {
  536. expect(response.statusCode).to.equal(200)
  537. },
  538. cb
  539. ),
  540. cb =>
  541. tryAnonContentAccess(
  542. this.anon,
  543. this.projectId,
  544. this.tokens.readOnly,
  545. (response, body) => {
  546. expect(body.privilegeLevel).to.equal('readOnly')
  547. expect(body.isRestrictedUser).to.equal(true)
  548. expect(body.isTokenMember).to.equal(false)
  549. expect(body.isInvitedMember).to.equal(false)
  550. expect(body.project.owner).to.have.keys('_id')
  551. expect(body.project.owner).to.not.have.any.keys(
  552. 'email',
  553. 'first_name',
  554. 'last_name'
  555. )
  556. },
  557. cb
  558. ),
  559. ],
  560. done
  561. )
  562. })
  563. describe('made private again', function () {
  564. beforeEach(function (done) {
  565. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  566. })
  567. it('should deny access to project', function (done) {
  568. async.series(
  569. [
  570. cb =>
  571. tryEditorAccess(
  572. this.anon,
  573. this.projectId,
  574. expectErrorResponse.restricted.html,
  575. cb
  576. ),
  577. // should not allow the user to access read-only token
  578. cb =>
  579. tryReadOnlyTokenAccess(
  580. this.anon,
  581. this.tokens.readOnly,
  582. (response, body) => {
  583. expect(response.statusCode).to.equal(200)
  584. },
  585. (response, body) => {
  586. expect(response.statusCode).to.equal(404)
  587. },
  588. cb
  589. ),
  590. // still no access
  591. cb =>
  592. tryEditorAccess(
  593. this.anon,
  594. this.projectId,
  595. expectErrorResponse.restricted.html,
  596. cb
  597. ),
  598. // should not allow the user to join the project
  599. cb =>
  600. tryAnonContentAccess(
  601. this.anon,
  602. this.projectId,
  603. this.tokens.readOnly,
  604. (response, body) => {
  605. expect(response.statusCode).to.equal(403)
  606. expect(body).to.equal('Forbidden')
  607. },
  608. cb
  609. ),
  610. ],
  611. done
  612. )
  613. })
  614. })
  615. })
  616. describe('read-and-write token', function () {
  617. beforeEach(function (done) {
  618. this.projectName = `token-rw-test${Math.random()}`
  619. this.owner.createProject(this.projectName, (err, projectId) => {
  620. if (err != null) {
  621. return done(err)
  622. }
  623. this.projectId = projectId
  624. this.owner.makeTokenBased(this.projectId, err => {
  625. if (err != null) {
  626. return done(err)
  627. }
  628. this.owner.getProject(this.projectId, (err, project) => {
  629. if (err != null) {
  630. return done(err)
  631. }
  632. this.tokens = project.tokens
  633. done()
  634. })
  635. })
  636. })
  637. })
  638. it('should allow the user to access project via read-and-write token url', function (done) {
  639. async.series(
  640. [
  641. // deny access before the token is used
  642. cb =>
  643. tryEditorAccess(
  644. this.other1,
  645. this.projectId,
  646. expectErrorResponse.restricted.html,
  647. cb
  648. ),
  649. // try token
  650. cb =>
  651. tryReadAndWriteTokenAccess(
  652. this.other1,
  653. this.tokens.readAndWrite,
  654. (response, body) => {
  655. expect(response.statusCode).to.equal(200)
  656. },
  657. (response, body) => {
  658. expect(response.statusCode).to.equal(200)
  659. expect(body.requireAccept.projectName).to.equal(
  660. this.projectName
  661. )
  662. },
  663. cb
  664. ),
  665. // deny access before the token is accepted
  666. cb =>
  667. tryEditorAccess(
  668. this.other1,
  669. this.projectId,
  670. expectErrorResponse.restricted.html,
  671. cb
  672. ),
  673. // accept token
  674. cb =>
  675. tryReadAndWriteTokenAccept(
  676. this.other1,
  677. this.tokens.readAndWrite,
  678. (response, body) => {
  679. expect(response.statusCode).to.equal(200)
  680. },
  681. (response, body) => {
  682. expect(response.statusCode).to.equal(200)
  683. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  684. expect(body.tokenAccessGranted).to.equal('readAndWrite')
  685. },
  686. cb
  687. ),
  688. cb =>
  689. tryEditorAccess(
  690. this.other1,
  691. this.projectId,
  692. (response, body) => {
  693. expect(response.statusCode).to.equal(200)
  694. },
  695. cb
  696. ),
  697. cb =>
  698. tryContentAccess(
  699. this.other1,
  700. this.projectId,
  701. (response, body) => {
  702. expect(body.privilegeLevel).to.equal('readAndWrite')
  703. expect(body.isRestrictedUser).to.equal(false)
  704. expect(body.isTokenMember).to.equal(true)
  705. expect(body.isInvitedMember).to.equal(false)
  706. expect(body.project.owner).to.have.all.keys(
  707. '_id',
  708. 'email',
  709. 'first_name',
  710. 'last_name',
  711. 'privileges',
  712. 'signUpDate'
  713. )
  714. },
  715. cb
  716. ),
  717. ],
  718. done
  719. )
  720. })
  721. describe('upgrading from a read-only token', function () {
  722. beforeEach(function (done) {
  723. this.owner.createProject(
  724. `token-rw-upgrade-test${Math.random()}`,
  725. (err, projectId) => {
  726. if (err != null) {
  727. return done(err)
  728. }
  729. this.projectId = projectId
  730. this.owner.makeTokenBased(this.projectId, err => {
  731. if (err != null) {
  732. return done(err)
  733. }
  734. this.owner.getProject(this.projectId, (err, project) => {
  735. if (err != null) {
  736. return done(err)
  737. }
  738. this.tokens = project.tokens
  739. done()
  740. })
  741. })
  742. }
  743. )
  744. })
  745. it('should allow user to access project via read-only, then upgrade to read-write', function (done) {
  746. async.series(
  747. [
  748. // deny access before the token is used
  749. cb =>
  750. tryEditorAccess(
  751. this.other1,
  752. this.projectId,
  753. expectErrorResponse.restricted.html,
  754. cb
  755. ),
  756. cb => {
  757. // use read-only token
  758. tryReadOnlyTokenAccept(
  759. this.other1,
  760. this.tokens.readOnly,
  761. (response, body) => {
  762. expect(response.statusCode).to.equal(200)
  763. },
  764. (response, body) => {
  765. expect(response.statusCode).to.equal(200)
  766. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  767. expect(body.tokenAccessGranted).to.equal('readOnly')
  768. },
  769. cb
  770. )
  771. },
  772. cb => {
  773. tryEditorAccess(
  774. this.other1,
  775. this.projectId,
  776. (response, body) => {
  777. expect(response.statusCode).to.equal(200)
  778. },
  779. cb
  780. )
  781. },
  782. cb => {
  783. // allow content access read-only
  784. tryContentAccess(
  785. this.other1,
  786. this.projectId,
  787. (response, body) => {
  788. expect(body.privilegeLevel).to.equal('readOnly')
  789. expect(body.isRestrictedUser).to.equal(true)
  790. expect(body.isTokenMember).to.equal(true)
  791. expect(body.isInvitedMember).to.equal(false)
  792. expect(body.project.owner).to.have.keys('_id')
  793. expect(body.project.owner).to.not.have.any.keys(
  794. 'email',
  795. 'first_name',
  796. 'last_name'
  797. )
  798. },
  799. cb
  800. )
  801. },
  802. //
  803. // Then switch to read-write token
  804. //
  805. cb =>
  806. tryReadAndWriteTokenAccept(
  807. this.other1,
  808. this.tokens.readAndWrite,
  809. (response, body) => {
  810. expect(response.statusCode).to.equal(200)
  811. },
  812. (response, body) => {
  813. expect(response.statusCode).to.equal(200)
  814. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  815. expect(body.tokenAccessGranted).to.equal('readAndWrite')
  816. },
  817. cb
  818. ),
  819. cb =>
  820. tryEditorAccess(
  821. this.other1,
  822. this.projectId,
  823. (response, body) => {
  824. expect(response.statusCode).to.equal(200)
  825. },
  826. cb
  827. ),
  828. cb =>
  829. tryContentAccess(
  830. this.other1,
  831. this.projectId,
  832. (response, body) => {
  833. expect(body.privilegeLevel).to.equal('readAndWrite')
  834. expect(body.isRestrictedUser).to.equal(false)
  835. expect(body.isTokenMember).to.equal(true)
  836. expect(body.isInvitedMember).to.equal(false)
  837. expect(body.project.owner).to.have.all.keys(
  838. '_id',
  839. 'email',
  840. 'first_name',
  841. 'last_name',
  842. 'privileges',
  843. 'signUpDate'
  844. )
  845. },
  846. cb
  847. ),
  848. ],
  849. done
  850. )
  851. })
  852. })
  853. describe('made private again', function () {
  854. beforeEach(function (done) {
  855. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  856. })
  857. it('should deny access to project', function (done) {
  858. async.series(
  859. [
  860. cb => {
  861. tryEditorAccess(
  862. this.other1,
  863. this.projectId,
  864. (response, body) => {},
  865. cb
  866. )
  867. },
  868. cb => {
  869. tryReadAndWriteTokenAccess(
  870. this.other1,
  871. this.tokens.readAndWrite,
  872. (response, body) => {
  873. expect(response.statusCode).to.equal(200)
  874. },
  875. (response, body) => {
  876. expect(response.statusCode).to.equal(404)
  877. },
  878. cb
  879. )
  880. },
  881. cb => {
  882. tryEditorAccess(
  883. this.other1,
  884. this.projectId,
  885. expectErrorResponse.restricted.html,
  886. cb
  887. )
  888. },
  889. cb => {
  890. tryContentAccess(
  891. this.other1,
  892. this.projectId,
  893. (response, body) => {
  894. expect(response.statusCode).to.equal(403)
  895. expect(body).to.equal('Forbidden')
  896. },
  897. cb
  898. )
  899. },
  900. ],
  901. done
  902. )
  903. })
  904. })
  905. })
  906. if (!settings.allowAnonymousReadAndWriteSharing) {
  907. describe('anonymous read-and-write token, disabled', function () {
  908. beforeEach(function (done) {
  909. this.owner.createProject(
  910. `token-anon-rw-test${Math.random()}`,
  911. (err, projectId) => {
  912. if (err != null) {
  913. return done(err)
  914. }
  915. this.projectId = projectId
  916. this.owner.makeTokenBased(this.projectId, err => {
  917. if (err != null) {
  918. return done(err)
  919. }
  920. this.owner.getProject(this.projectId, (err, project) => {
  921. if (err != null) {
  922. return done(err)
  923. }
  924. this.tokens = project.tokens
  925. done()
  926. })
  927. })
  928. }
  929. )
  930. })
  931. it('should not allow the user to access read-and-write token', function (done) {
  932. async.series(
  933. [
  934. cb =>
  935. tryEditorAccess(
  936. this.anon,
  937. this.projectId,
  938. expectErrorResponse.restricted.html,
  939. cb
  940. ),
  941. cb =>
  942. tryReadAndWriteTokenAccess(
  943. this.anon,
  944. this.tokens.readAndWrite,
  945. (response, body) => {
  946. expect(response.statusCode).to.equal(200)
  947. },
  948. (response, body) => {
  949. expect(response.statusCode).to.equal(200)
  950. expect(body).to.deep.equal({
  951. redirect: '/restricted',
  952. anonWriteAccessDenied: true,
  953. })
  954. },
  955. cb
  956. ),
  957. cb =>
  958. tryAnonContentAccess(
  959. this.anon,
  960. this.projectId,
  961. this.tokens.readAndWrite,
  962. (response, body) => {
  963. expect(response.statusCode).to.equal(403)
  964. expect(body).to.equal('Forbidden')
  965. },
  966. cb
  967. ),
  968. cb =>
  969. this.anon.login((err, response, body) => {
  970. expect(err).to.not.exist
  971. expect(response.statusCode).to.equal(200)
  972. expect(body.redir).to.equal(`/${this.tokens.readAndWrite}`)
  973. cb()
  974. }),
  975. ],
  976. done
  977. )
  978. })
  979. it('should require login if project does not exist', function (done) {
  980. async.series(
  981. [
  982. // delete project
  983. cb => {
  984. this.owner.deleteProject(this.projectId, cb)
  985. },
  986. cb =>
  987. tryReadAndWriteTokenAccess(
  988. this.anon,
  989. this.tokens.readAndWrite,
  990. (response, body) => {
  991. expect(response.statusCode).to.equal(200)
  992. },
  993. (response, body) => {
  994. expect(response.statusCode).to.equal(200)
  995. expect(body).to.deep.equal({
  996. redirect: '/restricted',
  997. anonWriteAccessDenied: true,
  998. })
  999. },
  1000. cb
  1001. ),
  1002. cb =>
  1003. this.anon.login((err, response, body) => {
  1004. expect(err).to.not.exist
  1005. expect(response.statusCode).to.equal(200)
  1006. expect(body.redir).to.equal(`/${this.tokens.readAndWrite}`)
  1007. cb()
  1008. }),
  1009. ],
  1010. done
  1011. )
  1012. })
  1013. })
  1014. } else {
  1015. describe('anonymous read-and-write token, enabled', function () {
  1016. beforeEach(function (done) {
  1017. this.owner.createProject(
  1018. `token-anon-rw-test${Math.random()}`,
  1019. (err, projectId) => {
  1020. if (err != null) {
  1021. return done(err)
  1022. }
  1023. this.projectId = projectId
  1024. this.owner.makeTokenBased(this.projectId, err => {
  1025. if (err != null) {
  1026. return done(err)
  1027. }
  1028. this.owner.getProject(this.projectId, (err, project) => {
  1029. if (err != null) {
  1030. return done(err)
  1031. }
  1032. this.tokens = project.tokens
  1033. done()
  1034. })
  1035. })
  1036. }
  1037. )
  1038. })
  1039. it('should allow the user to access project via read-and-write token url', function (done) {
  1040. async.series(
  1041. [
  1042. cb =>
  1043. tryEditorAccess(
  1044. this.anon,
  1045. this.projectId,
  1046. expectErrorResponse.restricted.html,
  1047. cb
  1048. ),
  1049. cb =>
  1050. tryReadAndWriteTokenAccess(
  1051. this.anon,
  1052. this.tokens.readAndWrite,
  1053. (response, body) => {
  1054. expect(response.statusCode).to.equal(200)
  1055. },
  1056. (response, body) => {
  1057. expect(response.statusCode).to.equal(200)
  1058. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  1059. expect(body.grantAnonymousAccess).to.equal('readAndWrite')
  1060. },
  1061. cb
  1062. ),
  1063. cb =>
  1064. tryEditorAccess(
  1065. this.anon,
  1066. this.projectId,
  1067. (response, body) => {
  1068. expect(response.statusCode).to.equal(200)
  1069. },
  1070. cb
  1071. ),
  1072. cb =>
  1073. tryAnonContentAccess(
  1074. this.anon,
  1075. this.projectId,
  1076. this.tokens.readAndWrite,
  1077. (response, body) => {
  1078. expect(body.privilegeLevel).to.equal('readAndWrite')
  1079. },
  1080. cb
  1081. ),
  1082. ],
  1083. done
  1084. )
  1085. })
  1086. describe('made private again', function () {
  1087. beforeEach(function (done) {
  1088. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  1089. })
  1090. it('should not allow the user to access read-and-write token', function (done) {
  1091. async.series(
  1092. [
  1093. cb =>
  1094. tryEditorAccess(
  1095. this.anon,
  1096. this.projectId,
  1097. expectErrorResponse.restricted.html,
  1098. cb
  1099. ),
  1100. cb =>
  1101. tryReadAndWriteTokenAccess(
  1102. this.anon,
  1103. this.tokens.readAndWrite,
  1104. (response, body) => {
  1105. expect(response.statusCode).to.equal(200)
  1106. },
  1107. (response, body) => {
  1108. expect(response.statusCode).to.equal(404)
  1109. },
  1110. cb
  1111. ),
  1112. cb =>
  1113. tryEditorAccess(
  1114. this.anon,
  1115. this.projectId,
  1116. expectErrorResponse.restricted.html,
  1117. cb
  1118. ),
  1119. cb =>
  1120. tryAnonContentAccess(
  1121. this.anon,
  1122. this.projectId,
  1123. this.tokens.readAndWrite,
  1124. (response, body) => {
  1125. expect(response.statusCode).to.equal(403)
  1126. expect(body).to.equal('Forbidden')
  1127. },
  1128. cb
  1129. ),
  1130. ],
  1131. done
  1132. )
  1133. })
  1134. })
  1135. it('should 404 if project does not exist', function (done) {
  1136. async.series(
  1137. [
  1138. // delete project
  1139. cb => {
  1140. this.owner.deleteProject(this.projectId, cb)
  1141. },
  1142. cb =>
  1143. tryReadAndWriteTokenAccess(
  1144. this.anon,
  1145. this.tokens.readAndWrite,
  1146. (response, body) => {
  1147. expect(response.statusCode).to.equal(200)
  1148. },
  1149. (response, body) => {
  1150. expect(response.statusCode).to.equal(200)
  1151. expect(body).to.deep.equal({
  1152. v1Import: {
  1153. status: 'mustLogin',
  1154. },
  1155. })
  1156. },
  1157. cb
  1158. ),
  1159. ],
  1160. done
  1161. )
  1162. })
  1163. })
  1164. }
  1165. describe('private overleaf project', function () {
  1166. beforeEach(function (done) {
  1167. this.owner.createProject('overleaf-import', (err, projectId) => {
  1168. expect(err).not.to.exist
  1169. this.projectId = projectId
  1170. this.owner.makeTokenBased(this.projectId, err => {
  1171. expect(err).not.to.exist
  1172. this.owner.getProject(this.projectId, (err, project) => {
  1173. expect(err).not.to.exist
  1174. this.tokens = project.tokens
  1175. this.owner.makePrivate(this.projectId, () => {
  1176. db.projects.updateOne(
  1177. { _id: project._id },
  1178. {
  1179. $set: {
  1180. overleaf: { id: 1234 },
  1181. },
  1182. },
  1183. err => {
  1184. expect(err).not.to.exist
  1185. done()
  1186. }
  1187. )
  1188. })
  1189. })
  1190. })
  1191. })
  1192. })
  1193. it('should only allow the owner access to the project', function (done) {
  1194. async.series(
  1195. [
  1196. // should redirect to canonical path, when owner uses read-write token
  1197. cb =>
  1198. tryReadAndWriteTokenAccess(
  1199. this.owner,
  1200. this.tokens.readAndWrite,
  1201. (response, body) => {
  1202. expect(response.statusCode).to.equal(200)
  1203. },
  1204. (response, body) => {
  1205. expect(response.statusCode).to.equal(200)
  1206. expect(response.body.redirect).to.equal(
  1207. `/project/${this.projectId}`
  1208. )
  1209. expect(response.body.higherAccess).to.equal(true)
  1210. },
  1211. cb
  1212. ),
  1213. cb =>
  1214. tryEditorAccess(
  1215. this.owner,
  1216. this.projectId,
  1217. (response, body) => {
  1218. expect(response.statusCode).to.equal(200)
  1219. },
  1220. cb
  1221. ),
  1222. cb =>
  1223. tryContentAccess(
  1224. this.owner,
  1225. this.projectId,
  1226. (response, body) => {
  1227. expect(body.privilegeLevel).to.equal('owner')
  1228. },
  1229. cb
  1230. ),
  1231. // non-owner should be denied access
  1232. cb =>
  1233. tryContentAccess(
  1234. this.other2,
  1235. this.projectId,
  1236. (response, body) => {
  1237. expect(response.statusCode).to.equal(403)
  1238. expect(body).to.equal('Forbidden')
  1239. },
  1240. cb
  1241. ),
  1242. ],
  1243. done
  1244. )
  1245. })
  1246. })
  1247. describe('private project, with higher access', function () {
  1248. beforeEach(function (done) {
  1249. this.owner.createProject(
  1250. `higher-access-test-${Math.random()}`,
  1251. (err, projectId) => {
  1252. expect(err).not.to.exist
  1253. this.projectId = projectId
  1254. this.owner.addUserToProject(
  1255. this.projectId,
  1256. this.other1,
  1257. 'readAndWrite',
  1258. err => {
  1259. expect(err).not.to.exist
  1260. this.owner.makeTokenBased(this.projectId, err => {
  1261. expect(err).not.to.exist
  1262. this.owner.getProject(this.projectId, (err, project) => {
  1263. expect(err).not.to.exist
  1264. this.tokens = project.tokens
  1265. this.owner.makePrivate(this.projectId, () => {
  1266. setTimeout(done, 1000)
  1267. })
  1268. })
  1269. })
  1270. }
  1271. )
  1272. }
  1273. )
  1274. })
  1275. it('should allow the user access to the project', function (done) {
  1276. async.series(
  1277. [
  1278. // should redirect to canonical path, when user uses read-write token
  1279. cb =>
  1280. tryReadAndWriteTokenAccess(
  1281. this.other1,
  1282. this.tokens.readAndWrite,
  1283. (response, body) => {
  1284. expect(response.statusCode).to.equal(200)
  1285. },
  1286. (response, body) => {
  1287. expect(response.statusCode).to.equal(200)
  1288. expect(response.body.redirect).to.equal(
  1289. `/project/${this.projectId}`
  1290. )
  1291. expect(response.body.higherAccess).to.equal(true)
  1292. },
  1293. cb
  1294. ),
  1295. // should redirect to canonical path, when user uses read-only token
  1296. cb =>
  1297. tryReadOnlyTokenAccess(
  1298. this.other1,
  1299. this.tokens.readOnly,
  1300. (response, body) => {
  1301. expect(response.statusCode).to.equal(200)
  1302. },
  1303. (response, body) => {
  1304. expect(response.statusCode).to.equal(200)
  1305. expect(response.body.redirect).to.equal(
  1306. `/project/${this.projectId}`
  1307. )
  1308. expect(response.body.higherAccess).to.equal(true)
  1309. },
  1310. cb
  1311. ),
  1312. // should allow the user access to the project
  1313. cb =>
  1314. tryEditorAccess(
  1315. this.other1,
  1316. this.projectId,
  1317. (response, body) => {
  1318. expect(response.statusCode).to.equal(200)
  1319. },
  1320. cb
  1321. ),
  1322. // should allow user to join the project
  1323. cb =>
  1324. tryContentAccess(
  1325. this.other1,
  1326. this.projectId,
  1327. (response, body) => {
  1328. expect(body.privilegeLevel).to.equal('readAndWrite')
  1329. expect(body.isRestrictedUser).to.equal(false)
  1330. expect(body.isTokenMember).to.equal(false)
  1331. expect(body.isInvitedMember).to.equal(true)
  1332. },
  1333. cb
  1334. ),
  1335. // should not allow a different user to join the project
  1336. cb =>
  1337. tryEditorAccess(
  1338. this.other2,
  1339. this.projectId,
  1340. expectErrorResponse.restricted.html,
  1341. cb
  1342. ),
  1343. cb =>
  1344. tryContentAccess(
  1345. this.other2,
  1346. this.projectId,
  1347. (response, body) => {
  1348. expect(response.statusCode).to.equal(403)
  1349. expect(body).to.equal('Forbidden')
  1350. },
  1351. cb
  1352. ),
  1353. ],
  1354. done
  1355. )
  1356. })
  1357. })
  1358. describe('deleted project', function () {
  1359. beforeEach(function (done) {
  1360. this.owner.createProject(
  1361. `delete-test${Math.random()}`,
  1362. (err, projectId) => {
  1363. if (err != null) {
  1364. return done(err)
  1365. }
  1366. this.projectId = projectId
  1367. this.owner.makeTokenBased(this.projectId, err => {
  1368. if (err != null) {
  1369. return done(err)
  1370. }
  1371. this.owner.getProject(this.projectId, (err, project) => {
  1372. if (err != null) {
  1373. return done(err)
  1374. }
  1375. this.tokens = project.tokens
  1376. done()
  1377. })
  1378. })
  1379. }
  1380. )
  1381. })
  1382. it('should 404', function (done) {
  1383. async.series(
  1384. [
  1385. // delete project
  1386. cb => {
  1387. this.owner.deleteProject(this.projectId, cb)
  1388. },
  1389. cb => {
  1390. // use read-only token
  1391. tryReadOnlyTokenAccess(
  1392. this.other1,
  1393. this.tokens.readOnly,
  1394. (response, body) => {
  1395. expect(response.statusCode).to.equal(200)
  1396. },
  1397. (response, body) => {
  1398. expect(response.statusCode).to.equal(404)
  1399. },
  1400. cb
  1401. )
  1402. },
  1403. cb => {
  1404. // use read-write token
  1405. tryReadAndWriteTokenAccess(
  1406. this.other1,
  1407. this.tokens.readAndWrite,
  1408. (response, body) => {
  1409. expect(response.statusCode).to.equal(200)
  1410. },
  1411. (response, body) => {
  1412. expect(response.statusCode).to.equal(404)
  1413. },
  1414. cb
  1415. )
  1416. },
  1417. ],
  1418. done
  1419. )
  1420. })
  1421. })
  1422. })