TokenAccessTests.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const { expect } = require('chai')
  16. const async = require('async')
  17. const MockV1Api = require('./helpers/MockV1Api')
  18. const User = require('./helpers/User')
  19. const request = require('./helpers/request')
  20. const settings = require('settings-sharelatex')
  21. const { db, ObjectId } = require('../../../app/src/infrastructure/mongojs')
  22. const try_read_access = (user, project_id, test, callback) =>
  23. async.series(
  24. [
  25. cb =>
  26. user.request.get(`/project/${project_id}`, function(
  27. error,
  28. response,
  29. body
  30. ) {
  31. if (error != null) {
  32. return cb(error)
  33. }
  34. test(response, body)
  35. return cb()
  36. }),
  37. cb =>
  38. user.request.get(`/project/${project_id}/download/zip`, function(
  39. error,
  40. response,
  41. body
  42. ) {
  43. if (error != null) {
  44. return cb(error)
  45. }
  46. test(response, body)
  47. return cb()
  48. })
  49. ],
  50. callback
  51. )
  52. const try_read_only_token_access = (user, token, test, callback) =>
  53. async.series(
  54. [
  55. cb =>
  56. user.request.get(`/read/${token}`, function(error, response, body) {
  57. if (error != null) {
  58. return cb(error)
  59. }
  60. test(response, body)
  61. return cb()
  62. })
  63. ],
  64. callback
  65. )
  66. const try_read_and_write_token_access = (user, token, test, callback) =>
  67. async.series(
  68. [
  69. cb =>
  70. user.request.get(`/${token}`, function(error, response, body) {
  71. if (error != null) {
  72. return cb(error)
  73. }
  74. test(response, body)
  75. return cb()
  76. })
  77. ],
  78. callback
  79. )
  80. const try_content_access = function(user, project_id, test, callback) {
  81. // The real-time service calls this end point to determine the user's
  82. // permissions.
  83. let user_id
  84. if (user.id != null) {
  85. user_id = user.id
  86. } else {
  87. user_id = 'anonymous-user'
  88. }
  89. return request.post(
  90. {
  91. url: `/project/${project_id}/join`,
  92. qs: { user_id },
  93. auth: {
  94. user: settings.apis.web.user,
  95. pass: settings.apis.web.pass,
  96. sendImmediately: true
  97. },
  98. json: true,
  99. jar: false
  100. },
  101. function(error, response, body) {
  102. if (error != null) {
  103. return callback(error)
  104. }
  105. test(response, body)
  106. return callback()
  107. }
  108. )
  109. }
  110. const try_anon_content_access = function(
  111. user,
  112. project_id,
  113. token,
  114. test,
  115. callback
  116. ) {
  117. // The real-time service calls this end point to determine the user's
  118. // permissions.
  119. let user_id
  120. if (user.id != null) {
  121. user_id = user.id
  122. } else {
  123. user_id = 'anonymous-user'
  124. }
  125. return request.post(
  126. {
  127. url: `/project/${project_id}/join`,
  128. qs: { user_id },
  129. auth: {
  130. user: settings.apis.web.user,
  131. pass: settings.apis.web.pass,
  132. sendImmediately: true
  133. },
  134. headers: {
  135. 'x-sl-anonymous-access-token': token
  136. },
  137. json: true,
  138. jar: false
  139. },
  140. function(error, response, body) {
  141. if (error != null) {
  142. return callback(error)
  143. }
  144. test(response, body)
  145. return callback()
  146. }
  147. )
  148. }
  149. describe('TokenAccess', function() {
  150. before(function(done) {
  151. this.timeout(90000)
  152. this.owner = new User()
  153. this.other1 = new User()
  154. this.other2 = new User()
  155. this.anon = new User()
  156. return async.parallel(
  157. [
  158. cb => this.owner.login(cb),
  159. cb => this.other1.login(cb),
  160. cb => this.other2.login(cb),
  161. cb => this.anon.getCsrfToken(cb)
  162. ],
  163. done
  164. )
  165. })
  166. describe('no token-access', function() {
  167. before(function(done) {
  168. return this.owner.createProject(
  169. `token-ro-test${Math.random()}`,
  170. (err, project_id) => {
  171. if (err != null) {
  172. return done(err)
  173. }
  174. this.project_id = project_id
  175. // Note, never made token-based,
  176. // thus no tokens
  177. return done()
  178. }
  179. )
  180. })
  181. it('should deny access ', function(done) {
  182. return try_read_access(
  183. this.other1,
  184. this.project_id,
  185. (response, body) => {
  186. expect(response.statusCode).to.equal(302)
  187. return expect(body).to.match(/.*\/restricted.*/)
  188. },
  189. done
  190. )
  191. })
  192. return it('should not allow the user to join the project', function(done) {
  193. return try_content_access(
  194. this.other1,
  195. this.project_id,
  196. (response, body) => {
  197. return expect(body.privilegeLevel).to.equal(false)
  198. },
  199. done
  200. )
  201. })
  202. })
  203. describe('read-only token', function() {
  204. before(function(done) {
  205. return this.owner.createProject(
  206. `token-ro-test${Math.random()}`,
  207. (err, project_id) => {
  208. if (err != null) {
  209. return done(err)
  210. }
  211. this.project_id = project_id
  212. return this.owner.makeTokenBased(this.project_id, err => {
  213. if (err != null) {
  214. return done(err)
  215. }
  216. return this.owner.getProject(this.project_id, (err, project) => {
  217. if (err != null) {
  218. return done(err)
  219. }
  220. this.tokens = project.tokens
  221. return done()
  222. })
  223. })
  224. }
  225. )
  226. })
  227. it('should deny access before the token is used', function(done) {
  228. return try_read_access(
  229. this.other1,
  230. this.project_id,
  231. (response, body) => {
  232. expect(response.statusCode).to.equal(302)
  233. return expect(body).to.match(/.*\/restricted.*/)
  234. },
  235. done
  236. )
  237. })
  238. it('should allow the user to access project via read-only token url', function(done) {
  239. return try_read_only_token_access(
  240. this.other1,
  241. this.tokens.readOnly,
  242. (response, body) => {
  243. return expect(response.statusCode).to.equal(200)
  244. },
  245. done
  246. )
  247. })
  248. it('should allow the user to join the project with read-only access', function(done) {
  249. return try_content_access(
  250. this.other1,
  251. this.project_id,
  252. (response, body) => {
  253. return expect(body.privilegeLevel).to.equal('readOnly')
  254. },
  255. done
  256. )
  257. })
  258. return describe('made private again', function() {
  259. before(function(done) {
  260. return this.owner.makePrivate(this.project_id, () =>
  261. setTimeout(done, 1000)
  262. )
  263. })
  264. it('should deny access to project', function(done) {
  265. return try_read_access(
  266. this.other1,
  267. this.project_id,
  268. (response, body) => {
  269. expect(response.statusCode).to.equal(302)
  270. return expect(body).to.match(/.*\/restricted.*/)
  271. },
  272. done
  273. )
  274. })
  275. it('should not allow the user to access read-only token', function(done) {
  276. return try_read_only_token_access(
  277. this.other1,
  278. this.tokens.readOnly,
  279. (response, body) => {
  280. return expect(response.statusCode).to.equal(404)
  281. },
  282. done
  283. )
  284. })
  285. return it('should not allow the user to join the project', function(done) {
  286. return try_content_access(
  287. this.other1,
  288. this.project_id,
  289. (response, body) => {
  290. return expect(body.privilegeLevel).to.equal(false)
  291. },
  292. done
  293. )
  294. })
  295. })
  296. })
  297. describe('anonymous read-only token', function() {
  298. before(function(done) {
  299. return this.owner.createProject(
  300. `token-anon-ro-test${Math.random()}`,
  301. (err, project_id) => {
  302. if (err != null) {
  303. return done(err)
  304. }
  305. this.project_id = project_id
  306. return this.owner.makeTokenBased(this.project_id, err => {
  307. if (err != null) {
  308. return done(err)
  309. }
  310. return this.owner.getProject(this.project_id, (err, project) => {
  311. if (err != null) {
  312. return done(err)
  313. }
  314. this.tokens = project.tokens
  315. return done()
  316. })
  317. })
  318. }
  319. )
  320. })
  321. it('should deny access before the token is used', function(done) {
  322. return try_read_access(
  323. this.anon,
  324. this.project_id,
  325. (response, body) => {
  326. expect(response.statusCode).to.equal(302)
  327. return expect(body).to.match(/.*\/restricted.*/)
  328. },
  329. done
  330. )
  331. })
  332. it('should allow the user to access project via read-only token url', function(done) {
  333. return try_read_only_token_access(
  334. this.anon,
  335. this.tokens.readOnly,
  336. (response, body) => {
  337. return expect(response.statusCode).to.equal(200)
  338. },
  339. done
  340. )
  341. })
  342. it('should allow the user to anonymously join the project with read-only access', function(done) {
  343. return try_anon_content_access(
  344. this.anon,
  345. this.project_id,
  346. this.tokens.readOnly,
  347. (response, body) => {
  348. return expect(body.privilegeLevel).to.equal('readOnly')
  349. },
  350. done
  351. )
  352. })
  353. return describe('made private again', function() {
  354. before(function(done) {
  355. return this.owner.makePrivate(this.project_id, () =>
  356. setTimeout(done, 1000)
  357. )
  358. })
  359. it('should deny access to project', function(done) {
  360. return try_read_access(
  361. this.anon,
  362. this.project_id,
  363. (response, body) => {
  364. expect(response.statusCode).to.equal(302)
  365. return expect(body).to.match(/.*\/restricted.*/)
  366. },
  367. done
  368. )
  369. })
  370. it('should not allow the user to access read-only token', function(done) {
  371. return try_read_only_token_access(
  372. this.anon,
  373. this.tokens.readOnly,
  374. (response, body) => {
  375. return expect(response.statusCode).to.equal(404)
  376. },
  377. done
  378. )
  379. })
  380. return it('should not allow the user to join the project', function(done) {
  381. return try_anon_content_access(
  382. this.anon,
  383. this.project_id,
  384. this.tokens.readOnly,
  385. (response, body) => {
  386. return expect(body.privilegeLevel).to.equal(false)
  387. },
  388. done
  389. )
  390. })
  391. })
  392. })
  393. describe('read-and-write token', function() {
  394. before(function(done) {
  395. return this.owner.createProject(
  396. `token-rw-test${Math.random()}`,
  397. (err, project_id) => {
  398. if (err != null) {
  399. return done(err)
  400. }
  401. this.project_id = project_id
  402. return this.owner.makeTokenBased(this.project_id, err => {
  403. if (err != null) {
  404. return done(err)
  405. }
  406. return this.owner.getProject(this.project_id, (err, project) => {
  407. if (err != null) {
  408. return done(err)
  409. }
  410. this.tokens = project.tokens
  411. return done()
  412. })
  413. })
  414. }
  415. )
  416. })
  417. it('should deny access before the token is used', function(done) {
  418. return try_read_access(
  419. this.other1,
  420. this.project_id,
  421. (response, body) => {
  422. expect(response.statusCode).to.equal(302)
  423. expect(response.headers.location).to.match(/\/restricted.*/)
  424. return expect(body).to.match(/.*\/restricted.*/)
  425. },
  426. done
  427. )
  428. })
  429. it('should allow the user to access project via read-and-write token url', function(done) {
  430. return try_read_and_write_token_access(
  431. this.other1,
  432. this.tokens.readAndWrite,
  433. (response, body) => {
  434. return expect(response.statusCode).to.equal(200)
  435. },
  436. done
  437. )
  438. })
  439. it('should allow the user to join the project with read-and-write access', function(done) {
  440. return try_content_access(
  441. this.other1,
  442. this.project_id,
  443. (response, body) => {
  444. return expect(body.privilegeLevel).to.equal('readAndWrite')
  445. },
  446. done
  447. )
  448. })
  449. return describe('made private again', function() {
  450. before(function(done) {
  451. return this.owner.makePrivate(this.project_id, () =>
  452. setTimeout(done, 1000)
  453. )
  454. })
  455. it('should deny access to project', function(done) {
  456. return try_read_access(
  457. this.other1,
  458. this.project_id,
  459. (response, body) => {
  460. expect(response.statusCode).to.equal(302)
  461. return expect(body).to.match(/.*\/restricted.*/)
  462. },
  463. done
  464. )
  465. })
  466. it('should not allow the user to access read-and-write token', function(done) {
  467. return try_read_and_write_token_access(
  468. this.other1,
  469. this.tokens.readAndWrite,
  470. (response, body) => {
  471. return expect(response.statusCode).to.equal(404)
  472. },
  473. done
  474. )
  475. })
  476. return it('should not allow the user to join the project', function(done) {
  477. return try_content_access(
  478. this.other1,
  479. this.project_id,
  480. (response, body) => {
  481. return expect(body.privilegeLevel).to.equal(false)
  482. },
  483. done
  484. )
  485. })
  486. })
  487. })
  488. if (!settings.allowAnonymousReadAndWriteSharing) {
  489. describe('anonymous read-and-write token, disabled', function() {
  490. before(function(done) {
  491. return this.owner.createProject(
  492. `token-anon-rw-test${Math.random()}`,
  493. (err, project_id) => {
  494. if (err != null) {
  495. return done(err)
  496. }
  497. this.project_id = project_id
  498. return this.owner.makeTokenBased(this.project_id, err => {
  499. if (err != null) {
  500. return done(err)
  501. }
  502. return this.owner.getProject(this.project_id, (err, project) => {
  503. if (err != null) {
  504. return done(err)
  505. }
  506. this.tokens = project.tokens
  507. return done()
  508. })
  509. })
  510. }
  511. )
  512. })
  513. it('should deny access before the token is used', function(done) {
  514. return try_read_access(
  515. this.anon,
  516. this.project_id,
  517. (response, body) => {
  518. expect(response.statusCode).to.equal(302)
  519. return expect(body).to.match(/.*\/restricted.*/)
  520. },
  521. done
  522. )
  523. })
  524. it('should not allow the user to access read-and-write token', function(done) {
  525. return try_read_and_write_token_access(
  526. this.anon,
  527. this.tokens.readAndWrite,
  528. (response, body) => {
  529. expect(response.statusCode).to.equal(302)
  530. return expect(body).to.match(/.*\/restricted.*/)
  531. },
  532. done
  533. )
  534. })
  535. return it('should not allow the user to join the project', function(done) {
  536. return try_anon_content_access(
  537. this.anon,
  538. this.project_id,
  539. this.tokens.readAndWrite,
  540. (response, body) => {
  541. return expect(body.privilegeLevel).to.equal(false)
  542. },
  543. done
  544. )
  545. })
  546. })
  547. } else {
  548. describe('anonymous read-and-write token, enabled', function() {
  549. before(function(done) {
  550. return this.owner.createProject(
  551. `token-anon-rw-test${Math.random()}`,
  552. (err, project_id) => {
  553. if (err != null) {
  554. return done(err)
  555. }
  556. this.project_id = project_id
  557. return this.owner.makeTokenBased(this.project_id, err => {
  558. if (err != null) {
  559. return done(err)
  560. }
  561. return this.owner.getProject(this.project_id, (err, project) => {
  562. if (err != null) {
  563. return done(err)
  564. }
  565. this.tokens = project.tokens
  566. return done()
  567. })
  568. })
  569. }
  570. )
  571. })
  572. it('should deny access before the token is used', function(done) {
  573. return try_read_access(
  574. this.anon,
  575. this.project_id,
  576. (response, body) => {
  577. expect(response.statusCode).to.equal(302)
  578. return expect(body).to.match(/.*\/restricted.*/)
  579. },
  580. done
  581. )
  582. })
  583. it('should allow the user to access project via read-and-write token url', function(done) {
  584. return try_read_and_write_token_access(
  585. this.anon,
  586. this.tokens.readAndWrite,
  587. (response, body) => {
  588. return expect(response.statusCode).to.equal(200)
  589. },
  590. done
  591. )
  592. })
  593. it('should allow the user to anonymously join the project with read-and-write access', function(done) {
  594. return try_anon_content_access(
  595. this.anon,
  596. this.project_id,
  597. this.tokens.readAndWrite,
  598. (response, body) => {
  599. return expect(body.privilegeLevel).to.equal('readAndWrite')
  600. },
  601. done
  602. )
  603. })
  604. return describe('made private again', function() {
  605. before(function(done) {
  606. return this.owner.makePrivate(this.project_id, () =>
  607. setTimeout(done, 1000)
  608. )
  609. })
  610. it('should deny access to project', function(done) {
  611. return try_read_access(
  612. this.anon,
  613. this.project_id,
  614. (response, body) => {
  615. expect(response.statusCode).to.equal(302)
  616. return expect(body).to.match(/.*\/restricted.*/)
  617. },
  618. done
  619. )
  620. })
  621. it('should not allow the user to access read-and-write token', function(done) {
  622. return try_read_and_write_token_access(
  623. this.anon,
  624. this.tokens.readAndWrite,
  625. (response, body) => {
  626. return expect(response.statusCode).to.equal(404)
  627. },
  628. done
  629. )
  630. })
  631. return it('should not allow the user to join the project', function(done) {
  632. return try_anon_content_access(
  633. this.anon,
  634. this.project_id,
  635. this.tokens.readAndWrite,
  636. (response, body) => {
  637. return expect(body.privilegeLevel).to.equal(false)
  638. },
  639. done
  640. )
  641. })
  642. })
  643. })
  644. }
  645. describe('private overleaf project', function() {
  646. before(function(done) {
  647. return this.owner.createProject('overleaf-import', (err, project_id) => {
  648. this.project_id = project_id
  649. return this.owner.makeTokenBased(this.project_id, err => {
  650. return this.owner.getProject(this.project_id, (err, project) => {
  651. this.tokens = project.tokens
  652. return this.owner.makePrivate(this.project_id, () => {
  653. return db.projects.update(
  654. { _id: project._id },
  655. {
  656. $set: {
  657. overleaf: { id: 1234 }
  658. }
  659. },
  660. err => {
  661. return done()
  662. }
  663. )
  664. })
  665. })
  666. })
  667. })
  668. })
  669. it('should redirect to canonical path, when owner uses read-write token', function(done) {
  670. return try_read_and_write_token_access(
  671. this.owner,
  672. this.tokens.readAndWrite,
  673. (response, body) => {
  674. expect(response.statusCode).to.equal(302)
  675. return expect(response.headers.location).to.equal(
  676. `/project/${this.project_id}`
  677. )
  678. },
  679. done
  680. )
  681. })
  682. it('should allow the owner access to the project', function(done) {
  683. return try_read_access(
  684. this.owner,
  685. this.project_id,
  686. (response, body) => {
  687. return expect(response.statusCode).to.equal(200)
  688. },
  689. done
  690. )
  691. })
  692. it('should allow owner to join the project', function(done) {
  693. return try_content_access(
  694. this.owner,
  695. this.project_id,
  696. (response, body) => {
  697. return expect(body.privilegeLevel).to.equal('owner')
  698. },
  699. done
  700. )
  701. })
  702. return it('should not allow other user to join the project', function(done) {
  703. return try_content_access(
  704. this.other2,
  705. this.project_id,
  706. (response, body) => {
  707. return expect(body.privilegeLevel).to.equal(false)
  708. },
  709. done
  710. )
  711. })
  712. })
  713. describe('private project, with higher access', function() {
  714. before(function(done) {
  715. return this.owner.createProject(
  716. `higher-access-test-${Math.random()}`,
  717. (err, project_id) => {
  718. this.project_id = project_id
  719. return this.owner.addUserToProject(
  720. this.project_id,
  721. this.other1,
  722. 'readAndWrite',
  723. err => {
  724. return this.owner.makeTokenBased(this.project_id, err => {
  725. return this.owner.getProject(
  726. this.project_id,
  727. (err, project) => {
  728. this.tokens = project.tokens
  729. return this.owner.makePrivate(this.project_id, () => {
  730. return setTimeout(done, 1000)
  731. })
  732. }
  733. )
  734. })
  735. }
  736. )
  737. }
  738. )
  739. })
  740. it('should redirect to canonical path, when user uses read-write token', function(done) {
  741. return try_read_and_write_token_access(
  742. this.other1,
  743. this.tokens.readAndWrite,
  744. (response, body) => {
  745. expect(response.statusCode).to.equal(302)
  746. return expect(response.headers.location).to.equal(
  747. `/project/${this.project_id}`
  748. )
  749. },
  750. done
  751. )
  752. })
  753. it('should redirect to canonical path, when user uses read-only token', function(done) {
  754. return try_read_only_token_access(
  755. this.other1,
  756. this.tokens.readOnly,
  757. (response, body) => {
  758. expect(response.statusCode).to.equal(302)
  759. return expect(response.headers.location).to.equal(
  760. `/project/${this.project_id}`
  761. )
  762. },
  763. done
  764. )
  765. })
  766. it('should allow the user access to the project', function(done) {
  767. return try_read_access(
  768. this.other1,
  769. this.project_id,
  770. (response, body) => {
  771. return expect(response.statusCode).to.equal(200)
  772. },
  773. done
  774. )
  775. })
  776. it('should allow user to join the project', function(done) {
  777. return try_content_access(
  778. this.other1,
  779. this.project_id,
  780. (response, body) => {
  781. return expect(body.privilegeLevel).to.equal('readAndWrite')
  782. },
  783. done
  784. )
  785. })
  786. return it('should not allow a different user to join the project', function(done) {
  787. return try_content_access(
  788. this.other2,
  789. this.project_id,
  790. (response, body) => {
  791. return expect(body.privilegeLevel).to.equal(false)
  792. },
  793. done
  794. )
  795. })
  796. })
  797. describe('unimported v1 project', function() {
  798. before(() => (settings.overleaf = { host: 'http://localhost:5000' }))
  799. after(() => delete settings.overleaf)
  800. it('should redirect read and write token to v1', function(done) {
  801. const unimportedV1Token = '123abc'
  802. return try_read_and_write_token_access(
  803. this.owner,
  804. unimportedV1Token,
  805. (response, body) => {
  806. expect(response.statusCode).to.equal(302)
  807. return expect(response.headers.location).to.equal(
  808. '/sign_in_to_v1?return_to=/123abc'
  809. )
  810. },
  811. done
  812. )
  813. })
  814. return it('should redirect read only token to v1', function(done) {
  815. const unimportedV1Token = 'abcd'
  816. return try_read_only_token_access(
  817. this.owner,
  818. unimportedV1Token,
  819. (response, body) => {
  820. expect(response.statusCode).to.equal(302)
  821. return expect(response.headers.location).to.equal(
  822. '/sign_in_to_v1?return_to=/read/abcd'
  823. )
  824. },
  825. done
  826. )
  827. })
  828. })
  829. return describe('importing v1 project', function() {
  830. before(function(done) {
  831. settings.projectImportingCheckMaxCreateDelta = 3600
  832. settings.overleaf = { host: 'http://localhost:5000' }
  833. return this.owner.createProject(
  834. `token-rw-test${Math.random()}`,
  835. (err, project_id) => {
  836. if (err != null) {
  837. return done(err)
  838. }
  839. this.project_id = project_id
  840. return this.owner.makeTokenBased(this.project_id, err => {
  841. if (err != null) {
  842. return done(err)
  843. }
  844. return db.projects.update(
  845. { _id: ObjectId(project_id) },
  846. { $set: { overleaf: { id: 1234 } } },
  847. err => {
  848. if (err != null) {
  849. return done(err)
  850. }
  851. return this.owner.getProject(
  852. this.project_id,
  853. (err, project) => {
  854. if (err != null) {
  855. return done(err)
  856. }
  857. this.tokens = project.tokens
  858. MockV1Api.setDocExported(this.tokens.readAndWrite, {
  859. exporting: true
  860. })
  861. MockV1Api.setDocExported(this.tokens.readOnly, {
  862. exporting: true
  863. })
  864. return done()
  865. }
  866. )
  867. }
  868. )
  869. })
  870. }
  871. )
  872. })
  873. after(function() {
  874. delete settings.projectImportingCheckMaxCreateDelta
  875. return delete settings.overleaf
  876. })
  877. it('should show importing page for read and write token', function(done) {
  878. return try_read_and_write_token_access(
  879. this.owner,
  880. this.tokens.readAndWrite,
  881. (response, body) => {
  882. expect(response.statusCode).to.equal(200)
  883. return expect(body).to.include('ImportingController')
  884. },
  885. done
  886. )
  887. })
  888. it('should show importing page for read only token', function(done) {
  889. return try_read_only_token_access(
  890. this.owner,
  891. this.tokens.readOnly,
  892. (response, body) => {
  893. expect(response.statusCode).to.equal(200)
  894. return expect(body).to.include('ImportingController')
  895. },
  896. done
  897. )
  898. })
  899. return describe('when importing check not configured', function() {
  900. before(() => delete settings.projectImportingCheckMaxCreateDelta)
  901. return it('should load editor', function(done) {
  902. return try_read_and_write_token_access(
  903. this.owner,
  904. this.tokens.readAndWrite,
  905. (response, body) => {
  906. expect(response.statusCode).to.equal(200)
  907. return expect(body).to.include('IdeController')
  908. },
  909. done
  910. )
  911. })
  912. })
  913. })
  914. })