TokenAccessControllerTests.js 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const should = require('chai').should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/TokenAccess/TokenAccessController'
  21. )
  22. const { expect } = require('chai')
  23. const { ObjectId } = require('mongojs')
  24. const MockRequest = require('../helpers/MockRequest')
  25. const MockResponse = require('../helpers/MockResponse')
  26. const Errors = require('../../../../app/src/Features/Errors/Errors.js')
  27. describe('TokenAccessController', function() {
  28. beforeEach(function() {
  29. this.readOnlyToken = 'somereadonlytoken'
  30. this.readAndWriteToken = '42somereadandwritetoken'
  31. this.projectId = ObjectId()
  32. this.ownerId = 'owner'
  33. this.project = {
  34. _id: this.projectId,
  35. publicAccesLevel: 'tokenBased',
  36. tokens: {
  37. readOnly: this.readOnlyToken,
  38. readAndWrite: this.readAndWriteToken
  39. },
  40. owner_ref: this.ownerId
  41. }
  42. this.userId = ObjectId()
  43. this.TokenAccessController = SandboxedModule.require(modulePath, {
  44. requires: {
  45. '../Project/ProjectController': (this.ProjectController = {}),
  46. '../Authentication/AuthenticationController': (this.AuthenticationController = {}),
  47. './TokenAccessHandler': (this.TokenAccessHandler = {
  48. getV1DocPublishedInfo: sinon.stub().yields(null, {
  49. allow: true
  50. }),
  51. getV1DocInfo: sinon.stub().yields(null, {
  52. exists: true,
  53. exported: false
  54. })
  55. }),
  56. '../../infrastructure/Features': (this.Features = {
  57. hasFeature: sinon.stub().returns(false)
  58. }),
  59. 'logger-sharelatex': {
  60. log: sinon.stub(),
  61. warn: sinon.stub(),
  62. err: sinon.stub()
  63. },
  64. 'settings-sharelatex': {
  65. overleaf: {
  66. host: 'http://overleaf.test:5000'
  67. }
  68. },
  69. '../V1/V1Api': (this.V1Api = {
  70. request: sinon.stub().callsArgWith(1, null, {}, { allow: true })
  71. })
  72. }
  73. })
  74. return (this.AuthenticationController.getLoggedInUserId = sinon
  75. .stub()
  76. .returns(this.userId.toString()))
  77. })
  78. describe('readAndWriteToken', function() {
  79. beforeEach(function() {})
  80. describe('when all goes well', function() {
  81. beforeEach(function() {
  82. this.req = new MockRequest()
  83. this.res = new MockResponse()
  84. this.next = sinon.stub()
  85. this.req.params['read_and_write_token'] = this.readAndWriteToken
  86. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  87. .stub()
  88. .callsArgWith(1, null, this.project, true)
  89. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  90. .stub()
  91. .callsArgWith(2, null)
  92. this.ProjectController.loadEditor = sinon.stub()
  93. this.AuthenticationController.setRedirectInSession = sinon.stub()
  94. return this.TokenAccessController.readAndWriteToken(
  95. this.req,
  96. this.res,
  97. this.next
  98. )
  99. })
  100. it('should try to find a project with this token', function(done) {
  101. expect(
  102. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  103. ).to.equal(1)
  104. expect(
  105. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  106. this.readAndWriteToken
  107. )
  108. ).to.equal(true)
  109. return done()
  110. })
  111. it('should add the user to the project with read-write access', function(done) {
  112. expect(
  113. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  114. ).to.equal(1)
  115. expect(
  116. this.TokenAccessHandler.addReadAndWriteUserToProject.calledWith(
  117. this.userId.toString(),
  118. this.projectId
  119. )
  120. ).to.equal(true)
  121. return done()
  122. })
  123. it('should pass control to loadEditor', function(done) {
  124. expect(this.req.params.Project_id).to.equal(this.projectId.toString())
  125. expect(this.ProjectController.loadEditor.callCount).to.equal(1)
  126. expect(
  127. this.ProjectController.loadEditor.calledWith(
  128. this.req,
  129. this.res,
  130. this.next
  131. )
  132. ).to.equal(true)
  133. return done()
  134. })
  135. })
  136. describe('when the user is already the owner', function() {
  137. beforeEach(function() {
  138. this.req = new MockRequest()
  139. this.res = new MockResponse()
  140. this.next = sinon.stub()
  141. this.req.params['read_and_write_token'] = this.readAndWriteToken
  142. this.project.owner_ref = this.userId
  143. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  144. .stub()
  145. .callsArgWith(1, null, this.project, true)
  146. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  147. .stub()
  148. .callsArgWith(2, null)
  149. this.ProjectController.loadEditor = sinon.stub()
  150. return this.TokenAccessController.readAndWriteToken(
  151. this.req,
  152. this.res,
  153. this.next
  154. )
  155. })
  156. it('should try to find a project with this token', function(done) {
  157. expect(
  158. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  159. ).to.equal(1)
  160. expect(
  161. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  162. this.readAndWriteToken
  163. )
  164. ).to.equal(true)
  165. return done()
  166. })
  167. it('should not add the user to the project with read-write access', function(done) {
  168. expect(
  169. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  170. ).to.equal(0)
  171. return done()
  172. })
  173. it('should pass control to loadEditor', function(done) {
  174. expect(this.req.params.Project_id).to.equal(this.projectId.toString())
  175. expect(this.ProjectController.loadEditor.callCount).to.equal(1)
  176. expect(
  177. this.ProjectController.loadEditor.calledWith(
  178. this.req,
  179. this.res,
  180. this.next
  181. )
  182. ).to.equal(true)
  183. return done()
  184. })
  185. })
  186. describe('when there is no user', function() {
  187. beforeEach(function() {
  188. return (this.AuthenticationController.getLoggedInUserId = sinon
  189. .stub()
  190. .returns(null))
  191. })
  192. describe('when anonymous read-write access is enabled', function() {
  193. beforeEach(function() {
  194. this.TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = true
  195. this.req = new MockRequest()
  196. this.res = new MockResponse()
  197. this.next = sinon.stub()
  198. this.req.params['read_and_write_token'] = this.readAndWriteToken
  199. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  200. .stub()
  201. .callsArgWith(1, null, this.project, true)
  202. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  203. .stub()
  204. .callsArgWith(2, null)
  205. this.ProjectController.loadEditor = sinon.stub()
  206. this.TokenAccessHandler.grantSessionTokenAccess = sinon.stub()
  207. return this.TokenAccessController.readAndWriteToken(
  208. this.req,
  209. this.res,
  210. this.next
  211. )
  212. })
  213. it('should not add the user to the project with read-write access', function(done) {
  214. expect(
  215. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  216. ).to.equal(0)
  217. return done()
  218. })
  219. it('should give the user session token access', function(done) {
  220. expect(
  221. this.TokenAccessHandler.grantSessionTokenAccess.callCount
  222. ).to.equal(1)
  223. expect(
  224. this.TokenAccessHandler.grantSessionTokenAccess.calledWith(
  225. this.req,
  226. this.projectId,
  227. this.readAndWriteToken
  228. )
  229. ).to.equal(true)
  230. return done()
  231. })
  232. it('should pass control to loadEditor', function(done) {
  233. expect(this.req.params.Project_id).to.equal(this.projectId.toString())
  234. expect(this.ProjectController.loadEditor.callCount).to.equal(1)
  235. expect(
  236. this.ProjectController.loadEditor.calledWith(
  237. this.req,
  238. this.res,
  239. this.next
  240. )
  241. ).to.equal(true)
  242. return done()
  243. })
  244. })
  245. describe('when anonymous read-write access is not enabled', function() {
  246. beforeEach(function() {
  247. this.TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = false
  248. this.req = new MockRequest()
  249. this.res = new MockResponse()
  250. this.res.redirect = sinon.stub()
  251. this.next = sinon.stub()
  252. this.req.params['read_and_write_token'] = this.readAndWriteToken
  253. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  254. .stub()
  255. .callsArgWith(1, null, this.project, true)
  256. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  257. .stub()
  258. .callsArgWith(2, null)
  259. this.ProjectController.loadEditor = sinon.stub()
  260. this.TokenAccessHandler.grantSessionTokenAccess = sinon.stub()
  261. this.AuthenticationController.setRedirectInSession = sinon.stub()
  262. return this.TokenAccessController.readAndWriteToken(
  263. this.req,
  264. this.res,
  265. this.next
  266. )
  267. })
  268. it('should not add the user to the project with read-write access', function(done) {
  269. expect(
  270. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  271. ).to.equal(0)
  272. return done()
  273. })
  274. it('should give the user session token access', function(done) {
  275. expect(
  276. this.TokenAccessHandler.grantSessionTokenAccess.callCount
  277. ).to.equal(0)
  278. return done()
  279. })
  280. it('should not pass control to loadEditor', function(done) {
  281. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  282. expect(
  283. this.ProjectController.loadEditor.calledWith(
  284. this.req,
  285. this.res,
  286. this.next
  287. )
  288. ).to.equal(false)
  289. return done()
  290. })
  291. it('should set redirect in session', function(done) {
  292. expect(
  293. this.AuthenticationController.setRedirectInSession.callCount
  294. ).to.equal(1)
  295. expect(
  296. this.AuthenticationController.setRedirectInSession.calledWith(
  297. this.req
  298. )
  299. ).to.equal(true)
  300. return done()
  301. })
  302. it('should redirect to restricted page', function(done) {
  303. expect(this.res.redirect.callCount).to.equal(1)
  304. expect(this.res.redirect.calledWith('/restricted')).to.equal(true)
  305. return done()
  306. })
  307. })
  308. })
  309. describe('when findProject produces an error', function() {
  310. beforeEach(function() {
  311. this.req = new MockRequest()
  312. this.res = new MockResponse()
  313. this.next = sinon.stub()
  314. this.req.params['read_and_write_token'] = this.readAndWriteToken
  315. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  316. .stub()
  317. .callsArgWith(1, new Error('woops'))
  318. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  319. .stub()
  320. .callsArgWith(2, null)
  321. this.ProjectController.loadEditor = sinon.stub()
  322. return this.TokenAccessController.readAndWriteToken(
  323. this.req,
  324. this.res,
  325. this.next
  326. )
  327. })
  328. it('should try to find a project with this token', function(done) {
  329. expect(
  330. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  331. ).to.equal(1)
  332. expect(
  333. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  334. this.readAndWriteToken
  335. )
  336. ).to.equal(true)
  337. return done()
  338. })
  339. it('should not add the user to the project with read-write access', function(done) {
  340. expect(
  341. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  342. ).to.equal(0)
  343. return done()
  344. })
  345. it('should not pass control to loadEditor', function(done) {
  346. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  347. expect(
  348. this.ProjectController.loadEditor.calledWith(
  349. this.req,
  350. this.res,
  351. this.next
  352. )
  353. ).to.equal(false)
  354. return done()
  355. })
  356. it('should call next with an error', function(done) {
  357. expect(this.next.callCount).to.equal(1)
  358. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  359. return done()
  360. })
  361. })
  362. describe('when findProject does not find a project', function() {
  363. beforeEach(function() {})
  364. describe('when user is present', function() {
  365. beforeEach(function() {
  366. return (this.AuthenticationController.getLoggedInUserId = sinon
  367. .stub()
  368. .returns(this.userId.toString()))
  369. })
  370. describe('when project does not exist', function() {
  371. beforeEach(function() {
  372. this.req = new MockRequest()
  373. this.req.url = '/123abc'
  374. this.res = new MockResponse()
  375. this.res.redirect = sinon.stub()
  376. this.res.render = sinon.stub()
  377. this.next = sinon.stub()
  378. this.req.params['read_and_write_token'] = '123abc'
  379. return (this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  380. .stub()
  381. .callsArgWith(1, null, null, false))
  382. })
  383. describe('when project was not exported from v1', function() {
  384. beforeEach(function() {
  385. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  386. exists: true,
  387. exported: false
  388. })
  389. return this.TokenAccessController.readAndWriteToken(
  390. this.req,
  391. this.res,
  392. this.next
  393. )
  394. })
  395. it('should not redirect to v1', function(done) {
  396. expect(this.res.redirect.callCount).to.equal(0)
  397. done()
  398. })
  399. it('should show project import page', function(done) {
  400. expect(this.res.render.calledWith('project/v2-import')).to.equal(
  401. true
  402. )
  403. done()
  404. })
  405. })
  406. describe('when project was not exported from v1 but forcing import to v2', function() {
  407. beforeEach(function() {
  408. return this.Features.hasFeature.returns(true)
  409. })
  410. describe('with project name', function() {
  411. beforeEach(function() {
  412. this.TokenAccessHandler.getV1DocInfo = sinon
  413. .stub()
  414. .yields(null, {
  415. exists: true,
  416. exported: false,
  417. has_owner: true,
  418. name: 'A title',
  419. has_assignment: false,
  420. brand_info: null
  421. })
  422. return this.TokenAccessController.readAndWriteToken(
  423. this.req,
  424. this.res,
  425. this.next
  426. )
  427. })
  428. it('should render v2-import page with name', function(done) {
  429. expect(
  430. this.res.render.calledWith('project/v2-import', {
  431. projectId: '123abc',
  432. name: 'A title',
  433. hasOwner: true,
  434. hasAssignment: false,
  435. brandInfo: null
  436. })
  437. ).to.equal(true)
  438. return done()
  439. })
  440. })
  441. describe('with project owner', function() {
  442. beforeEach(function() {
  443. this.TokenAccessHandler.getV1DocInfo = sinon
  444. .stub()
  445. .yields(null, {
  446. exists: true,
  447. exported: false,
  448. has_owner: true,
  449. name: 'A title',
  450. has_assignment: false,
  451. brand_info: null
  452. })
  453. return this.TokenAccessController.readAndWriteToken(
  454. this.req,
  455. this.res,
  456. this.next
  457. )
  458. })
  459. it('should render v2-import page', function(done) {
  460. expect(
  461. this.res.render.calledWith('project/v2-import', {
  462. projectId: '123abc',
  463. hasOwner: true,
  464. name: 'A title',
  465. hasAssignment: false,
  466. brandInfo: null
  467. })
  468. ).to.equal(true)
  469. return done()
  470. })
  471. })
  472. describe('without project owner', function() {
  473. beforeEach(function() {
  474. this.TokenAccessHandler.getV1DocInfo = sinon
  475. .stub()
  476. .yields(null, {
  477. exists: true,
  478. exported: false,
  479. has_owner: false,
  480. name: 'A title',
  481. has_assignment: false,
  482. brand_info: null
  483. })
  484. return this.TokenAccessController.readAndWriteToken(
  485. this.req,
  486. this.res,
  487. this.next
  488. )
  489. })
  490. it('should render v2-import page', function(done) {
  491. expect(
  492. this.res.render.calledWith('project/v2-import', {
  493. projectId: '123abc',
  494. hasOwner: false,
  495. name: 'A title',
  496. hasAssignment: false,
  497. brandInfo: null
  498. })
  499. ).to.equal(true)
  500. return done()
  501. })
  502. })
  503. describe('with assignment', function() {
  504. beforeEach(function() {
  505. this.TokenAccessHandler.getV1DocInfo = sinon
  506. .stub()
  507. .yields(null, {
  508. exists: true,
  509. exported: false,
  510. has_owner: false,
  511. name: 'A title',
  512. has_assignment: true,
  513. brand_info: null
  514. })
  515. return this.TokenAccessController.readAndWriteToken(
  516. this.req,
  517. this.res,
  518. this.next
  519. )
  520. })
  521. it('should render v2-import page', function(done) {
  522. expect(
  523. this.res.render.calledWith('project/v2-import', {
  524. projectId: '123abc',
  525. hasOwner: false,
  526. name: 'A title',
  527. hasAssignment: true,
  528. brandInfo: null
  529. })
  530. ).to.equal(true)
  531. return done()
  532. })
  533. })
  534. describe('with brand info', function() {
  535. beforeEach(function() {
  536. this.TokenAccessHandler.getV1DocInfo = sinon
  537. .stub()
  538. .yields(null, {
  539. exists: true,
  540. exported: false,
  541. has_owner: false,
  542. name: 'A title',
  543. has_assignment: false,
  544. brand_info: 'wellcome'
  545. })
  546. return this.TokenAccessController.readAndWriteToken(
  547. this.req,
  548. this.res,
  549. this.next
  550. )
  551. })
  552. it('should render v2-import page', function(done) {
  553. expect(
  554. this.res.render.calledWith('project/v2-import', {
  555. projectId: '123abc',
  556. hasOwner: false,
  557. name: 'A title',
  558. hasAssignment: false,
  559. brandInfo: 'wellcome'
  560. })
  561. ).to.equal(true)
  562. return done()
  563. })
  564. })
  565. describe('with anonymous user', function() {
  566. beforeEach(function() {
  567. this.AuthenticationController.getLoggedInUserId = sinon
  568. .stub()
  569. .returns(null)
  570. return this.TokenAccessController.readAndWriteToken(
  571. this.req,
  572. this.res,
  573. this.next
  574. )
  575. })
  576. it('should render anonymous import status page', function(done) {
  577. expect(this.res.render.callCount).to.equal(1)
  578. expect(
  579. this.res.render.calledWith('project/v2-import', {
  580. loginRedirect: '/123abc'
  581. })
  582. ).to.equal(true)
  583. return done()
  584. })
  585. })
  586. })
  587. describe('when project was exported from v1', function() {
  588. beforeEach(function() {
  589. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  590. exists: true,
  591. exported: true
  592. })
  593. return this.TokenAccessController.readAndWriteToken(
  594. this.req,
  595. this.res,
  596. this.next
  597. )
  598. })
  599. it('should call next with a not-found error', function(done) {
  600. expect(this.next.callCount).to.equal(1)
  601. return done()
  602. })
  603. })
  604. describe('when project does not exist on v1', function() {
  605. beforeEach(function() {
  606. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  607. exists: false,
  608. exported: false
  609. })
  610. return this.TokenAccessController.readAndWriteToken(
  611. this.req,
  612. this.res,
  613. this.next
  614. )
  615. })
  616. it('should call next with a not-found error', function(done) {
  617. expect(this.next.callCount).to.equal(1)
  618. expect(this.next.calledWith(new Errors.NotFoundError())).to.equal(
  619. true
  620. )
  621. return done()
  622. })
  623. })
  624. })
  625. describe('when token access is off, but user has higher access anyway', function() {
  626. beforeEach(function() {
  627. this.req = new MockRequest()
  628. this.res = new MockResponse()
  629. this.res.redirect = sinon.stub()
  630. this.next = sinon.stub()
  631. this.req.params['read_and_write_token'] = this.readAndWriteToken
  632. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  633. .stub()
  634. .callsArgWith(1, null, null, true)
  635. this.TokenAccessHandler.findProjectWithHigherAccess = sinon
  636. .stub()
  637. .callsArgWith(2, null, this.project)
  638. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  639. .stub()
  640. .callsArgWith(2, null)
  641. this.ProjectController.loadEditor = sinon.stub()
  642. return this.TokenAccessController.readAndWriteToken(
  643. this.req,
  644. this.res,
  645. this.next
  646. )
  647. })
  648. it('should try to find a project with this token', function(done) {
  649. expect(
  650. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  651. ).to.equal(1)
  652. expect(
  653. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  654. this.readAndWriteToken
  655. )
  656. ).to.equal(true)
  657. return done()
  658. })
  659. it('should check if user has higher access to the token project', function(done) {
  660. expect(
  661. this.TokenAccessHandler.findProjectWithHigherAccess.callCount
  662. ).to.equal(1)
  663. return done()
  664. })
  665. it('should not add the user to the project with read-write access', function(done) {
  666. expect(
  667. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  668. ).to.equal(0)
  669. return done()
  670. })
  671. it('should not pass control to loadEditor', function(done) {
  672. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  673. expect(
  674. this.ProjectController.loadEditor.calledWith(
  675. this.req,
  676. this.res,
  677. this.next
  678. )
  679. ).to.equal(false)
  680. return done()
  681. })
  682. it('should not call next with a not-found error', function(done) {
  683. expect(this.next.callCount).to.equal(0)
  684. return done()
  685. })
  686. it('should redirect to the canonical project url', function(done) {
  687. expect(this.res.redirect.callCount).to.equal(1)
  688. expect(
  689. this.res.redirect.calledWith(302, `/project/${this.project._id}`)
  690. ).to.equal(true)
  691. return done()
  692. })
  693. })
  694. describe('when higher access is not available', function() {
  695. beforeEach(function() {
  696. this.req = new MockRequest()
  697. this.res = new MockResponse()
  698. this.next = sinon.stub()
  699. this.req.params['read_and_write_token'] = this.readAndWriteToken
  700. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  701. .stub()
  702. .callsArgWith(1, null, null, true)
  703. this.TokenAccessHandler.findProjectWithHigherAccess = sinon
  704. .stub()
  705. .callsArgWith(2, null, null)
  706. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  707. .stub()
  708. .callsArgWith(2, null)
  709. this.ProjectController.loadEditor = sinon.stub()
  710. return this.TokenAccessController.readAndWriteToken(
  711. this.req,
  712. this.res,
  713. this.next
  714. )
  715. })
  716. it('should try to find a project with this token', function(done) {
  717. expect(
  718. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  719. ).to.equal(1)
  720. expect(
  721. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  722. this.readAndWriteToken
  723. )
  724. ).to.equal(true)
  725. return done()
  726. })
  727. it('should check if user has higher access to the token project', function(done) {
  728. expect(
  729. this.TokenAccessHandler.findProjectWithHigherAccess.callCount
  730. ).to.equal(1)
  731. return done()
  732. })
  733. it('should not add the user to the project with read-write access', function(done) {
  734. expect(
  735. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  736. ).to.equal(0)
  737. return done()
  738. })
  739. it('should not pass control to loadEditor', function(done) {
  740. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  741. expect(
  742. this.ProjectController.loadEditor.calledWith(
  743. this.req,
  744. this.res,
  745. this.next
  746. )
  747. ).to.equal(false)
  748. return done()
  749. })
  750. it('should call next with a not-found error', function(done) {
  751. expect(this.next.callCount).to.equal(1)
  752. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  753. return done()
  754. })
  755. })
  756. })
  757. })
  758. describe('when adding user to project produces an error', function() {
  759. beforeEach(function() {
  760. this.req = new MockRequest()
  761. this.res = new MockResponse()
  762. this.next = sinon.stub()
  763. this.req.params['read_and_write_token'] = this.readAndWriteToken
  764. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  765. .stub()
  766. .callsArgWith(1, null, this.project, true)
  767. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  768. .stub()
  769. .callsArgWith(2, new Error('woops'))
  770. this.ProjectController.loadEditor = sinon.stub()
  771. return this.TokenAccessController.readAndWriteToken(
  772. this.req,
  773. this.res,
  774. this.next
  775. )
  776. })
  777. it('should try to find a project with this token', function(done) {
  778. expect(
  779. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  780. ).to.equal(1)
  781. expect(
  782. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  783. this.readAndWriteToken
  784. )
  785. ).to.equal(true)
  786. return done()
  787. })
  788. it('should add the user to the project with read-write access', function(done) {
  789. expect(
  790. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  791. ).to.equal(1)
  792. expect(
  793. this.TokenAccessHandler.addReadAndWriteUserToProject.calledWith(
  794. this.userId.toString(),
  795. this.projectId
  796. )
  797. ).to.equal(true)
  798. return done()
  799. })
  800. it('should not pass control to loadEditor', function(done) {
  801. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  802. expect(
  803. this.ProjectController.loadEditor.calledWith(
  804. this.req,
  805. this.res,
  806. this.next
  807. )
  808. ).to.equal(false)
  809. return done()
  810. })
  811. it('should call next with an error', function(done) {
  812. expect(this.next.callCount).to.equal(1)
  813. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  814. return done()
  815. })
  816. })
  817. })
  818. describe('readOnlyToken', function() {
  819. beforeEach(function() {
  820. return (this.TokenAccessHandler.checkV1Access = sinon
  821. .stub()
  822. .callsArgWith(1, null, true))
  823. })
  824. describe('when access not allowed by v1 api', function() {
  825. beforeEach(function() {
  826. this.req = new MockRequest()
  827. this.res = new MockResponse()
  828. this.res.redirect = sinon.stub()
  829. this.next = sinon.stub()
  830. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  831. .stub()
  832. .callsArgWith(1, null, this.project, true)
  833. this.TokenAccessHandler.getV1DocPublishedInfo = sinon
  834. .stub()
  835. .yields(null, {
  836. allow: false,
  837. published_path: 'doc-url'
  838. })
  839. return this.TokenAccessController.readOnlyToken(
  840. this.req,
  841. this.res,
  842. this.next
  843. )
  844. })
  845. it('should redirect to doc-url', function() {
  846. return expect(this.res.redirect.calledWith('doc-url')).to.equal(true)
  847. })
  848. })
  849. describe('with a user', function() {
  850. beforeEach(function() {
  851. return (this.AuthenticationController.getLoggedInUserId = sinon
  852. .stub()
  853. .returns(this.userId.toString()))
  854. })
  855. describe('when all goes well', function() {
  856. beforeEach(function() {
  857. this.req = new MockRequest()
  858. this.res = new MockResponse()
  859. this.next = sinon.stub()
  860. this.req.params['read_only_token'] = this.readOnlyToken
  861. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  862. .stub()
  863. .callsArgWith(1, null, this.project, true)
  864. this.TokenAccessHandler.addReadOnlyUserToProject = sinon
  865. .stub()
  866. .callsArgWith(2, null)
  867. this.ProjectController.loadEditor = sinon.stub()
  868. return this.TokenAccessController.readOnlyToken(
  869. this.req,
  870. this.res,
  871. this.next
  872. )
  873. })
  874. it('should try to find a project with this token', function(done) {
  875. expect(
  876. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  877. ).to.equal(1)
  878. expect(
  879. this.TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(
  880. this.readOnlyToken
  881. )
  882. ).to.equal(true)
  883. return done()
  884. })
  885. it('should add the user to the project with read-only access', function(done) {
  886. expect(
  887. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  888. ).to.equal(1)
  889. expect(
  890. this.TokenAccessHandler.addReadOnlyUserToProject.calledWith(
  891. this.userId.toString(),
  892. this.projectId
  893. )
  894. ).to.equal(true)
  895. return done()
  896. })
  897. it('should pass control to loadEditor', function(done) {
  898. expect(this.req.params.Project_id).to.equal(this.projectId.toString())
  899. expect(this.ProjectController.loadEditor.callCount).to.equal(1)
  900. expect(
  901. this.ProjectController.loadEditor.calledWith(
  902. this.req,
  903. this.res,
  904. this.next
  905. )
  906. ).to.equal(true)
  907. return done()
  908. })
  909. })
  910. describe('when the user is already the owner', function() {
  911. beforeEach(function() {
  912. this.req = new MockRequest()
  913. this.res = new MockResponse()
  914. this.next = sinon.stub()
  915. this.req.params['read_only_token'] = this.readOnlyToken
  916. this.project.owner_ref = this.userId
  917. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  918. .stub()
  919. .callsArgWith(1, null, this.project, true)
  920. this.TokenAccessHandler.addReadOnlyUserToProject = sinon
  921. .stub()
  922. .callsArgWith(2, null)
  923. this.ProjectController.loadEditor = sinon.stub()
  924. return this.TokenAccessController.readOnlyToken(
  925. this.req,
  926. this.res,
  927. this.next
  928. )
  929. })
  930. it('should try to find a project with this token', function(done) {
  931. expect(
  932. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  933. ).to.equal(1)
  934. expect(
  935. this.TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(
  936. this.readOnlyToken
  937. )
  938. ).to.equal(true)
  939. return done()
  940. })
  941. it('should not add the user to the project with read-only access', function(done) {
  942. expect(
  943. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  944. ).to.equal(0)
  945. return done()
  946. })
  947. it('should pass control to loadEditor', function(done) {
  948. expect(this.req.params.Project_id).to.equal(this.projectId.toString())
  949. expect(this.ProjectController.loadEditor.callCount).to.equal(1)
  950. expect(
  951. this.ProjectController.loadEditor.calledWith(
  952. this.req,
  953. this.res,
  954. this.next
  955. )
  956. ).to.equal(true)
  957. return done()
  958. })
  959. })
  960. describe('when findProject produces an error', function() {
  961. beforeEach(function() {
  962. this.req = new MockRequest()
  963. this.res = new MockResponse()
  964. this.next = sinon.stub()
  965. this.req.params['read_only_token'] = this.readOnlyToken
  966. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  967. .stub()
  968. .callsArgWith(1, new Error('woops'))
  969. this.TokenAccessHandler.addReadOnlyUserToProject = sinon
  970. .stub()
  971. .callsArgWith(2, null)
  972. this.ProjectController.loadEditor = sinon.stub()
  973. return this.TokenAccessController.readOnlyToken(
  974. this.req,
  975. this.res,
  976. this.next
  977. )
  978. })
  979. it('should try to find a project with this token', function(done) {
  980. expect(
  981. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  982. ).to.equal(1)
  983. expect(
  984. this.TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(
  985. this.readOnlyToken
  986. )
  987. ).to.equal(true)
  988. return done()
  989. })
  990. it('should not add the user to the project with read-only access', function(done) {
  991. expect(
  992. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  993. ).to.equal(0)
  994. return done()
  995. })
  996. it('should not pass control to loadEditor', function(done) {
  997. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  998. expect(
  999. this.ProjectController.loadEditor.calledWith(
  1000. this.req,
  1001. this.res,
  1002. this.next
  1003. )
  1004. ).to.equal(false)
  1005. return done()
  1006. })
  1007. it('should call next with an error', function(done) {
  1008. expect(this.next.callCount).to.equal(1)
  1009. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  1010. return done()
  1011. })
  1012. })
  1013. })
  1014. describe('when findProject does not find a project', function() {
  1015. describe('when project does not exist', function() {
  1016. beforeEach(function() {
  1017. this.req = new MockRequest()
  1018. this.res = new MockResponse()
  1019. this.res.redirect = sinon.stub()
  1020. this.res.render = sinon.stub()
  1021. this.next = sinon.stub()
  1022. this.req.params['read_only_token'] = 'abcd'
  1023. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  1024. .stub()
  1025. .callsArgWith(1, null, null, false)
  1026. this.TokenAccessHandler.checkV1ProjectExported = sinon
  1027. .stub()
  1028. .callsArgWith(1, null, false)
  1029. this.TokenAccessController.readOnlyToken(
  1030. this.req,
  1031. this.res,
  1032. this.next
  1033. )
  1034. })
  1035. it('should not redirect to v1', function(done) {
  1036. expect(this.res.redirect.callCount).to.equal(0)
  1037. done()
  1038. })
  1039. it('should show project import page', function(done) {
  1040. expect(this.res.render.calledWith('project/v2-import')).to.equal(true)
  1041. done()
  1042. })
  1043. })
  1044. describe('when project was not exported from v1 but forcing import to v2', function() {
  1045. beforeEach(function() {
  1046. this.Features.hasFeature.returns(true)
  1047. this.req = new MockRequest()
  1048. this.res = new MockResponse()
  1049. this.res.render = sinon.stub()
  1050. this.next = sinon.stub()
  1051. this.req.params['read_only_token'] = 'abcd'
  1052. return (this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  1053. .stub()
  1054. .callsArgWith(1, null, null, false))
  1055. })
  1056. describe('with project name', function() {
  1057. beforeEach(function() {
  1058. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1059. exists: true,
  1060. exported: false,
  1061. has_owner: true,
  1062. name: 'A title',
  1063. has_assignment: false,
  1064. brand_info: null
  1065. })
  1066. return this.TokenAccessController.readOnlyToken(
  1067. this.req,
  1068. this.res,
  1069. this.next
  1070. )
  1071. })
  1072. it('should render v2-import page with name', function(done) {
  1073. expect(
  1074. this.res.render.calledWith('project/v2-import', {
  1075. projectId: 'abcd',
  1076. name: 'A title',
  1077. hasOwner: true,
  1078. hasAssignment: false,
  1079. brandInfo: null
  1080. })
  1081. ).to.equal(true)
  1082. return done()
  1083. })
  1084. })
  1085. describe('with project owner', function() {
  1086. beforeEach(function() {
  1087. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1088. exists: true,
  1089. exported: false,
  1090. has_owner: true,
  1091. name: 'A title',
  1092. has_assignment: false,
  1093. brand_info: null
  1094. })
  1095. return this.TokenAccessController.readOnlyToken(
  1096. this.req,
  1097. this.res,
  1098. this.next
  1099. )
  1100. })
  1101. it('should render v2-import page', function(done) {
  1102. expect(
  1103. this.res.render.calledWith('project/v2-import', {
  1104. projectId: 'abcd',
  1105. hasOwner: true,
  1106. name: 'A title',
  1107. hasAssignment: false,
  1108. brandInfo: null
  1109. })
  1110. ).to.equal(true)
  1111. return done()
  1112. })
  1113. })
  1114. describe('without project owner', function() {
  1115. beforeEach(function() {
  1116. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1117. exists: true,
  1118. exported: false,
  1119. has_owner: false,
  1120. name: 'A title',
  1121. has_assignment: false,
  1122. brand_info: null
  1123. })
  1124. return this.TokenAccessController.readOnlyToken(
  1125. this.req,
  1126. this.res,
  1127. this.next
  1128. )
  1129. })
  1130. it('should render v2-import page', function(done) {
  1131. expect(
  1132. this.res.render.calledWith('project/v2-import', {
  1133. projectId: 'abcd',
  1134. hasOwner: false,
  1135. name: 'A title',
  1136. hasAssignment: false,
  1137. brandInfo: null
  1138. })
  1139. ).to.equal(true)
  1140. return done()
  1141. })
  1142. })
  1143. describe('with assignment', function() {
  1144. beforeEach(function() {
  1145. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1146. exists: true,
  1147. exported: false,
  1148. has_owner: false,
  1149. name: 'A title',
  1150. has_assignment: true,
  1151. brand_info: null
  1152. })
  1153. return this.TokenAccessController.readOnlyToken(
  1154. this.req,
  1155. this.res,
  1156. this.next
  1157. )
  1158. })
  1159. it('should render v2-import page', function(done) {
  1160. expect(
  1161. this.res.render.calledWith('project/v2-import', {
  1162. projectId: 'abcd',
  1163. hasOwner: false,
  1164. name: 'A title',
  1165. hasAssignment: true,
  1166. brandInfo: null
  1167. })
  1168. ).to.equal(true)
  1169. return done()
  1170. })
  1171. })
  1172. describe('with brand info', function() {
  1173. beforeEach(function() {
  1174. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1175. exists: true,
  1176. exported: false,
  1177. has_owner: false,
  1178. name: 'A title',
  1179. has_assignment: false,
  1180. brand_info: 'f1000'
  1181. })
  1182. return this.TokenAccessController.readOnlyToken(
  1183. this.req,
  1184. this.res,
  1185. this.next
  1186. )
  1187. })
  1188. it('should render v2-import page', function(done) {
  1189. expect(
  1190. this.res.render.calledWith('project/v2-import', {
  1191. projectId: 'abcd',
  1192. hasOwner: false,
  1193. name: 'A title',
  1194. hasAssignment: false,
  1195. brandInfo: 'f1000'
  1196. })
  1197. ).to.equal(true)
  1198. return done()
  1199. })
  1200. })
  1201. })
  1202. describe('when project was exported from v1', function() {
  1203. beforeEach(function() {
  1204. this.req = new MockRequest()
  1205. this.res = new MockResponse()
  1206. this.res.redirect = sinon.stub()
  1207. this.next = sinon.stub()
  1208. this.req.params['read_only_token'] = 'abcd'
  1209. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  1210. .stub()
  1211. .callsArgWith(1, null, null, false)
  1212. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1213. allow: true,
  1214. exists: true,
  1215. exported: true
  1216. })
  1217. return this.TokenAccessController.readOnlyToken(
  1218. this.req,
  1219. this.res,
  1220. this.next
  1221. )
  1222. })
  1223. it('should call next with a not-found error', function(done) {
  1224. expect(this.next.callCount).to.equal(1)
  1225. return done()
  1226. })
  1227. })
  1228. describe('when token access is off, but user has higher access anyway', function() {
  1229. beforeEach(function() {
  1230. this.req = new MockRequest()
  1231. this.res = new MockResponse()
  1232. this.res.redirect = sinon.stub()
  1233. this.next = sinon.stub()
  1234. this.req.params['read_and_write_token'] = this.readAndWriteToken
  1235. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  1236. .stub()
  1237. .callsArgWith(1, null, null, true)
  1238. this.TokenAccessHandler.findProjectWithHigherAccess = sinon
  1239. .stub()
  1240. .callsArgWith(2, null, this.project)
  1241. this.TokenAccessHandler.addReadAndWriteUserToProject = sinon
  1242. .stub()
  1243. .callsArgWith(2, null)
  1244. this.ProjectController.loadEditor = sinon.stub()
  1245. return this.TokenAccessController.readAndWriteToken(
  1246. this.req,
  1247. this.res,
  1248. this.next
  1249. )
  1250. })
  1251. it('should try to find a project with this token', function(done) {
  1252. expect(
  1253. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  1254. ).to.equal(1)
  1255. expect(
  1256. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  1257. this.readAndWriteToken
  1258. )
  1259. ).to.equal(true)
  1260. return done()
  1261. })
  1262. it('should check if user has higher access to the token project', function(done) {
  1263. expect(
  1264. this.TokenAccessHandler.findProjectWithHigherAccess.callCount
  1265. ).to.equal(1)
  1266. return done()
  1267. })
  1268. it('should not add the user to the project with read-write access', function(done) {
  1269. expect(
  1270. this.TokenAccessHandler.addReadAndWriteUserToProject.callCount
  1271. ).to.equal(0)
  1272. return done()
  1273. })
  1274. it('should not pass control to loadEditor', function(done) {
  1275. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  1276. expect(
  1277. this.ProjectController.loadEditor.calledWith(
  1278. this.req,
  1279. this.res,
  1280. this.next
  1281. )
  1282. ).to.equal(false)
  1283. return done()
  1284. })
  1285. it('should not call next with a not-found error', function(done) {
  1286. expect(this.next.callCount).to.equal(0)
  1287. return done()
  1288. })
  1289. it('should redirect to the canonical project url', function(done) {
  1290. expect(this.res.redirect.callCount).to.equal(1)
  1291. expect(
  1292. this.res.redirect.calledWith(302, `/project/${this.project._id}`)
  1293. ).to.equal(true)
  1294. return done()
  1295. })
  1296. })
  1297. describe('when higher access is not available', function() {
  1298. beforeEach(function() {
  1299. this.req = new MockRequest()
  1300. this.res = new MockResponse()
  1301. this.next = sinon.stub()
  1302. this.req.params['read_and_write_token'] = this.readAndWriteToken
  1303. this.TokenAccessHandler.findProjectWithReadAndWriteToken = sinon
  1304. .stub()
  1305. .callsArgWith(1, null, null, true)
  1306. this.TokenAccessHandler.findProjectWithHigherAccess = sinon
  1307. .stub()
  1308. .callsArgWith(2, null, null)
  1309. this.TokenAccessHandler.addReadOnlyUserToProject = sinon
  1310. .stub()
  1311. .callsArgWith(2, null)
  1312. this.ProjectController.loadEditor = sinon.stub()
  1313. return this.TokenAccessController.readAndWriteToken(
  1314. this.req,
  1315. this.res,
  1316. this.next
  1317. )
  1318. })
  1319. it('should try to find a project with this token', function(done) {
  1320. expect(
  1321. this.TokenAccessHandler.findProjectWithReadAndWriteToken.callCount
  1322. ).to.equal(1)
  1323. expect(
  1324. this.TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  1325. this.readAndWriteToken
  1326. )
  1327. ).to.equal(true)
  1328. return done()
  1329. })
  1330. it('should check if user has higher access to the token project', function(done) {
  1331. expect(
  1332. this.TokenAccessHandler.findProjectWithHigherAccess.callCount
  1333. ).to.equal(1)
  1334. return done()
  1335. })
  1336. it('should not add the user to the project with read-write access', function(done) {
  1337. expect(
  1338. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  1339. ).to.equal(0)
  1340. return done()
  1341. })
  1342. it('should not pass control to loadEditor', function(done) {
  1343. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  1344. expect(
  1345. this.ProjectController.loadEditor.calledWith(
  1346. this.req,
  1347. this.res,
  1348. this.next
  1349. )
  1350. ).to.equal(false)
  1351. return done()
  1352. })
  1353. it('should call next with a not-found error', function(done) {
  1354. expect(this.next.callCount).to.equal(1)
  1355. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  1356. return done()
  1357. })
  1358. })
  1359. })
  1360. describe('when adding user to project produces an error', function() {
  1361. beforeEach(function() {
  1362. this.req = new MockRequest()
  1363. this.res = new MockResponse()
  1364. this.next = sinon.stub()
  1365. this.req.params['read_only_token'] = this.readOnlyToken
  1366. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  1367. .stub()
  1368. .callsArgWith(1, null, this.project, true)
  1369. this.TokenAccessHandler.addReadOnlyUserToProject = sinon
  1370. .stub()
  1371. .callsArgWith(2, new Error('woops'))
  1372. this.ProjectController.loadEditor = sinon.stub()
  1373. return this.TokenAccessController.readOnlyToken(
  1374. this.req,
  1375. this.res,
  1376. this.next
  1377. )
  1378. })
  1379. it('should try to find a project with this token', function(done) {
  1380. expect(
  1381. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  1382. ).to.equal(1)
  1383. expect(
  1384. this.TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(
  1385. this.readOnlyToken
  1386. )
  1387. ).to.equal(true)
  1388. return done()
  1389. })
  1390. it('should add the user to the project with read-only access', function(done) {
  1391. expect(
  1392. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  1393. ).to.equal(1)
  1394. expect(
  1395. this.TokenAccessHandler.addReadOnlyUserToProject.calledWith(
  1396. this.userId.toString(),
  1397. this.projectId
  1398. )
  1399. ).to.equal(true)
  1400. return done()
  1401. })
  1402. it('should not pass control to loadEditor', function(done) {
  1403. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  1404. expect(
  1405. this.ProjectController.loadEditor.calledWith(
  1406. this.req,
  1407. this.res,
  1408. this.next
  1409. )
  1410. ).to.equal(false)
  1411. return done()
  1412. })
  1413. it('should call next with an error', function(done) {
  1414. expect(this.next.callCount).to.equal(1)
  1415. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  1416. return done()
  1417. })
  1418. })
  1419. describe('anonymous', function() {
  1420. beforeEach(function() {
  1421. this.AuthenticationController.getLoggedInUserId = sinon
  1422. .stub()
  1423. .returns(null)
  1424. return (this.TokenAccessHandler.grantSessionTokenAccess = sinon.stub())
  1425. })
  1426. describe('when all goes well', function() {
  1427. beforeEach(function() {
  1428. this.req = new MockRequest()
  1429. this.res = new MockResponse()
  1430. this.next = sinon.stub()
  1431. this.req.params['read_only_token'] = this.readOnlyToken
  1432. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  1433. .stub()
  1434. .callsArgWith(1, null, this.project, true)
  1435. this.TokenAccessHandler.addReadOnlyUserToProject = sinon
  1436. .stub()
  1437. .callsArgWith(2, null)
  1438. this.ProjectController.loadEditor = sinon.stub()
  1439. return this.TokenAccessController.readOnlyToken(
  1440. this.req,
  1441. this.res,
  1442. this.next
  1443. )
  1444. })
  1445. it('should try to find a project with this token', function(done) {
  1446. expect(
  1447. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  1448. ).to.equal(1)
  1449. expect(
  1450. this.TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(
  1451. this.readOnlyToken
  1452. )
  1453. ).to.equal(true)
  1454. return done()
  1455. })
  1456. it('should give the user session read-only access', function(done) {
  1457. expect(
  1458. this.TokenAccessHandler.grantSessionTokenAccess.callCount
  1459. ).to.equal(1)
  1460. expect(
  1461. this.TokenAccessHandler.grantSessionTokenAccess.calledWith(
  1462. this.req,
  1463. this.projectId,
  1464. this.readOnlyToken
  1465. )
  1466. ).to.equal(true)
  1467. return done()
  1468. })
  1469. it('should not add the user to the project with read-only access', function(done) {
  1470. expect(
  1471. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  1472. ).to.equal(0)
  1473. return done()
  1474. })
  1475. it('should pass control to loadEditor', function(done) {
  1476. expect(this.req.params.Project_id).to.equal(this.projectId.toString())
  1477. expect(this.req._anonymousAccessToken).to.equal(this.readOnlyToken)
  1478. expect(this.ProjectController.loadEditor.callCount).to.equal(1)
  1479. expect(
  1480. this.ProjectController.loadEditor.calledWith(
  1481. this.req,
  1482. this.res,
  1483. this.next
  1484. )
  1485. ).to.equal(true)
  1486. return done()
  1487. })
  1488. })
  1489. describe('when findProject produces an error', function() {
  1490. beforeEach(function() {
  1491. this.req = new MockRequest()
  1492. this.res = new MockResponse()
  1493. this.next = sinon.stub()
  1494. this.req.params['read_only_token'] = this.readOnlyToken
  1495. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  1496. .stub()
  1497. .callsArgWith(1, new Error('woops'))
  1498. this.TokenAccessHandler.addReadOnlyUserToProject = sinon
  1499. .stub()
  1500. .callsArgWith(2, null)
  1501. this.ProjectController.loadEditor = sinon.stub()
  1502. return this.TokenAccessController.readOnlyToken(
  1503. this.req,
  1504. this.res,
  1505. this.next
  1506. )
  1507. })
  1508. it('should try to find a project with this token', function(done) {
  1509. expect(
  1510. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  1511. ).to.equal(1)
  1512. expect(
  1513. this.TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(
  1514. this.readOnlyToken
  1515. )
  1516. ).to.equal(true)
  1517. return done()
  1518. })
  1519. it('should not give the user session read-only access', function(done) {
  1520. expect(
  1521. this.TokenAccessHandler.grantSessionTokenAccess.callCount
  1522. ).to.equal(0)
  1523. return done()
  1524. })
  1525. it('should not add the user to the project with read-only access', function(done) {
  1526. expect(
  1527. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  1528. ).to.equal(0)
  1529. return done()
  1530. })
  1531. it('should not pass control to loadEditor', function(done) {
  1532. expect(this.ProjectController.loadEditor.callCount).to.equal(0)
  1533. expect(
  1534. this.ProjectController.loadEditor.calledWith(
  1535. this.req,
  1536. this.res,
  1537. this.next
  1538. )
  1539. ).to.equal(false)
  1540. return done()
  1541. })
  1542. it('should call next with an error', function(done) {
  1543. expect(this.next.callCount).to.equal(1)
  1544. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  1545. return done()
  1546. })
  1547. })
  1548. describe('when findProject does not find a project', function() {
  1549. beforeEach(function() {
  1550. this.req = new MockRequest()
  1551. this.res = new MockResponse()
  1552. this.res.redirect = sinon.stub()
  1553. this.next = sinon.stub()
  1554. this.req.params['read_only_token'] = this.readOnlyToken
  1555. this.AuthenticationController.getLoggedInUserId = sinon
  1556. .stub()
  1557. .returns(this.userId.toString())
  1558. this.TokenAccessHandler.findProjectWithReadOnlyToken = sinon
  1559. .stub()
  1560. .callsArgWith(1, null, false)
  1561. return (this.TokenAccessHandler.addReadOnlyUserToProject = sinon.stub())
  1562. })
  1563. describe('when project does not exist', function() {
  1564. beforeEach(function() {
  1565. return this.TokenAccessController.readOnlyToken(
  1566. this.req,
  1567. this.res,
  1568. this.next
  1569. )
  1570. })
  1571. it('should try to find a project with this token', function(done) {
  1572. expect(
  1573. this.TokenAccessHandler.findProjectWithReadOnlyToken.callCount
  1574. ).to.equal(1)
  1575. expect(
  1576. this.TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(
  1577. this.readOnlyToken
  1578. )
  1579. ).to.equal(true)
  1580. return done()
  1581. })
  1582. it('should not give the user session read-only access', function(done) {
  1583. expect(
  1584. this.TokenAccessHandler.grantSessionTokenAccess.callCount
  1585. ).to.equal(0)
  1586. return done()
  1587. })
  1588. it('should not add the user to the project with read-only access', function(done) {
  1589. expect(
  1590. this.TokenAccessHandler.addReadOnlyUserToProject.callCount
  1591. ).to.equal(0)
  1592. return done()
  1593. })
  1594. })
  1595. describe('when project was exported to v2', function() {
  1596. beforeEach(function() {
  1597. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1598. exists: true,
  1599. exported: true
  1600. })
  1601. return this.TokenAccessController.readOnlyToken(
  1602. this.req,
  1603. this.res,
  1604. this.next
  1605. )
  1606. })
  1607. it('should call next with not found error', function(done) {
  1608. expect(this.next.callCount).to.equal(1)
  1609. expect(this.next.calledWith(new Errors.NotFoundError())).to.equal(
  1610. true
  1611. )
  1612. return done()
  1613. })
  1614. })
  1615. describe('when project was not exported to v2', function() {
  1616. beforeEach(function() {
  1617. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1618. exists: true,
  1619. exported: false
  1620. })
  1621. this.res.render = sinon.stub()
  1622. this.TokenAccessController.readOnlyToken(
  1623. this.req,
  1624. this.res,
  1625. this.next
  1626. )
  1627. })
  1628. it('should not redirect to v1', function(done) {
  1629. expect(this.res.redirect.callCount).to.equal(0)
  1630. done()
  1631. })
  1632. it('should show project import page', function(done) {
  1633. expect(this.res.render.calledWith('project/v2-import')).to.equal(
  1634. true
  1635. )
  1636. done()
  1637. })
  1638. })
  1639. describe('when project does not exist on v1', function() {
  1640. beforeEach(function() {
  1641. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1642. exists: false,
  1643. exported: false
  1644. })
  1645. return this.TokenAccessController.readOnlyToken(
  1646. this.req,
  1647. this.res,
  1648. this.next
  1649. )
  1650. })
  1651. it('should call next with not found error', function(done) {
  1652. expect(this.next.callCount).to.equal(1)
  1653. expect(this.next.calledWith(new Errors.NotFoundError())).to.equal(
  1654. true
  1655. )
  1656. return done()
  1657. })
  1658. })
  1659. describe('anonymous user', function() {
  1660. beforeEach(function() {
  1661. return (this.AuthenticationController.getLoggedInUserId = sinon
  1662. .stub()
  1663. .returns(null))
  1664. })
  1665. describe('when project was not exported to v2', function() {
  1666. beforeEach(function() {
  1667. this.TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
  1668. exists: true,
  1669. exported: false
  1670. })
  1671. this.res.render = sinon.stub()
  1672. this.TokenAccessController.readOnlyToken(
  1673. this.req,
  1674. this.res,
  1675. this.next
  1676. )
  1677. })
  1678. it('should not redirect to v1', function(done) {
  1679. expect(this.res.redirect.callCount).to.equal(0)
  1680. done()
  1681. })
  1682. it('should show project import page', function(done) {
  1683. expect(this.res.render.calledWith('project/v2-import')).to.equal(
  1684. true
  1685. )
  1686. done()
  1687. })
  1688. })
  1689. describe('force-import-to-v2 flag is on', function() {
  1690. beforeEach(function() {
  1691. this.res.render = sinon.stub()
  1692. return this.Features.hasFeature.returns(true)
  1693. })
  1694. describe('when project was not exported to v2', function() {
  1695. beforeEach(function() {
  1696. this.TokenAccessHandler.getV1DocInfo = sinon
  1697. .stub()
  1698. .yields(null, {
  1699. exists: true,
  1700. exported: false
  1701. })
  1702. return this.TokenAccessController.readOnlyToken(
  1703. this.req,
  1704. this.res,
  1705. this.next
  1706. )
  1707. })
  1708. it('should render anonymous import status page', function(done) {
  1709. expect(this.res.render.callCount).to.equal(1)
  1710. expect(
  1711. this.res.render.calledWith('project/v2-import', {
  1712. loginRedirect: `/read/${this.readOnlyToken}`
  1713. })
  1714. ).to.equal(true)
  1715. return done()
  1716. })
  1717. })
  1718. })
  1719. })
  1720. })
  1721. })
  1722. })
  1723. })