TokenAccessControllerTests.js 62 KB

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