TokenAccessTests.js 41 KB

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