TokenAccessTests.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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. const tryFetchProjectTokens = (user, projectId, callback) => {
  219. user.request.get(
  220. { url: `/project/${projectId}/tokens`, json: true },
  221. (error, response, body) => {
  222. if (error) {
  223. return callback(error)
  224. }
  225. callback(null, response, body)
  226. }
  227. )
  228. }
  229. describe('TokenAccess', function () {
  230. beforeEach(function (done) {
  231. this.timeout(90000)
  232. this.owner = new User()
  233. this.other1 = new User()
  234. this.other2 = new User()
  235. this.anon = new User()
  236. this.siteAdmin = new User({ email: 'admin@example.com' })
  237. async.parallel(
  238. [
  239. cb =>
  240. this.siteAdmin.login(err => {
  241. if (err) {
  242. return cb(err)
  243. }
  244. this.siteAdmin.ensureAdmin(cb)
  245. }),
  246. cb => this.owner.login(cb),
  247. cb => this.other1.login(cb),
  248. cb => this.other2.login(cb),
  249. cb => this.anon.getCsrfToken(cb),
  250. ],
  251. done
  252. )
  253. })
  254. describe('no token-access', function () {
  255. beforeEach(function (done) {
  256. this.owner.createProject(
  257. `token-ro-test${Math.random()}`,
  258. (err, projectId) => {
  259. if (err != null) {
  260. return done(err)
  261. }
  262. this.projectId = projectId
  263. // Note, never made token-based,
  264. // thus no tokens
  265. done()
  266. }
  267. )
  268. })
  269. it('should deny access ', function (done) {
  270. async.series(
  271. [
  272. cb => {
  273. tryEditorAccess(
  274. this.other1,
  275. this.projectId,
  276. expectErrorResponse.restricted.html,
  277. cb
  278. )
  279. },
  280. cb => {
  281. tryContentAccess(
  282. this.other1,
  283. this.projectId,
  284. (response, body) => {
  285. expect(response.statusCode).to.equal(403)
  286. expect(body).to.equal('Forbidden')
  287. },
  288. cb
  289. )
  290. },
  291. ],
  292. done
  293. )
  294. })
  295. it('should deny access to access tokens', function (done) {
  296. tryFetchProjectTokens(this.other1, this.projectId, (error, response) => {
  297. expect(error).to.equal(null)
  298. expect(response.statusCode).to.equal(403)
  299. done()
  300. })
  301. })
  302. })
  303. describe('owner', function () {
  304. beforeEach(function (done) {
  305. this.projectName = `token-owner-test${Math.random()}`
  306. this.owner.createProject(this.projectName, (err, projectId) => {
  307. if (err != null) {
  308. return done(err)
  309. }
  310. this.projectId = projectId
  311. this.owner.makeTokenBased(this.projectId, err => {
  312. if (err != null) {
  313. return done(err)
  314. }
  315. this.owner.getProject(this.projectId, (err, project) => {
  316. if (err != null) {
  317. return done(err)
  318. }
  319. this.tokens = project.tokens
  320. done()
  321. })
  322. })
  323. })
  324. })
  325. it('should be able to fetch read-only and read-write tokens', function (done) {
  326. tryFetchProjectTokens(
  327. this.owner,
  328. this.projectId,
  329. (error, response, body) => {
  330. expect(error).to.equal(null)
  331. expect(response.statusCode).to.equal(200)
  332. expect(body).to.deep.equal({
  333. readOnly: this.tokens.readOnly,
  334. readAndWrite: this.tokens.readAndWrite,
  335. readAndWritePrefix: this.tokens.readAndWritePrefix,
  336. })
  337. done()
  338. }
  339. )
  340. })
  341. })
  342. describe('read-only token', function () {
  343. beforeEach(function (done) {
  344. this.projectName = `token-ro-test${Math.random()}`
  345. this.owner.createProject(this.projectName, (err, projectId) => {
  346. if (err != null) {
  347. return done(err)
  348. }
  349. this.projectId = projectId
  350. this.owner.makeTokenBased(this.projectId, err => {
  351. if (err != null) {
  352. return done(err)
  353. }
  354. this.owner.getProject(this.projectId, (err, project) => {
  355. if (err != null) {
  356. return done(err)
  357. }
  358. this.tokens = project.tokens
  359. done()
  360. })
  361. })
  362. })
  363. })
  364. it('allow the user read-only access to the project', function (done) {
  365. async.series(
  366. [
  367. cb => {
  368. // deny access before token is used
  369. tryEditorAccess(
  370. this.other1,
  371. this.projectId,
  372. expectErrorResponse.restricted.html,
  373. cb
  374. )
  375. },
  376. cb => {
  377. // try token
  378. tryReadOnlyTokenAccess(
  379. this.other1,
  380. this.tokens.readOnly,
  381. (response, body) => {
  382. expect(response.statusCode).to.equal(200)
  383. },
  384. (response, body) => {
  385. expect(response.statusCode).to.equal(200)
  386. expect(body.requireAccept.projectName).to.equal(
  387. this.projectName
  388. )
  389. },
  390. cb
  391. )
  392. },
  393. cb => {
  394. // deny access before token is accepted
  395. tryEditorAccess(
  396. this.other1,
  397. this.projectId,
  398. expectErrorResponse.restricted.html,
  399. cb
  400. )
  401. },
  402. cb => {
  403. // accept token
  404. tryReadOnlyTokenAccept(
  405. this.other1,
  406. this.tokens.readOnly,
  407. (response, body) => {
  408. expect(response.statusCode).to.equal(200)
  409. },
  410. (response, body) => {
  411. expect(response.statusCode).to.equal(200)
  412. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  413. expect(body.tokenAccessGranted).to.equal('readOnly')
  414. },
  415. cb
  416. )
  417. },
  418. cb => {
  419. // allow content access read-only
  420. tryContentAccess(
  421. this.other1,
  422. this.projectId,
  423. (response, body) => {
  424. expect(body.privilegeLevel).to.equal('readOnly')
  425. expect(body.isRestrictedUser).to.equal(true)
  426. expect(body.isTokenMember).to.equal(true)
  427. expect(body.isInvitedMember).to.equal(false)
  428. expect(body.project.owner).to.have.keys('_id')
  429. expect(body.project.owner).to.not.have.any.keys(
  430. 'email',
  431. 'first_name',
  432. 'last_name'
  433. )
  434. },
  435. cb
  436. )
  437. },
  438. cb => {
  439. tryEditorAccess(
  440. this.other1,
  441. this.projectId,
  442. (response, body) => {
  443. expect(response.statusCode).to.equal(200)
  444. },
  445. cb
  446. )
  447. },
  448. ],
  449. done
  450. )
  451. })
  452. it('should allow the user to fetch the read-only token', function (done) {
  453. async.series(
  454. [
  455. cb => {
  456. // accept token
  457. tryReadOnlyTokenAccept(
  458. this.other1,
  459. this.tokens.readOnly,
  460. (response, body) => {
  461. expect(response.statusCode).to.equal(200)
  462. },
  463. (response, body) => {
  464. expect(response.statusCode).to.equal(200)
  465. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  466. expect(body.tokenAccessGranted).to.equal('readOnly')
  467. },
  468. cb
  469. )
  470. },
  471. cb => {
  472. tryFetchProjectTokens(
  473. this.other1,
  474. this.projectId,
  475. (error, response, body) => {
  476. expect(error).to.equal(null)
  477. expect(response.statusCode).to.equal(200)
  478. expect(body).to.deep.equal({ readOnly: this.tokens.readOnly })
  479. cb()
  480. }
  481. )
  482. },
  483. ],
  484. done
  485. )
  486. })
  487. it('should redirect the admin to the project (with rw access)', function (done) {
  488. async.series(
  489. [
  490. cb => {
  491. // use token
  492. tryReadOnlyTokenAccess(
  493. this.siteAdmin,
  494. this.tokens.readOnly,
  495. (response, body) => {
  496. expect(response.statusCode).to.equal(200)
  497. },
  498. (response, body) => {
  499. expect(response.statusCode).to.equal(200)
  500. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  501. },
  502. cb
  503. )
  504. },
  505. cb => {
  506. // allow content access read-and-write
  507. tryContentAccess(
  508. this.siteAdmin,
  509. this.projectId,
  510. (response, body) => {
  511. expect(body.privilegeLevel).to.equal('owner')
  512. expect(body.isRestrictedUser).to.equal(false)
  513. expect(body.isTokenMember).to.equal(false)
  514. expect(body.isInvitedMember).to.equal(false)
  515. },
  516. cb
  517. )
  518. },
  519. ],
  520. done
  521. )
  522. })
  523. describe('made private again', function () {
  524. beforeEach(function (done) {
  525. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  526. })
  527. it('should not allow the user to access the project', function (done) {
  528. async.series(
  529. [
  530. // no access before token is used
  531. cb =>
  532. tryEditorAccess(
  533. this.other1,
  534. this.projectId,
  535. expectErrorResponse.restricted.html,
  536. cb
  537. ),
  538. // token goes nowhere
  539. cb =>
  540. tryReadOnlyTokenAccess(
  541. this.other1,
  542. this.tokens.readOnly,
  543. (response, body) => {
  544. expect(response.statusCode).to.equal(200)
  545. },
  546. (response, body) => {
  547. expect(response.statusCode).to.equal(404)
  548. },
  549. cb
  550. ),
  551. // still no access
  552. cb =>
  553. tryEditorAccess(
  554. this.other1,
  555. this.projectId,
  556. expectErrorResponse.restricted.html,
  557. cb
  558. ),
  559. cb =>
  560. tryContentAccess(
  561. this.other1,
  562. this.projectId,
  563. (response, body) => {
  564. expect(response.statusCode).to.equal(403)
  565. expect(body).to.equal('Forbidden')
  566. },
  567. cb
  568. ),
  569. ],
  570. done
  571. )
  572. })
  573. })
  574. it('should deny access to access tokens', function (done) {
  575. tryFetchProjectTokens(this.other1, this.projectId, (error, response) => {
  576. expect(error).to.equal(null)
  577. expect(response.statusCode).to.equal(403)
  578. done()
  579. })
  580. })
  581. })
  582. describe('anonymous read-only token', function () {
  583. beforeEach(function (done) {
  584. this.owner.createProject(
  585. `token-anon-ro-test${Math.random()}`,
  586. (err, projectId) => {
  587. if (err != null) {
  588. return done(err)
  589. }
  590. this.projectId = projectId
  591. this.owner.makeTokenBased(this.projectId, err => {
  592. if (err != null) {
  593. return done(err)
  594. }
  595. this.owner.getProject(this.projectId, (err, project) => {
  596. if (err != null) {
  597. return done(err)
  598. }
  599. this.tokens = project.tokens
  600. done()
  601. })
  602. })
  603. }
  604. )
  605. })
  606. it('should allow the user to access project via read-only token url', function (done) {
  607. async.series(
  608. [
  609. cb =>
  610. tryEditorAccess(
  611. this.anon,
  612. this.projectId,
  613. expectErrorResponse.restricted.html,
  614. cb
  615. ),
  616. cb =>
  617. tryReadOnlyTokenAccess(
  618. this.anon,
  619. this.tokens.readOnly,
  620. (response, body) => {
  621. expect(response.statusCode).to.equal(200)
  622. },
  623. (response, body) => {
  624. expect(response.statusCode).to.equal(200)
  625. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  626. expect(body.grantAnonymousAccess).to.equal('readOnly')
  627. },
  628. cb
  629. ),
  630. cb =>
  631. tryEditorAccess(
  632. this.anon,
  633. this.projectId,
  634. (response, body) => {
  635. expect(response.statusCode).to.equal(200)
  636. },
  637. cb
  638. ),
  639. cb =>
  640. tryAnonContentAccess(
  641. this.anon,
  642. this.projectId,
  643. this.tokens.readOnly,
  644. (response, body) => {
  645. expect(body.privilegeLevel).to.equal('readOnly')
  646. expect(body.isRestrictedUser).to.equal(true)
  647. expect(body.isTokenMember).to.equal(false)
  648. expect(body.isInvitedMember).to.equal(false)
  649. expect(body.project.owner).to.have.keys('_id')
  650. expect(body.project.owner).to.not.have.any.keys(
  651. 'email',
  652. 'first_name',
  653. 'last_name'
  654. )
  655. },
  656. cb
  657. ),
  658. ],
  659. done
  660. )
  661. })
  662. it('should allow the anonymous user to fetch the read-only token', function (done) {
  663. async.series(
  664. [
  665. cb =>
  666. tryReadOnlyTokenAccess(
  667. this.anon,
  668. this.tokens.readOnly,
  669. (response, body) => {
  670. expect(response.statusCode).to.equal(200)
  671. },
  672. (response, body) => {
  673. expect(response.statusCode).to.equal(200)
  674. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  675. expect(body.grantAnonymousAccess).to.equal('readOnly')
  676. },
  677. cb
  678. ),
  679. cb => {
  680. tryFetchProjectTokens(
  681. this.anon,
  682. this.projectId,
  683. (error, response, body) => {
  684. expect(error).to.equal(null)
  685. expect(response.statusCode).to.equal(200)
  686. expect(body).to.deep.equal({ readOnly: this.tokens.readOnly })
  687. cb()
  688. }
  689. )
  690. },
  691. ],
  692. done
  693. )
  694. })
  695. describe('made private again', function () {
  696. beforeEach(function (done) {
  697. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  698. })
  699. it('should deny access to project', function (done) {
  700. async.series(
  701. [
  702. cb =>
  703. tryEditorAccess(
  704. this.anon,
  705. this.projectId,
  706. expectErrorResponse.restricted.html,
  707. cb
  708. ),
  709. // should not allow the user to access read-only token
  710. cb =>
  711. tryReadOnlyTokenAccess(
  712. this.anon,
  713. this.tokens.readOnly,
  714. (response, body) => {
  715. expect(response.statusCode).to.equal(200)
  716. },
  717. (response, body) => {
  718. expect(response.statusCode).to.equal(404)
  719. },
  720. cb
  721. ),
  722. // still no access
  723. cb =>
  724. tryEditorAccess(
  725. this.anon,
  726. this.projectId,
  727. expectErrorResponse.restricted.html,
  728. cb
  729. ),
  730. // should not allow the user to join the project
  731. cb =>
  732. tryAnonContentAccess(
  733. this.anon,
  734. this.projectId,
  735. this.tokens.readOnly,
  736. (response, body) => {
  737. expect(response.statusCode).to.equal(403)
  738. expect(body).to.equal('Forbidden')
  739. },
  740. cb
  741. ),
  742. ],
  743. done
  744. )
  745. })
  746. it('should deny access to access tokens', function (done) {
  747. tryFetchProjectTokens(this.anon, this.projectId, (error, response) => {
  748. expect(error).to.equal(null)
  749. expect(response.statusCode).to.equal(403)
  750. done()
  751. })
  752. })
  753. })
  754. })
  755. describe('read-and-write token', function () {
  756. beforeEach(function (done) {
  757. this.projectName = `token-rw-test${Math.random()}`
  758. this.owner.createProject(this.projectName, (err, projectId) => {
  759. if (err != null) {
  760. return done(err)
  761. }
  762. this.projectId = projectId
  763. this.owner.makeTokenBased(this.projectId, err => {
  764. if (err != null) {
  765. return done(err)
  766. }
  767. this.owner.getProject(this.projectId, (err, project) => {
  768. if (err != null) {
  769. return done(err)
  770. }
  771. this.tokens = project.tokens
  772. done()
  773. })
  774. })
  775. })
  776. })
  777. it('should allow the user to access project via read-and-write token url', function (done) {
  778. async.series(
  779. [
  780. // deny access before the token is used
  781. cb =>
  782. tryEditorAccess(
  783. this.other1,
  784. this.projectId,
  785. expectErrorResponse.restricted.html,
  786. cb
  787. ),
  788. // try token
  789. cb =>
  790. tryReadAndWriteTokenAccess(
  791. this.other1,
  792. this.tokens.readAndWrite,
  793. (response, body) => {
  794. expect(response.statusCode).to.equal(200)
  795. },
  796. (response, body) => {
  797. expect(response.statusCode).to.equal(200)
  798. expect(body.requireAccept.projectName).to.equal(
  799. this.projectName
  800. )
  801. },
  802. cb
  803. ),
  804. // deny access before the token is accepted
  805. cb =>
  806. tryEditorAccess(
  807. this.other1,
  808. this.projectId,
  809. expectErrorResponse.restricted.html,
  810. cb
  811. ),
  812. // accept token
  813. cb =>
  814. tryReadAndWriteTokenAccept(
  815. this.other1,
  816. this.tokens.readAndWrite,
  817. (response, body) => {
  818. expect(response.statusCode).to.equal(200)
  819. },
  820. (response, body) => {
  821. expect(response.statusCode).to.equal(200)
  822. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  823. expect(body.tokenAccessGranted).to.equal('readAndWrite')
  824. },
  825. cb
  826. ),
  827. cb =>
  828. tryEditorAccess(
  829. this.other1,
  830. this.projectId,
  831. (response, body) => {
  832. expect(response.statusCode).to.equal(200)
  833. },
  834. cb
  835. ),
  836. cb =>
  837. tryContentAccess(
  838. this.other1,
  839. this.projectId,
  840. (response, body) => {
  841. expect(body.privilegeLevel).to.equal('readAndWrite')
  842. expect(body.isRestrictedUser).to.equal(false)
  843. expect(body.isTokenMember).to.equal(true)
  844. expect(body.isInvitedMember).to.equal(false)
  845. expect(body.project.owner).to.have.all.keys(
  846. '_id',
  847. 'email',
  848. 'first_name',
  849. 'last_name',
  850. 'privileges',
  851. 'signUpDate'
  852. )
  853. },
  854. cb
  855. ),
  856. ],
  857. done
  858. )
  859. })
  860. it('fetching access tokens returns an empty object', function (done) {
  861. async.series(
  862. [
  863. cb =>
  864. tryReadAndWriteTokenAccept(
  865. this.other1,
  866. this.tokens.readAndWrite,
  867. (response, body) => {
  868. expect(response.statusCode).to.equal(200)
  869. },
  870. (response, body) => {
  871. expect(response.statusCode).to.equal(200)
  872. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  873. expect(body.tokenAccessGranted).to.equal('readAndWrite')
  874. },
  875. cb
  876. ),
  877. cb => {
  878. tryFetchProjectTokens(
  879. this.other1,
  880. this.projectId,
  881. (error, response, body) => {
  882. expect(error).to.equal(null)
  883. expect(response.statusCode).to.equal(200)
  884. expect(body).to.deep.equal({})
  885. cb()
  886. }
  887. )
  888. },
  889. ],
  890. done
  891. )
  892. })
  893. describe('upgrading from a read-only token', function () {
  894. beforeEach(function (done) {
  895. this.owner.createProject(
  896. `token-rw-upgrade-test${Math.random()}`,
  897. (err, projectId) => {
  898. if (err != null) {
  899. return done(err)
  900. }
  901. this.projectId = projectId
  902. this.owner.makeTokenBased(this.projectId, err => {
  903. if (err != null) {
  904. return done(err)
  905. }
  906. this.owner.getProject(this.projectId, (err, project) => {
  907. if (err != null) {
  908. return done(err)
  909. }
  910. this.tokens = project.tokens
  911. done()
  912. })
  913. })
  914. }
  915. )
  916. })
  917. it('should allow user to access project via read-only, then upgrade to read-write', function (done) {
  918. async.series(
  919. [
  920. // deny access before the token is used
  921. cb =>
  922. tryEditorAccess(
  923. this.other1,
  924. this.projectId,
  925. expectErrorResponse.restricted.html,
  926. cb
  927. ),
  928. cb => {
  929. // use read-only token
  930. tryReadOnlyTokenAccept(
  931. this.other1,
  932. this.tokens.readOnly,
  933. (response, body) => {
  934. expect(response.statusCode).to.equal(200)
  935. },
  936. (response, body) => {
  937. expect(response.statusCode).to.equal(200)
  938. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  939. expect(body.tokenAccessGranted).to.equal('readOnly')
  940. },
  941. cb
  942. )
  943. },
  944. cb => {
  945. tryEditorAccess(
  946. this.other1,
  947. this.projectId,
  948. (response, body) => {
  949. expect(response.statusCode).to.equal(200)
  950. },
  951. cb
  952. )
  953. },
  954. cb => {
  955. // allow content access read-only
  956. tryContentAccess(
  957. this.other1,
  958. this.projectId,
  959. (response, body) => {
  960. expect(body.privilegeLevel).to.equal('readOnly')
  961. expect(body.isRestrictedUser).to.equal(true)
  962. expect(body.isTokenMember).to.equal(true)
  963. expect(body.isInvitedMember).to.equal(false)
  964. expect(body.project.owner).to.have.keys('_id')
  965. expect(body.project.owner).to.not.have.any.keys(
  966. 'email',
  967. 'first_name',
  968. 'last_name'
  969. )
  970. },
  971. cb
  972. )
  973. },
  974. //
  975. // Then switch to read-write token
  976. //
  977. cb =>
  978. tryReadAndWriteTokenAccept(
  979. this.other1,
  980. this.tokens.readAndWrite,
  981. (response, body) => {
  982. expect(response.statusCode).to.equal(200)
  983. },
  984. (response, body) => {
  985. expect(response.statusCode).to.equal(200)
  986. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  987. expect(body.tokenAccessGranted).to.equal('readAndWrite')
  988. },
  989. cb
  990. ),
  991. cb =>
  992. tryEditorAccess(
  993. this.other1,
  994. this.projectId,
  995. (response, body) => {
  996. expect(response.statusCode).to.equal(200)
  997. },
  998. cb
  999. ),
  1000. cb =>
  1001. tryContentAccess(
  1002. this.other1,
  1003. this.projectId,
  1004. (response, body) => {
  1005. expect(body.privilegeLevel).to.equal('readAndWrite')
  1006. expect(body.isRestrictedUser).to.equal(false)
  1007. expect(body.isTokenMember).to.equal(true)
  1008. expect(body.isInvitedMember).to.equal(false)
  1009. expect(body.project.owner).to.have.all.keys(
  1010. '_id',
  1011. 'email',
  1012. 'first_name',
  1013. 'last_name',
  1014. 'privileges',
  1015. 'signUpDate'
  1016. )
  1017. },
  1018. cb
  1019. ),
  1020. ],
  1021. done
  1022. )
  1023. })
  1024. })
  1025. describe('made private again', function () {
  1026. beforeEach(function (done) {
  1027. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  1028. })
  1029. it('should deny access to project', function (done) {
  1030. async.series(
  1031. [
  1032. cb => {
  1033. tryEditorAccess(
  1034. this.other1,
  1035. this.projectId,
  1036. (response, body) => {},
  1037. cb
  1038. )
  1039. },
  1040. cb => {
  1041. tryReadAndWriteTokenAccess(
  1042. this.other1,
  1043. this.tokens.readAndWrite,
  1044. (response, body) => {
  1045. expect(response.statusCode).to.equal(200)
  1046. },
  1047. (response, body) => {
  1048. expect(response.statusCode).to.equal(404)
  1049. },
  1050. cb
  1051. )
  1052. },
  1053. cb => {
  1054. tryEditorAccess(
  1055. this.other1,
  1056. this.projectId,
  1057. expectErrorResponse.restricted.html,
  1058. cb
  1059. )
  1060. },
  1061. cb => {
  1062. tryContentAccess(
  1063. this.other1,
  1064. this.projectId,
  1065. (response, body) => {
  1066. expect(response.statusCode).to.equal(403)
  1067. expect(body).to.equal('Forbidden')
  1068. },
  1069. cb
  1070. )
  1071. },
  1072. ],
  1073. done
  1074. )
  1075. })
  1076. it('should deny access to access tokens', function (done) {
  1077. tryFetchProjectTokens(
  1078. this.other1,
  1079. this.projectId,
  1080. (error, response) => {
  1081. expect(error).to.equal(null)
  1082. expect(response.statusCode).to.equal(403)
  1083. done()
  1084. }
  1085. )
  1086. })
  1087. })
  1088. })
  1089. if (!settings.allowAnonymousReadAndWriteSharing) {
  1090. describe('anonymous read-and-write token, disabled', function () {
  1091. beforeEach(function (done) {
  1092. this.owner.createProject(
  1093. `token-anon-rw-test${Math.random()}`,
  1094. (err, projectId) => {
  1095. if (err != null) {
  1096. return done(err)
  1097. }
  1098. this.projectId = projectId
  1099. this.owner.makeTokenBased(this.projectId, err => {
  1100. if (err != null) {
  1101. return done(err)
  1102. }
  1103. this.owner.getProject(this.projectId, (err, project) => {
  1104. if (err != null) {
  1105. return done(err)
  1106. }
  1107. this.tokens = project.tokens
  1108. done()
  1109. })
  1110. })
  1111. }
  1112. )
  1113. })
  1114. it('should not allow the user to access read-and-write token', function (done) {
  1115. async.series(
  1116. [
  1117. cb =>
  1118. tryEditorAccess(
  1119. this.anon,
  1120. this.projectId,
  1121. expectErrorResponse.restricted.html,
  1122. cb
  1123. ),
  1124. cb =>
  1125. tryReadAndWriteTokenAccess(
  1126. this.anon,
  1127. this.tokens.readAndWrite,
  1128. (response, body) => {
  1129. expect(response.statusCode).to.equal(200)
  1130. },
  1131. (response, body) => {
  1132. expect(response.statusCode).to.equal(200)
  1133. expect(body).to.deep.equal({
  1134. redirect: '/restricted',
  1135. anonWriteAccessDenied: true,
  1136. })
  1137. },
  1138. cb
  1139. ),
  1140. cb =>
  1141. tryAnonContentAccess(
  1142. this.anon,
  1143. this.projectId,
  1144. this.tokens.readAndWrite,
  1145. (response, body) => {
  1146. expect(response.statusCode).to.equal(403)
  1147. expect(body).to.equal('Forbidden')
  1148. },
  1149. cb
  1150. ),
  1151. cb =>
  1152. this.anon.login((err, response, body) => {
  1153. expect(err).to.not.exist
  1154. expect(response.statusCode).to.equal(200)
  1155. expect(body.redir).to.equal(`/${this.tokens.readAndWrite}`)
  1156. cb()
  1157. }),
  1158. ],
  1159. done
  1160. )
  1161. })
  1162. it('should deny access to access tokens', function (done) {
  1163. tryFetchProjectTokens(this.anon, this.projectId, (error, response) => {
  1164. expect(error).to.equal(null)
  1165. expect(response.statusCode).to.equal(403)
  1166. done()
  1167. })
  1168. })
  1169. it('should require login if project does not exist', function (done) {
  1170. async.series(
  1171. [
  1172. // delete project
  1173. cb => {
  1174. this.owner.deleteProject(this.projectId, cb)
  1175. },
  1176. cb =>
  1177. tryReadAndWriteTokenAccess(
  1178. this.anon,
  1179. this.tokens.readAndWrite,
  1180. (response, body) => {
  1181. expect(response.statusCode).to.equal(200)
  1182. },
  1183. (response, body) => {
  1184. expect(response.statusCode).to.equal(200)
  1185. expect(body).to.deep.equal({
  1186. redirect: '/restricted',
  1187. anonWriteAccessDenied: true,
  1188. })
  1189. },
  1190. cb
  1191. ),
  1192. cb =>
  1193. this.anon.login((err, response, body) => {
  1194. expect(err).to.not.exist
  1195. expect(response.statusCode).to.equal(200)
  1196. expect(body.redir).to.equal(`/${this.tokens.readAndWrite}`)
  1197. cb()
  1198. }),
  1199. ],
  1200. done
  1201. )
  1202. })
  1203. })
  1204. } else {
  1205. describe('anonymous read-and-write token, enabled', function () {
  1206. beforeEach(function (done) {
  1207. this.owner.createProject(
  1208. `token-anon-rw-test${Math.random()}`,
  1209. (err, projectId) => {
  1210. if (err != null) {
  1211. return done(err)
  1212. }
  1213. this.projectId = projectId
  1214. this.owner.makeTokenBased(this.projectId, err => {
  1215. if (err != null) {
  1216. return done(err)
  1217. }
  1218. this.owner.getProject(this.projectId, (err, project) => {
  1219. if (err != null) {
  1220. return done(err)
  1221. }
  1222. this.tokens = project.tokens
  1223. done()
  1224. })
  1225. })
  1226. }
  1227. )
  1228. })
  1229. it('should allow the user to access project via read-and-write token url', function (done) {
  1230. async.series(
  1231. [
  1232. cb =>
  1233. tryEditorAccess(
  1234. this.anon,
  1235. this.projectId,
  1236. expectErrorResponse.restricted.html,
  1237. cb
  1238. ),
  1239. cb =>
  1240. tryReadAndWriteTokenAccess(
  1241. this.anon,
  1242. this.tokens.readAndWrite,
  1243. (response, body) => {
  1244. expect(response.statusCode).to.equal(200)
  1245. },
  1246. (response, body) => {
  1247. expect(response.statusCode).to.equal(200)
  1248. expect(body.redirect).to.equal(`/project/${this.projectId}`)
  1249. expect(body.grantAnonymousAccess).to.equal('readAndWrite')
  1250. },
  1251. cb
  1252. ),
  1253. cb =>
  1254. tryEditorAccess(
  1255. this.anon,
  1256. this.projectId,
  1257. (response, body) => {
  1258. expect(response.statusCode).to.equal(200)
  1259. },
  1260. cb
  1261. ),
  1262. cb =>
  1263. tryAnonContentAccess(
  1264. this.anon,
  1265. this.projectId,
  1266. this.tokens.readAndWrite,
  1267. (response, body) => {
  1268. expect(body.privilegeLevel).to.equal('readAndWrite')
  1269. },
  1270. cb
  1271. ),
  1272. ],
  1273. done
  1274. )
  1275. })
  1276. describe('made private again', function () {
  1277. beforeEach(function (done) {
  1278. this.owner.makePrivate(this.projectId, () => setTimeout(done, 1000))
  1279. })
  1280. it('should not allow the user to access read-and-write token', function (done) {
  1281. async.series(
  1282. [
  1283. cb =>
  1284. tryEditorAccess(
  1285. this.anon,
  1286. this.projectId,
  1287. expectErrorResponse.restricted.html,
  1288. cb
  1289. ),
  1290. cb =>
  1291. tryReadAndWriteTokenAccess(
  1292. this.anon,
  1293. this.tokens.readAndWrite,
  1294. (response, body) => {
  1295. expect(response.statusCode).to.equal(200)
  1296. },
  1297. (response, body) => {
  1298. expect(response.statusCode).to.equal(404)
  1299. },
  1300. cb
  1301. ),
  1302. cb =>
  1303. tryEditorAccess(
  1304. this.anon,
  1305. this.projectId,
  1306. expectErrorResponse.restricted.html,
  1307. cb
  1308. ),
  1309. cb =>
  1310. tryAnonContentAccess(
  1311. this.anon,
  1312. this.projectId,
  1313. this.tokens.readAndWrite,
  1314. (response, body) => {
  1315. expect(response.statusCode).to.equal(403)
  1316. expect(body).to.equal('Forbidden')
  1317. },
  1318. cb
  1319. ),
  1320. ],
  1321. done
  1322. )
  1323. })
  1324. it('should deny access to access tokens', function (done) {
  1325. tryFetchProjectTokens(
  1326. this.anon,
  1327. this.projectId,
  1328. (error, response) => {
  1329. expect(error).to.equal(null)
  1330. expect(response.statusCode).to.equal(403)
  1331. done()
  1332. }
  1333. )
  1334. })
  1335. })
  1336. it('should 404 if project does not exist', function (done) {
  1337. async.series(
  1338. [
  1339. // delete project
  1340. cb => {
  1341. this.owner.deleteProject(this.projectId, cb)
  1342. },
  1343. cb =>
  1344. tryReadAndWriteTokenAccess(
  1345. this.anon,
  1346. this.tokens.readAndWrite,
  1347. (response, body) => {
  1348. expect(response.statusCode).to.equal(200)
  1349. },
  1350. (response, body) => {
  1351. expect(response.statusCode).to.equal(200)
  1352. expect(body).to.deep.equal({
  1353. v1Import: {
  1354. status: 'mustLogin',
  1355. },
  1356. })
  1357. },
  1358. cb
  1359. ),
  1360. ],
  1361. done
  1362. )
  1363. })
  1364. })
  1365. }
  1366. describe('private overleaf project', function () {
  1367. beforeEach(function (done) {
  1368. this.owner.createProject('overleaf-import', (err, projectId) => {
  1369. expect(err).not.to.exist
  1370. this.projectId = projectId
  1371. this.owner.makeTokenBased(this.projectId, err => {
  1372. expect(err).not.to.exist
  1373. this.owner.getProject(this.projectId, (err, project) => {
  1374. expect(err).not.to.exist
  1375. this.tokens = project.tokens
  1376. this.owner.makePrivate(this.projectId, () => {
  1377. db.projects.updateOne(
  1378. { _id: project._id },
  1379. {
  1380. $set: {
  1381. overleaf: { id: 1234 },
  1382. },
  1383. },
  1384. err => {
  1385. expect(err).not.to.exist
  1386. done()
  1387. }
  1388. )
  1389. })
  1390. })
  1391. })
  1392. })
  1393. })
  1394. it('should only allow the owner access to the project', function (done) {
  1395. async.series(
  1396. [
  1397. // should redirect to canonical path, when owner uses read-write token
  1398. cb =>
  1399. tryReadAndWriteTokenAccess(
  1400. this.owner,
  1401. this.tokens.readAndWrite,
  1402. (response, body) => {
  1403. expect(response.statusCode).to.equal(200)
  1404. },
  1405. (response, body) => {
  1406. expect(response.statusCode).to.equal(200)
  1407. expect(response.body.redirect).to.equal(
  1408. `/project/${this.projectId}`
  1409. )
  1410. expect(response.body.higherAccess).to.equal(true)
  1411. },
  1412. cb
  1413. ),
  1414. cb =>
  1415. tryEditorAccess(
  1416. this.owner,
  1417. this.projectId,
  1418. (response, body) => {
  1419. expect(response.statusCode).to.equal(200)
  1420. },
  1421. cb
  1422. ),
  1423. cb =>
  1424. tryContentAccess(
  1425. this.owner,
  1426. this.projectId,
  1427. (response, body) => {
  1428. expect(body.privilegeLevel).to.equal('owner')
  1429. },
  1430. cb
  1431. ),
  1432. // non-owner should be denied access
  1433. cb =>
  1434. tryContentAccess(
  1435. this.other2,
  1436. this.projectId,
  1437. (response, body) => {
  1438. expect(response.statusCode).to.equal(403)
  1439. expect(body).to.equal('Forbidden')
  1440. },
  1441. cb
  1442. ),
  1443. ],
  1444. done
  1445. )
  1446. })
  1447. })
  1448. describe('private project, with higher access', function () {
  1449. beforeEach(function (done) {
  1450. this.owner.createProject(
  1451. `higher-access-test-${Math.random()}`,
  1452. (err, projectId) => {
  1453. expect(err).not.to.exist
  1454. this.projectId = projectId
  1455. this.owner.addUserToProject(
  1456. this.projectId,
  1457. this.other1,
  1458. 'readAndWrite',
  1459. err => {
  1460. expect(err).not.to.exist
  1461. this.owner.makeTokenBased(this.projectId, err => {
  1462. expect(err).not.to.exist
  1463. this.owner.getProject(this.projectId, (err, project) => {
  1464. expect(err).not.to.exist
  1465. this.tokens = project.tokens
  1466. this.owner.makePrivate(this.projectId, () => {
  1467. setTimeout(done, 1000)
  1468. })
  1469. })
  1470. })
  1471. }
  1472. )
  1473. }
  1474. )
  1475. })
  1476. it('should allow the user access to the project', function (done) {
  1477. async.series(
  1478. [
  1479. // should redirect to canonical path, when user uses read-write token
  1480. cb =>
  1481. tryReadAndWriteTokenAccess(
  1482. this.other1,
  1483. this.tokens.readAndWrite,
  1484. (response, body) => {
  1485. expect(response.statusCode).to.equal(200)
  1486. },
  1487. (response, body) => {
  1488. expect(response.statusCode).to.equal(200)
  1489. expect(response.body.redirect).to.equal(
  1490. `/project/${this.projectId}`
  1491. )
  1492. expect(response.body.higherAccess).to.equal(true)
  1493. },
  1494. cb
  1495. ),
  1496. // should redirect to canonical path, when user uses read-only token
  1497. cb =>
  1498. tryReadOnlyTokenAccess(
  1499. this.other1,
  1500. this.tokens.readOnly,
  1501. (response, body) => {
  1502. expect(response.statusCode).to.equal(200)
  1503. },
  1504. (response, body) => {
  1505. expect(response.statusCode).to.equal(200)
  1506. expect(response.body.redirect).to.equal(
  1507. `/project/${this.projectId}`
  1508. )
  1509. expect(response.body.higherAccess).to.equal(true)
  1510. },
  1511. cb
  1512. ),
  1513. // should allow the user access to the project
  1514. cb =>
  1515. tryEditorAccess(
  1516. this.other1,
  1517. this.projectId,
  1518. (response, body) => {
  1519. expect(response.statusCode).to.equal(200)
  1520. },
  1521. cb
  1522. ),
  1523. // should allow user to join the project
  1524. cb =>
  1525. tryContentAccess(
  1526. this.other1,
  1527. this.projectId,
  1528. (response, body) => {
  1529. expect(body.privilegeLevel).to.equal('readAndWrite')
  1530. expect(body.isRestrictedUser).to.equal(false)
  1531. expect(body.isTokenMember).to.equal(false)
  1532. expect(body.isInvitedMember).to.equal(true)
  1533. },
  1534. cb
  1535. ),
  1536. // should not allow a different user to join the project
  1537. cb =>
  1538. tryEditorAccess(
  1539. this.other2,
  1540. this.projectId,
  1541. expectErrorResponse.restricted.html,
  1542. cb
  1543. ),
  1544. cb =>
  1545. tryContentAccess(
  1546. this.other2,
  1547. this.projectId,
  1548. (response, body) => {
  1549. expect(response.statusCode).to.equal(403)
  1550. expect(body).to.equal('Forbidden')
  1551. },
  1552. cb
  1553. ),
  1554. ],
  1555. done
  1556. )
  1557. })
  1558. })
  1559. describe('deleted project', function () {
  1560. beforeEach(function (done) {
  1561. this.owner.createProject(
  1562. `delete-test${Math.random()}`,
  1563. (err, projectId) => {
  1564. if (err != null) {
  1565. return done(err)
  1566. }
  1567. this.projectId = projectId
  1568. this.owner.makeTokenBased(this.projectId, err => {
  1569. if (err != null) {
  1570. return done(err)
  1571. }
  1572. this.owner.getProject(this.projectId, (err, project) => {
  1573. if (err != null) {
  1574. return done(err)
  1575. }
  1576. this.tokens = project.tokens
  1577. done()
  1578. })
  1579. })
  1580. }
  1581. )
  1582. })
  1583. it('should 404', function (done) {
  1584. async.series(
  1585. [
  1586. // delete project
  1587. cb => {
  1588. this.owner.deleteProject(this.projectId, cb)
  1589. },
  1590. cb => {
  1591. // use read-only token
  1592. tryReadOnlyTokenAccess(
  1593. this.other1,
  1594. this.tokens.readOnly,
  1595. (response, body) => {
  1596. expect(response.statusCode).to.equal(200)
  1597. },
  1598. (response, body) => {
  1599. expect(response.statusCode).to.equal(404)
  1600. },
  1601. cb
  1602. )
  1603. },
  1604. cb => {
  1605. // use read-write token
  1606. tryReadAndWriteTokenAccess(
  1607. this.other1,
  1608. this.tokens.readAndWrite,
  1609. (response, body) => {
  1610. expect(response.statusCode).to.equal(200)
  1611. },
  1612. (response, body) => {
  1613. expect(response.statusCode).to.equal(404)
  1614. },
  1615. cb
  1616. )
  1617. },
  1618. ],
  1619. done
  1620. )
  1621. })
  1622. it('should deny access to access tokens with a 404', function (done) {
  1623. async.series(
  1624. [
  1625. // delete project
  1626. cb => {
  1627. this.owner.deleteProject(this.projectId, cb)
  1628. },
  1629. cb => {
  1630. // use read-only token
  1631. tryReadOnlyTokenAccess(
  1632. this.other1,
  1633. this.tokens.readOnly,
  1634. (response, body) => {
  1635. expect(response.statusCode).to.equal(200)
  1636. },
  1637. (response, body) => {
  1638. expect(response.statusCode).to.equal(404)
  1639. },
  1640. cb
  1641. )
  1642. },
  1643. cb => {
  1644. tryFetchProjectTokens(
  1645. this.other1,
  1646. this.projectId,
  1647. (error, response) => {
  1648. expect(error).to.equal(null)
  1649. expect(response.statusCode).to.equal(404)
  1650. cb()
  1651. }
  1652. )
  1653. },
  1654. ],
  1655. done
  1656. )
  1657. })
  1658. })
  1659. })