TokenAccessHandlerTests.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const should = require('chai').should()
  15. const SandboxedModule = require('sandboxed-module')
  16. const assert = require('assert')
  17. const path = require('path')
  18. const sinon = require('sinon')
  19. const modulePath = path.join(
  20. __dirname,
  21. '../../../../app/src/Features/TokenAccess/TokenAccessHandler'
  22. )
  23. const { expect } = require('chai')
  24. const { ObjectId } = require('mongojs')
  25. describe('TokenAccessHandler', function() {
  26. beforeEach(function() {
  27. this.token = 'sometokenthing'
  28. this.projectId = ObjectId()
  29. this.project = {
  30. _id: this.projectId,
  31. publicAccesLevel: 'tokenBased'
  32. }
  33. this.userId = ObjectId()
  34. this.req = {}
  35. return (this.TokenAccessHandler = SandboxedModule.require(modulePath, {
  36. globals: {
  37. console: console
  38. },
  39. requires: {
  40. '../../models/Project': { Project: (this.Project = {}) },
  41. 'settings-sharelatex': (this.settings = {}),
  42. '../Collaborators/CollaboratorsHandler': (this.CollaboratorsHandler = {}),
  43. '../User/UserGetter': (this.UserGetter = {}),
  44. '../V1/V1Api': (this.V1Api = {
  45. request: sinon.stub()
  46. })
  47. }
  48. }))
  49. })
  50. describe('findProjectWithReadOnlyToken', function() {
  51. beforeEach(function() {
  52. return (this.Project.findOne = sinon
  53. .stub()
  54. .callsArgWith(2, null, this.project))
  55. })
  56. it('should call Project.findOne', function(done) {
  57. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  58. this.token,
  59. (err, project) => {
  60. expect(this.Project.findOne.callCount).to.equal(1)
  61. expect(
  62. this.Project.findOne.calledWith({
  63. 'tokens.readOnly': this.token
  64. })
  65. ).to.equal(true)
  66. return done()
  67. }
  68. )
  69. })
  70. it('should produce a project object with no error', function(done) {
  71. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  72. this.token,
  73. (err, project) => {
  74. expect(err).to.not.exist
  75. expect(project).to.exist
  76. expect(project).to.deep.equal(this.project)
  77. return done()
  78. }
  79. )
  80. })
  81. it('should return projectExists flag as true', function(done) {
  82. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  83. this.token,
  84. function(err, project, projectExists) {
  85. expect(projectExists).to.equal(true)
  86. return done()
  87. }
  88. )
  89. })
  90. describe('when Project.findOne produces an error', function() {
  91. beforeEach(function() {
  92. return (this.Project.findOne = sinon
  93. .stub()
  94. .callsArgWith(2, new Error('woops')))
  95. })
  96. it('should produce an error', function(done) {
  97. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  98. this.token,
  99. (err, project) => {
  100. expect(err).to.exist
  101. expect(project).to.not.exist
  102. expect(err).to.be.instanceof(Error)
  103. return done()
  104. }
  105. )
  106. })
  107. })
  108. describe('when project does not have tokenBased access level', function() {
  109. beforeEach(function() {
  110. this.project.publicAccesLevel = 'private'
  111. return (this.Project.findOne = sinon
  112. .stub()
  113. .callsArgWith(2, null, this.project, true))
  114. })
  115. it('should not return a project', function(done) {
  116. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  117. this.token,
  118. function(err, project) {
  119. expect(err).to.not.exist
  120. expect(project).to.not.exist
  121. return done()
  122. }
  123. )
  124. })
  125. it('should return projectExists flag as true', function(done) {
  126. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  127. this.token,
  128. function(err, project, projectExists) {
  129. expect(projectExists).to.equal(true)
  130. return done()
  131. }
  132. )
  133. })
  134. })
  135. describe('when project does not exist', function() {
  136. beforeEach(function() {
  137. return (this.Project.findOne = sinon.stub().callsArgWith(2, null, null))
  138. })
  139. it('should not return a project', function(done) {
  140. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  141. this.token,
  142. function(err, project) {
  143. expect(err).to.not.exist
  144. expect(project).to.not.exist
  145. return done()
  146. }
  147. )
  148. })
  149. it('should return projectExists flag as false', function(done) {
  150. return this.TokenAccessHandler.findProjectWithReadOnlyToken(
  151. this.token,
  152. function(err, project, projectExists) {
  153. expect(projectExists).to.equal(false)
  154. return done()
  155. }
  156. )
  157. })
  158. })
  159. })
  160. describe('findProjectWithReadAndWriteToken', function() {
  161. beforeEach(function() {
  162. this.token = '1234bcdf'
  163. this.tokenPrefix = '1234'
  164. this.project.tokens = {
  165. readOnly: 'atntntn',
  166. readAndWrite: this.token,
  167. readAndWritePrefix: this.tokenPrefix
  168. }
  169. return (this.Project.findOne = sinon
  170. .stub()
  171. .callsArgWith(2, null, this.project))
  172. })
  173. it('should call Project.findOne', function(done) {
  174. return this.TokenAccessHandler.findProjectWithReadAndWriteToken(
  175. this.token,
  176. (err, project) => {
  177. expect(this.Project.findOne.callCount).to.equal(1)
  178. expect(
  179. this.Project.findOne.calledWith({
  180. 'tokens.readAndWritePrefix': this.tokenPrefix
  181. })
  182. ).to.equal(true)
  183. return done()
  184. }
  185. )
  186. })
  187. it('should produce a project object with no error', function(done) {
  188. return this.TokenAccessHandler.findProjectWithReadAndWriteToken(
  189. this.token,
  190. (err, project) => {
  191. expect(err).to.not.exist
  192. expect(project).to.exist
  193. expect(project).to.deep.equal(this.project)
  194. return done()
  195. }
  196. )
  197. })
  198. it('should return projectExists flag as true', function(done) {
  199. return this.TokenAccessHandler.findProjectWithReadAndWriteToken(
  200. this.token,
  201. function(err, project, projectExists) {
  202. expect(projectExists).to.equal(true)
  203. return done()
  204. }
  205. )
  206. })
  207. describe('when Project.findOne produces an error', function() {
  208. beforeEach(function() {
  209. return (this.Project.findOne = sinon
  210. .stub()
  211. .callsArgWith(2, new Error('woops')))
  212. })
  213. it('should produce an error', function(done) {
  214. return this.TokenAccessHandler.findProjectWithReadAndWriteToken(
  215. this.token,
  216. (err, project) => {
  217. expect(err).to.exist
  218. expect(project).to.not.exist
  219. expect(err).to.be.instanceof(Error)
  220. return done()
  221. }
  222. )
  223. })
  224. })
  225. describe('when project does not have tokenBased access level', function() {
  226. beforeEach(function() {
  227. this.project.publicAccesLevel = 'private'
  228. return (this.Project.findOne = sinon
  229. .stub()
  230. .callsArgWith(2, null, this.project, true))
  231. })
  232. it('should not return a project', function(done) {
  233. return this.TokenAccessHandler.findProjectWithReadAndWriteToken(
  234. this.token,
  235. function(err, project) {
  236. expect(err).to.not.exist
  237. expect(project).to.not.exist
  238. return done()
  239. }
  240. )
  241. })
  242. it('should return projectExists flag as true', function(done) {
  243. return this.TokenAccessHandler.findProjectWithReadAndWriteToken(
  244. this.token,
  245. function(err, project, projectExists) {
  246. expect(projectExists).to.equal(true)
  247. return done()
  248. }
  249. )
  250. })
  251. })
  252. describe('when the tokens have different lengths', function() {
  253. beforeEach(function() {
  254. this.project.tokens = {
  255. readOnly: 'atntntn',
  256. readAndWrite: this.token + 'some-other-characters',
  257. readAndWritePrefix: this.tokenPrefix
  258. }
  259. return (this.Project.findOne = sinon
  260. .stub()
  261. .callsArgWith(2, null, this.project))
  262. })
  263. it('should not return a project', function(done) {
  264. return this.TokenAccessHandler.findProjectWithReadAndWriteToken(
  265. this.token,
  266. function(err, project) {
  267. expect(err).to.not.exist
  268. expect(project).to.not.exist
  269. return done()
  270. }
  271. )
  272. })
  273. })
  274. })
  275. describe('findProjectWithHigherAccess', function() {
  276. describe('when user does have higher access', function() {
  277. beforeEach(function() {
  278. this.Project.findOne = sinon.stub().callsArgWith(2, null, this.project)
  279. return (this.CollaboratorsHandler.isUserInvitedMemberOfProject = sinon
  280. .stub()
  281. .callsArgWith(2, null, true))
  282. })
  283. it('should call Project.findOne', function(done) {
  284. return this.TokenAccessHandler.findProjectWithHigherAccess(
  285. this.token,
  286. this.userId,
  287. (err, project) => {
  288. expect(this.Project.findOne.callCount).to.equal(1)
  289. expect(
  290. this.Project.findOne.calledWith({
  291. 'tokens.readOnly': this.token
  292. })
  293. ).to.equal(true)
  294. return done()
  295. }
  296. )
  297. })
  298. it('should call isUserInvitedMemberOfProject', function(done) {
  299. return this.TokenAccessHandler.findProjectWithHigherAccess(
  300. this.token,
  301. this.userId,
  302. (err, project) => {
  303. expect(
  304. this.CollaboratorsHandler.isUserInvitedMemberOfProject.callCount
  305. ).to.equal(1)
  306. expect(
  307. this.CollaboratorsHandler.isUserInvitedMemberOfProject.calledWith(
  308. this.userId,
  309. this.project._id
  310. )
  311. ).to.equal(true)
  312. return done()
  313. }
  314. )
  315. })
  316. it('should produce a project object', function(done) {
  317. return this.TokenAccessHandler.findProjectWithHigherAccess(
  318. this.token,
  319. this.userId,
  320. (err, project) => {
  321. expect(err).to.not.exist
  322. expect(project).to.exist
  323. expect(project).to.deep.equal(this.project)
  324. return done()
  325. }
  326. )
  327. })
  328. })
  329. describe('when user does not have higher access', function() {
  330. beforeEach(function() {
  331. this.Project.findOne = sinon.stub().callsArgWith(2, null, this.project)
  332. return (this.CollaboratorsHandler.isUserInvitedMemberOfProject = sinon
  333. .stub()
  334. .callsArgWith(2, null, false))
  335. })
  336. it('should call Project.findOne', function(done) {
  337. return this.TokenAccessHandler.findProjectWithHigherAccess(
  338. this.token,
  339. this.userId,
  340. (err, project) => {
  341. expect(this.Project.findOne.callCount).to.equal(1)
  342. expect(
  343. this.Project.findOne.calledWith({
  344. 'tokens.readOnly': this.token
  345. })
  346. ).to.equal(true)
  347. return done()
  348. }
  349. )
  350. })
  351. it('should call isUserInvitedMemberOfProject', function(done) {
  352. return this.TokenAccessHandler.findProjectWithHigherAccess(
  353. this.token,
  354. this.userId,
  355. (err, project) => {
  356. expect(
  357. this.CollaboratorsHandler.isUserInvitedMemberOfProject.callCount
  358. ).to.equal(1)
  359. expect(
  360. this.CollaboratorsHandler.isUserInvitedMemberOfProject.calledWith(
  361. this.userId,
  362. this.project._id
  363. )
  364. ).to.equal(true)
  365. return done()
  366. }
  367. )
  368. })
  369. it('should not produce a project', function(done) {
  370. return this.TokenAccessHandler.findProjectWithHigherAccess(
  371. this.token,
  372. this.userId,
  373. (err, project) => {
  374. expect(err).to.not.exist
  375. expect(project).to.not.exist
  376. return done()
  377. }
  378. )
  379. })
  380. })
  381. describe('when Project.findOne produces an error', function() {
  382. beforeEach(function() {
  383. return (this.Project.findOne = sinon
  384. .stub()
  385. .callsArgWith(2, new Error('woops')))
  386. })
  387. it('should produce an error', function(done) {
  388. return this.TokenAccessHandler.findProjectWithHigherAccess(
  389. this.token,
  390. this.userId,
  391. (err, project) => {
  392. expect(err).to.exist
  393. expect(project).to.not.exist
  394. expect(err).to.be.instanceof(Error)
  395. return done()
  396. }
  397. )
  398. })
  399. })
  400. describe('when isUserInvitedMemberOfProject produces an error', function() {
  401. beforeEach(function() {
  402. this.Project.findOne = sinon.stub().callsArgWith(2, null, this.project)
  403. return (this.CollaboratorsHandler.isUserInvitedMemberOfProject = sinon
  404. .stub()
  405. .callsArgWith(2, new Error('woops')))
  406. })
  407. it('should produce an error', function(done) {
  408. return this.TokenAccessHandler.findProjectWithHigherAccess(
  409. this.token,
  410. this.userId,
  411. (err, project) => {
  412. expect(err).to.exist
  413. expect(project).to.not.exist
  414. expect(err).to.be.instanceof(Error)
  415. return done()
  416. }
  417. )
  418. })
  419. })
  420. })
  421. describe('addReadOnlyUserToProject', function() {
  422. beforeEach(function() {
  423. return (this.Project.update = sinon.stub().callsArgWith(2, null))
  424. })
  425. it('should call Project.update', function(done) {
  426. return this.TokenAccessHandler.addReadOnlyUserToProject(
  427. this.userId,
  428. this.projectId,
  429. err => {
  430. expect(this.Project.update.callCount).to.equal(1)
  431. expect(
  432. this.Project.update.calledWith({
  433. _id: this.projectId
  434. })
  435. ).to.equal(true)
  436. expect(
  437. this.Project.update.lastCall.args[1]['$addToSet']
  438. ).to.have.keys('tokenAccessReadOnly_refs')
  439. return done()
  440. }
  441. )
  442. })
  443. it('should not produce an error', function(done) {
  444. return this.TokenAccessHandler.addReadOnlyUserToProject(
  445. this.userId,
  446. this.projectId,
  447. err => {
  448. expect(err).to.not.exist
  449. return done()
  450. }
  451. )
  452. })
  453. describe('when Project.update produces an error', function() {
  454. beforeEach(function() {
  455. return (this.Project.update = sinon
  456. .stub()
  457. .callsArgWith(2, new Error('woops')))
  458. })
  459. it('should produce an error', function(done) {
  460. return this.TokenAccessHandler.addReadOnlyUserToProject(
  461. this.userId,
  462. this.projectId,
  463. err => {
  464. expect(err).to.exist
  465. return done()
  466. }
  467. )
  468. })
  469. })
  470. })
  471. describe('addReadAndWriteUserToProject', function() {
  472. beforeEach(function() {
  473. return (this.Project.update = sinon.stub().callsArgWith(2, null))
  474. })
  475. it('should call Project.update', function(done) {
  476. return this.TokenAccessHandler.addReadAndWriteUserToProject(
  477. this.userId,
  478. this.projectId,
  479. err => {
  480. expect(this.Project.update.callCount).to.equal(1)
  481. expect(
  482. this.Project.update.calledWith({
  483. _id: this.projectId
  484. })
  485. ).to.equal(true)
  486. expect(
  487. this.Project.update.lastCall.args[1]['$addToSet']
  488. ).to.have.keys('tokenAccessReadAndWrite_refs')
  489. return done()
  490. }
  491. )
  492. })
  493. it('should not produce an error', function(done) {
  494. return this.TokenAccessHandler.addReadAndWriteUserToProject(
  495. this.userId,
  496. this.projectId,
  497. err => {
  498. expect(err).to.not.exist
  499. return done()
  500. }
  501. )
  502. })
  503. describe('when Project.update produces an error', function() {
  504. beforeEach(function() {
  505. return (this.Project.update = sinon
  506. .stub()
  507. .callsArgWith(2, new Error('woops')))
  508. })
  509. it('should produce an error', function(done) {
  510. return this.TokenAccessHandler.addReadAndWriteUserToProject(
  511. this.userId,
  512. this.projectId,
  513. err => {
  514. expect(err).to.exist
  515. return done()
  516. }
  517. )
  518. })
  519. })
  520. })
  521. describe('grantSessionTokenAccess', function() {
  522. beforeEach(function() {
  523. return (this.req = { session: {}, headers: {} })
  524. })
  525. it('should add the token to the session', function(done) {
  526. this.TokenAccessHandler.grantSessionTokenAccess(
  527. this.req,
  528. this.projectId,
  529. this.token
  530. )
  531. expect(
  532. this.req.session.anonTokenAccess[this.projectId.toString()]
  533. ).to.equal(this.token)
  534. return done()
  535. })
  536. })
  537. describe('isValidToken', function() {
  538. describe('when a read-only project is found', function() {
  539. beforeEach(function() {
  540. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  541. .stub()
  542. .callsArgWith(1, null, null)
  543. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  544. .stub()
  545. .callsArgWith(1, null, this.project))
  546. })
  547. it('should try to find projects with both kinds of token', function(done) {
  548. return this.TokenAccessHandler.isValidToken(
  549. this.projectId,
  550. this.token,
  551. (err, allowed) => {
  552. expect(
  553. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  554. ).to.equal(1)
  555. expect(
  556. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  557. ).to.equal(1)
  558. return done()
  559. }
  560. )
  561. })
  562. it('should allow read-only access', function(done) {
  563. return this.TokenAccessHandler.isValidToken(
  564. this.projectId,
  565. this.token,
  566. (err, rw, ro) => {
  567. expect(err).to.not.exist
  568. expect(rw).to.equal(false)
  569. expect(ro).to.equal(true)
  570. return done()
  571. }
  572. )
  573. })
  574. })
  575. describe('when a read-and-write project is found', function() {
  576. beforeEach(function() {
  577. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  578. .stub()
  579. .callsArgWith(1, null, this.project)
  580. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  581. .stub()
  582. .callsArgWith(1, null, null))
  583. })
  584. it('should try to find projects with both kinds of token', function(done) {
  585. return this.TokenAccessHandler.isValidToken(
  586. this.projectId,
  587. this.token,
  588. (err, allowed) => {
  589. expect(
  590. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  591. ).to.equal(1)
  592. expect(
  593. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  594. ).to.equal(1)
  595. return done()
  596. }
  597. )
  598. })
  599. it('should allow read-and-write access', function(done) {
  600. return this.TokenAccessHandler.isValidToken(
  601. this.projectId,
  602. this.token,
  603. (err, rw, ro) => {
  604. expect(err).to.not.exist
  605. expect(rw).to.equal(true)
  606. expect(ro).to.equal(false)
  607. return done()
  608. }
  609. )
  610. })
  611. })
  612. describe('when no project is found', function() {
  613. beforeEach(function() {
  614. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  615. .stub()
  616. .callsArgWith(1, null, null)
  617. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  618. .stub()
  619. .callsArgWith(1, null, null))
  620. })
  621. it('should try to find projects with both kinds of token', function(done) {
  622. return this.TokenAccessHandler.isValidToken(
  623. this.projectId,
  624. this.token,
  625. (err, allowed) => {
  626. expect(
  627. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  628. ).to.equal(1)
  629. expect(
  630. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  631. ).to.equal(1)
  632. return done()
  633. }
  634. )
  635. })
  636. it('should not allow any access', function(done) {
  637. return this.TokenAccessHandler.isValidToken(
  638. this.projectId,
  639. this.token,
  640. (err, rw, ro) => {
  641. expect(err).to.not.exist
  642. expect(rw).to.equal(false)
  643. expect(ro).to.equal(false)
  644. return done()
  645. }
  646. )
  647. })
  648. })
  649. describe('when findProject produces an error', function() {
  650. beforeEach(function() {
  651. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  652. .stub()
  653. .callsArgWith(1, null, null)
  654. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  655. .stub()
  656. .callsArgWith(1, new Error('woops')))
  657. })
  658. it('should try to find projects with both kinds of token', function(done) {
  659. return this.TokenAccessHandler.isValidToken(
  660. this.projectId,
  661. this.token,
  662. (err, allowed) => {
  663. expect(
  664. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  665. ).to.equal(1)
  666. expect(
  667. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  668. ).to.equal(1)
  669. return done()
  670. }
  671. )
  672. })
  673. it('should produce an error and not allow access', function(done) {
  674. return this.TokenAccessHandler.isValidToken(
  675. this.projectId,
  676. this.token,
  677. (err, rw, ro) => {
  678. expect(err).to.exist
  679. expect(err).to.be.instanceof(Error)
  680. expect(rw).to.equal(undefined)
  681. expect(ro).to.equal(undefined)
  682. return done()
  683. }
  684. )
  685. })
  686. })
  687. describe('when project is not set to token-based access', function() {
  688. beforeEach(function() {
  689. return (this.project.publicAccesLevel = 'private')
  690. })
  691. describe('for read-and-write project', function() {
  692. beforeEach(function() {
  693. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  694. .stub()
  695. .callsArgWith(1, null, this.project)
  696. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  697. .stub()
  698. .callsArgWith(1, null, null))
  699. })
  700. it('should try to find projects with both kinds of token', function(done) {
  701. return this.TokenAccessHandler.isValidToken(
  702. this.projectId,
  703. this.token,
  704. (err, allowed) => {
  705. expect(
  706. this.TokenAccessHandler.findProjectWithReadAndWriteToken
  707. .callCount
  708. ).to.equal(1)
  709. expect(
  710. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  711. ).to.equal(1)
  712. return done()
  713. }
  714. )
  715. })
  716. it('should not allow any access', function(done) {
  717. return this.TokenAccessHandler.isValidToken(
  718. this.projectId,
  719. this.token,
  720. (err, rw, ro) => {
  721. expect(err).to.not.exist
  722. expect(rw).to.equal(false)
  723. expect(ro).to.equal(false)
  724. return done()
  725. }
  726. )
  727. })
  728. })
  729. describe('for read-only project', function() {
  730. beforeEach(function() {
  731. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  732. .stub()
  733. .callsArgWith(1, null, null)
  734. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  735. .stub()
  736. .callsArgWith(1, null, this.project))
  737. })
  738. it('should try to find projects with both kinds of token', function(done) {
  739. return this.TokenAccessHandler.isValidToken(
  740. this.projectId,
  741. this.token,
  742. (err, allowed) => {
  743. expect(
  744. this.TokenAccessHandler.findProjectWithReadAndWriteToken
  745. .callCount
  746. ).to.equal(1)
  747. expect(
  748. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  749. ).to.equal(1)
  750. return done()
  751. }
  752. )
  753. })
  754. it('should not allow any access', function(done) {
  755. return this.TokenAccessHandler.isValidToken(
  756. this.projectId,
  757. this.token,
  758. (err, rw, ro) => {
  759. expect(err).to.not.exist
  760. expect(rw).to.equal(false)
  761. expect(ro).to.equal(false)
  762. return done()
  763. }
  764. )
  765. })
  766. })
  767. })
  768. describe('with nothing', function() {
  769. beforeEach(function() {
  770. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  771. .stub()
  772. .callsArgWith(1, null, this.project)
  773. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  774. .stub()
  775. .callsArgWith(1, null, null))
  776. })
  777. it('should not call findProjectWithReadOnlyToken', function(done) {
  778. return this.TokenAccessHandler.isValidToken(
  779. this.projectId,
  780. null,
  781. (err, allowed) => {
  782. expect(
  783. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  784. ).to.equal(0)
  785. return done()
  786. }
  787. )
  788. })
  789. it('should try to find projects with both kinds of token', function(done) {
  790. return this.TokenAccessHandler.isValidToken(
  791. this.projectId,
  792. null,
  793. (err, allowed) => {
  794. expect(
  795. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  796. ).to.equal(0)
  797. expect(
  798. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  799. ).to.equal(0)
  800. return done()
  801. }
  802. )
  803. })
  804. it('should not allow any access', function(done) {
  805. return this.TokenAccessHandler.isValidToken(
  806. this.projectId,
  807. null,
  808. (err, rw, ro) => {
  809. expect(err).to.not.exist
  810. expect(rw).to.equal(false)
  811. expect(ro).to.equal(false)
  812. return done()
  813. }
  814. )
  815. })
  816. })
  817. })
  818. describe('protectTokens', function() {
  819. beforeEach(function() {
  820. return (this.project = {
  821. tokens: {
  822. readAndWrite: 'rw',
  823. readOnly: 'ro',
  824. readAndWritePrefix: 'pre'
  825. }
  826. })
  827. })
  828. it('should hide write token from read-only user', function() {
  829. this.TokenAccessHandler.protectTokens(this.project, 'readOnly')
  830. expect(this.project.tokens.readAndWrite).to.equal('')
  831. expect(this.project.tokens.readAndWritePrefix).to.equal('')
  832. return expect(this.project.tokens.readOnly).to.equal('ro')
  833. })
  834. it('should hide read token from read-write user', function() {
  835. this.TokenAccessHandler.protectTokens(this.project, 'readAndWrite')
  836. expect(this.project.tokens.readAndWrite).to.equal('rw')
  837. return expect(this.project.tokens.readOnly).to.equal('')
  838. })
  839. it('should leave tokens in place for owner', function() {
  840. this.TokenAccessHandler.protectTokens(this.project, 'owner')
  841. expect(this.project.tokens.readAndWrite).to.equal('rw')
  842. return expect(this.project.tokens.readOnly).to.equal('ro')
  843. })
  844. })
  845. describe('getDocPublishedInfo', function() {
  846. beforeEach(function() {
  847. return (this.callback = sinon.stub())
  848. })
  849. describe('when v1 api not set', function() {
  850. beforeEach(function() {
  851. return this.TokenAccessHandler.getV1DocPublishedInfo(
  852. this.token,
  853. this.callback
  854. )
  855. })
  856. it('should not check access and return default info', function() {
  857. expect(this.V1Api.request.called).to.equal(false)
  858. return expect(
  859. this.callback.calledWith(null, {
  860. allow: true
  861. })
  862. ).to.equal(true)
  863. })
  864. })
  865. describe('when v1 api is set', function() {
  866. beforeEach(function() {
  867. return (this.settings.apis = { v1: 'v1' })
  868. })
  869. describe('on V1Api.request success', function() {
  870. beforeEach(function() {
  871. this.V1Api.request = sinon
  872. .stub()
  873. .callsArgWith(1, null, null, 'mock-data')
  874. return this.TokenAccessHandler.getV1DocPublishedInfo(
  875. this.token,
  876. this.callback
  877. )
  878. })
  879. it('should return response body', function() {
  880. expect(
  881. this.V1Api.request.calledWith({
  882. url: `/api/v1/sharelatex/docs/${this.token}/is_published`
  883. })
  884. ).to.equal(true)
  885. return expect(this.callback.calledWith(null, 'mock-data')).to.equal(
  886. true
  887. )
  888. })
  889. })
  890. describe('on V1Api.request error', function() {
  891. beforeEach(function() {
  892. this.V1Api.request = sinon.stub().callsArgWith(1, 'error')
  893. return this.TokenAccessHandler.getV1DocPublishedInfo(
  894. this.token,
  895. this.callback
  896. )
  897. })
  898. it('should callback with error', function() {
  899. return expect(this.callback.calledWith('error')).to.equal(true)
  900. })
  901. })
  902. })
  903. })
  904. describe('getV1DocInfo', function() {
  905. beforeEach(function() {
  906. this.v2UserId = 123
  907. return (this.callback = sinon.stub())
  908. })
  909. describe('when v1 api not set', function() {
  910. beforeEach(function() {
  911. return this.TokenAccessHandler.getV1DocInfo(
  912. this.token,
  913. this.v2UserId,
  914. this.callback
  915. )
  916. })
  917. it('should not check access and return default info', function() {
  918. expect(this.V1Api.request.called).to.equal(false)
  919. return expect(
  920. this.callback.calledWith(null, {
  921. exists: true,
  922. exported: false
  923. })
  924. ).to.equal(true)
  925. })
  926. })
  927. describe('when v1 api is set', function() {
  928. beforeEach(function() {
  929. return (this.settings.apis = { v1: 'v1' })
  930. })
  931. describe('on UserGetter.getUser success', function() {
  932. beforeEach(function() {
  933. this.UserGetter.getUser = sinon.stub().yields(null, {
  934. overleaf: { id: 1 }
  935. })
  936. return this.TokenAccessHandler.getV1DocInfo(
  937. this.token,
  938. this.v2UserId,
  939. this.callback
  940. )
  941. })
  942. it('should get user', function() {
  943. return expect(
  944. this.UserGetter.getUser.calledWith(this.v2UserId)
  945. ).to.equal(true)
  946. })
  947. })
  948. describe('on UserGetter.getUser error', function() {
  949. beforeEach(function() {
  950. this.error = new Error('failed to get user')
  951. this.UserGetter.getUser = sinon.stub().yields(this.error)
  952. return this.TokenAccessHandler.getV1DocInfo(
  953. this.token,
  954. this.v2UserId,
  955. this.callback
  956. )
  957. })
  958. it('should callback with error', function() {
  959. return expect(this.callback.calledWith(this.error)).to.equal(true)
  960. })
  961. })
  962. describe('on V1Api.request success', function() {
  963. beforeEach(function() {
  964. this.v1UserId = 1
  965. this.UserGetter.getUser = sinon.stub().yields(null, {
  966. overleaf: { id: this.v1UserId }
  967. })
  968. this.V1Api.request = sinon
  969. .stub()
  970. .callsArgWith(1, null, null, 'mock-data')
  971. return this.TokenAccessHandler.getV1DocInfo(
  972. this.token,
  973. this.v2UserId,
  974. this.callback
  975. )
  976. })
  977. it('should return response body', function() {
  978. expect(
  979. this.V1Api.request.calledWith({
  980. url: `/api/v1/sharelatex/users/${this.v1UserId}/docs/${
  981. this.token
  982. }/info`
  983. })
  984. ).to.equal(true)
  985. return expect(this.callback.calledWith(null, 'mock-data')).to.equal(
  986. true
  987. )
  988. })
  989. })
  990. describe('on V1Api.request error', function() {
  991. beforeEach(function() {
  992. this.UserGetter.getUser = sinon.stub().yields(null, {
  993. overleaf: { id: 1 }
  994. })
  995. this.V1Api.request = sinon.stub().callsArgWith(1, 'error')
  996. return this.TokenAccessHandler.getV1DocInfo(
  997. this.token,
  998. this.v2UserId,
  999. this.callback
  1000. )
  1001. })
  1002. it('should callback with error', function() {
  1003. return expect(this.callback.calledWith('error')).to.equal(true)
  1004. })
  1005. })
  1006. })
  1007. })
  1008. })