TokenAccessControllerTests.coffee 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/TokenAccess/TokenAccessController"
  7. expect = require("chai").expect
  8. ObjectId = require("mongojs").ObjectId
  9. MockRequest = require('../helpers/MockRequest')
  10. MockResponse = require('../helpers/MockResponse')
  11. Errors = require "../../../../app/js/Features/Errors/Errors.js"
  12. describe "TokenAccessController", ->
  13. beforeEach ->
  14. @readOnlyToken = 'somereadonlytoken'
  15. @readAndWriteToken = '42somereadandwritetoken'
  16. @projectId = ObjectId()
  17. @ownerId = 'owner'
  18. @project =
  19. _id: @projectId
  20. publicAccesLevel: 'tokenBased'
  21. tokens:
  22. readOnly: @readOnlyToken
  23. readAndWrite: @readAndWriteToken
  24. owner_ref: @ownerId
  25. @userId = ObjectId()
  26. @TokenAccessController = SandboxedModule.require modulePath, requires:
  27. '../Project/ProjectController': @ProjectController = {}
  28. '../Authentication/AuthenticationController': @AuthenticationController = {}
  29. './TokenAccessHandler': @TokenAccessHandler = {}
  30. 'logger-sharelatex': {log: sinon.stub(), err: sinon.stub()}
  31. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId.toString())
  32. describe 'readAndWriteToken', ->
  33. beforeEach ->
  34. describe 'when all goes well', ->
  35. beforeEach ->
  36. @req = new MockRequest()
  37. @res = new MockResponse()
  38. @next = sinon.stub()
  39. @req.params['read_and_write_token'] = @readAndWriteToken
  40. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  41. .callsArgWith(1, null, @project)
  42. @TokenAccessHandler.addReadAndWriteUserToProject = sinon.stub()
  43. .callsArgWith(2, null)
  44. @ProjectController.loadEditor = sinon.stub()
  45. @TokenAccessController.readAndWriteToken @req, @res, @next
  46. it 'should try to find a project with this token', (done) ->
  47. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  48. .to.equal 1
  49. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(@readAndWriteToken))
  50. .to.equal true
  51. done()
  52. it 'should add the user to the project with read-write access', (done) ->
  53. expect(@TokenAccessHandler.addReadAndWriteUserToProject.callCount)
  54. .to.equal 1
  55. expect(@TokenAccessHandler.addReadAndWriteUserToProject.calledWith(
  56. @userId.toString(), @projectId
  57. ))
  58. .to.equal true
  59. done()
  60. it 'should pass control to loadEditor', (done) ->
  61. expect(@req.params.Project_id).to.equal @projectId.toString()
  62. expect(@ProjectController.loadEditor.callCount).to.equal 1
  63. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal true
  64. done()
  65. describe 'when the user is already the owner', ->
  66. beforeEach ->
  67. @req = new MockRequest()
  68. @res = new MockResponse()
  69. @next = sinon.stub()
  70. @req.params['read_and_write_token'] = @readAndWriteToken
  71. @project.owner_ref = @userId
  72. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  73. .callsArgWith(1, null, @project)
  74. @TokenAccessHandler.addReadAndWriteUserToProject = sinon.stub()
  75. .callsArgWith(2, null)
  76. @ProjectController.loadEditor = sinon.stub()
  77. @TokenAccessController.readAndWriteToken @req, @res, @next
  78. it 'should try to find a project with this token', (done) ->
  79. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  80. .to.equal 1
  81. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(@readAndWriteToken))
  82. .to.equal true
  83. done()
  84. it 'should not add the user to the project with read-write access', (done) ->
  85. expect(@TokenAccessHandler.addReadAndWriteUserToProject.callCount)
  86. .to.equal 0
  87. done()
  88. it 'should pass control to loadEditor', (done) ->
  89. expect(@req.params.Project_id).to.equal @projectId.toString()
  90. expect(@ProjectController.loadEditor.callCount).to.equal 1
  91. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal true
  92. done()
  93. describe 'when findProject produces an error', ->
  94. beforeEach ->
  95. @req = new MockRequest()
  96. @res = new MockResponse()
  97. @next = sinon.stub()
  98. @req.params['read_and_write_token'] = @readAndWriteToken
  99. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  100. .callsArgWith(1, new Error('woops'))
  101. @TokenAccessHandler.addReadAndWriteUserToProject = sinon.stub()
  102. .callsArgWith(2, null)
  103. @ProjectController.loadEditor = sinon.stub()
  104. @TokenAccessController.readAndWriteToken @req, @res, @next
  105. it 'should try to find a project with this token', (done) ->
  106. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  107. .to.equal 1
  108. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(@readAndWriteToken))
  109. .to.equal true
  110. done()
  111. it 'should not add the user to the project with read-write access', (done) ->
  112. expect(@TokenAccessHandler.addReadAndWriteUserToProject.callCount)
  113. .to.equal 0
  114. done()
  115. it 'should not pass control to loadEditor', (done) ->
  116. expect(@ProjectController.loadEditor.callCount).to.equal 0
  117. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  118. done()
  119. it 'should call next with an error', (done) ->
  120. expect(@next.callCount).to.equal 1
  121. expect(@next.lastCall.args[0]).to.be.instanceof Error
  122. done()
  123. describe 'when findProject does not find a project', ->
  124. beforeEach ->
  125. describe 'when token access is off, but user has higher access anyway', ->
  126. beforeEach ->
  127. @req = new MockRequest()
  128. @res = new MockResponse()
  129. @res.redirect = sinon.stub()
  130. @next = sinon.stub()
  131. @req.params['read_and_write_token'] = @readAndWriteToken
  132. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  133. .callsArgWith(1, null, null)
  134. @TokenAccessHandler.findProjectWithHigherAccess =
  135. sinon.stub()
  136. .callsArgWith(2, null, @project)
  137. @TokenAccessHandler.addReadAndWriteUserToProject = sinon.stub()
  138. .callsArgWith(2, null)
  139. @ProjectController.loadEditor = sinon.stub()
  140. @TokenAccessController.readAndWriteToken @req, @res, @next
  141. it 'should try to find a project with this token', (done) ->
  142. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  143. .to.equal 1
  144. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(@readAndWriteToken))
  145. .to.equal true
  146. done()
  147. it 'should check if user has higher access to the token project', (done) ->
  148. expect(
  149. @TokenAccessHandler.findProjectWithHigherAccess.callCount
  150. ).to.equal 1
  151. done()
  152. it 'should not add the user to the project with read-write access', (done) ->
  153. expect(@TokenAccessHandler.addReadAndWriteUserToProject.callCount)
  154. .to.equal 0
  155. done()
  156. it 'should not pass control to loadEditor', (done) ->
  157. expect(@ProjectController.loadEditor.callCount).to.equal 0
  158. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  159. done()
  160. it 'should not call next with a not-found error', (done) ->
  161. expect(@next.callCount).to.equal 0
  162. done()
  163. it 'should redirect to the canonical project url', (done) ->
  164. expect(@res.redirect.callCount).to.equal 1
  165. expect(@res.redirect.calledWith(302, "/project/#{@project._id}")).to.equal true
  166. done()
  167. describe 'when higher access is not available', ->
  168. beforeEach ->
  169. @req = new MockRequest()
  170. @res = new MockResponse()
  171. @next = sinon.stub()
  172. @req.params['read_and_write_token'] = @readAndWriteToken
  173. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  174. .callsArgWith(1, null, null)
  175. @TokenAccessHandler.findProjectWithHigherAccess =
  176. sinon.stub()
  177. .callsArgWith(2, null, null)
  178. @TokenAccessHandler.addReadAndWriteUserToProject = sinon.stub()
  179. .callsArgWith(2, null)
  180. @ProjectController.loadEditor = sinon.stub()
  181. @TokenAccessController.readAndWriteToken @req, @res, @next
  182. it 'should try to find a project with this token', (done) ->
  183. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  184. .to.equal 1
  185. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  186. @readAndWriteToken
  187. )).to.equal true
  188. done()
  189. it 'should check if user has higher access to the token project', (done) ->
  190. expect(
  191. @TokenAccessHandler.findProjectWithHigherAccess.callCount
  192. ).to.equal 1
  193. done()
  194. it 'should not add the user to the project with read-write access', (done) ->
  195. expect(@TokenAccessHandler.addReadAndWriteUserToProject.callCount)
  196. .to.equal 0
  197. done()
  198. it 'should not pass control to loadEditor', (done) ->
  199. expect(@ProjectController.loadEditor.callCount).to.equal 0
  200. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  201. done()
  202. it 'should call next with a not-found error', (done) ->
  203. expect(@next.callCount).to.equal 1
  204. expect(@next.lastCall.args[0]).to.be.instanceof Error
  205. done()
  206. describe 'when adding user to project produces an error', ->
  207. beforeEach ->
  208. @req = new MockRequest()
  209. @res = new MockResponse()
  210. @next = sinon.stub()
  211. @req.params['read_and_write_token'] = @readAndWriteToken
  212. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  213. .callsArgWith(1, null, @project)
  214. @TokenAccessHandler.addReadAndWriteUserToProject = sinon.stub()
  215. .callsArgWith(2, new Error('woops'))
  216. @ProjectController.loadEditor = sinon.stub()
  217. @TokenAccessController.readAndWriteToken @req, @res, @next
  218. it 'should try to find a project with this token', (done) ->
  219. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  220. .to.equal 1
  221. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(@readAndWriteToken))
  222. .to.equal true
  223. done()
  224. it 'should add the user to the project with read-write access', (done) ->
  225. expect(@TokenAccessHandler.addReadAndWriteUserToProject.callCount)
  226. .to.equal 1
  227. expect(@TokenAccessHandler.addReadAndWriteUserToProject.calledWith(
  228. @userId.toString(), @projectId
  229. ))
  230. .to.equal true
  231. done()
  232. it 'should not pass control to loadEditor', (done) ->
  233. expect(@ProjectController.loadEditor.callCount).to.equal 0
  234. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  235. done()
  236. it 'should call next with an error', (done) ->
  237. expect(@next.callCount).to.equal 1
  238. expect(@next.lastCall.args[0]).to.be.instanceof Error
  239. done()
  240. describe 'readOnlyToken', ->
  241. beforeEach ->
  242. describe 'with a user', ->
  243. beforeEach ->
  244. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId.toString())
  245. describe 'when all goes well', ->
  246. beforeEach ->
  247. @req = new MockRequest()
  248. @res = new MockResponse()
  249. @next = sinon.stub()
  250. @req.params['read_only_token'] = @readOnlyToken
  251. @TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
  252. .callsArgWith(1, null, @project)
  253. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  254. .callsArgWith(2, null)
  255. @ProjectController.loadEditor = sinon.stub()
  256. @TokenAccessController.readOnlyToken @req, @res, @next
  257. it 'should try to find a project with this token', (done) ->
  258. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.callCount)
  259. .to.equal 1
  260. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(@readOnlyToken))
  261. .to.equal true
  262. done()
  263. it 'should add the user to the project with read-only access', (done) ->
  264. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  265. .to.equal 1
  266. expect(@TokenAccessHandler.addReadOnlyUserToProject.calledWith(
  267. @userId.toString(), @projectId
  268. ))
  269. .to.equal true
  270. done()
  271. it 'should pass control to loadEditor', (done) ->
  272. expect(@req.params.Project_id).to.equal @projectId.toString()
  273. expect(@ProjectController.loadEditor.callCount).to.equal 1
  274. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal true
  275. done()
  276. describe 'when the user is already the owner', ->
  277. beforeEach ->
  278. @req = new MockRequest()
  279. @res = new MockResponse()
  280. @next = sinon.stub()
  281. @req.params['read_only_token'] = @readOnlyToken
  282. @project.owner_ref = @userId
  283. @TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
  284. .callsArgWith(1, null, @project)
  285. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  286. .callsArgWith(2, null)
  287. @ProjectController.loadEditor = sinon.stub()
  288. @TokenAccessController.readOnlyToken @req, @res, @next
  289. it 'should try to find a project with this token', (done) ->
  290. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.callCount)
  291. .to.equal 1
  292. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(@readOnlyToken))
  293. .to.equal true
  294. done()
  295. it 'should not add the user to the project with read-only access', (done) ->
  296. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  297. .to.equal 0
  298. done()
  299. it 'should pass control to loadEditor', (done) ->
  300. expect(@req.params.Project_id).to.equal @projectId.toString()
  301. expect(@ProjectController.loadEditor.callCount).to.equal 1
  302. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal true
  303. done()
  304. describe 'when findProject produces an error', ->
  305. beforeEach ->
  306. @req = new MockRequest()
  307. @res = new MockResponse()
  308. @next = sinon.stub()
  309. @req.params['read_only_token'] = @readOnlyToken
  310. @TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
  311. .callsArgWith(1, new Error('woops'))
  312. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  313. .callsArgWith(2, null)
  314. @ProjectController.loadEditor = sinon.stub()
  315. @TokenAccessController.readOnlyToken @req, @res, @next
  316. it 'should try to find a project with this token', (done) ->
  317. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.callCount)
  318. .to.equal 1
  319. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(@readOnlyToken))
  320. .to.equal true
  321. done()
  322. it 'should not add the user to the project with read-only access', (done) ->
  323. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  324. .to.equal 0
  325. done()
  326. it 'should not pass control to loadEditor', (done) ->
  327. expect(@ProjectController.loadEditor.callCount).to.equal 0
  328. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  329. done()
  330. it 'should call next with an error', (done) ->
  331. expect(@next.callCount).to.equal 1
  332. expect(@next.lastCall.args[0]).to.be.instanceof Error
  333. done()
  334. ##
  335. describe 'when findProject does not find a project', ->
  336. beforeEach ->
  337. describe 'when token access is off, but user has higher access anyway', ->
  338. beforeEach ->
  339. @req = new MockRequest()
  340. @res = new MockResponse()
  341. @res.redirect = sinon.stub()
  342. @next = sinon.stub()
  343. @req.params['read_and_write_token'] = @readAndWriteToken
  344. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  345. .callsArgWith(1, null, null)
  346. @TokenAccessHandler.findProjectWithHigherAccess =
  347. sinon.stub()
  348. .callsArgWith(2, null, @project)
  349. @TokenAccessHandler.addReadAndWriteUserToProject = sinon.stub()
  350. .callsArgWith(2, null)
  351. @ProjectController.loadEditor = sinon.stub()
  352. @TokenAccessController.readAndWriteToken @req, @res, @next
  353. it 'should try to find a project with this token', (done) ->
  354. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  355. .to.equal 1
  356. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(@readAndWriteToken))
  357. .to.equal true
  358. done()
  359. it 'should check if user has higher access to the token project', (done) ->
  360. expect(
  361. @TokenAccessHandler.findProjectWithHigherAccess.callCount
  362. ).to.equal 1
  363. done()
  364. it 'should not add the user to the project with read-write access', (done) ->
  365. expect(@TokenAccessHandler.addReadAndWriteUserToProject.callCount)
  366. .to.equal 0
  367. done()
  368. it 'should not pass control to loadEditor', (done) ->
  369. expect(@ProjectController.loadEditor.callCount).to.equal 0
  370. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  371. done()
  372. it 'should not call next with a not-found error', (done) ->
  373. expect(@next.callCount).to.equal 0
  374. done()
  375. it 'should redirect to the canonical project url', (done) ->
  376. expect(@res.redirect.callCount).to.equal 1
  377. expect(@res.redirect.calledWith(302, "/project/#{@project._id}")).to.equal true
  378. done()
  379. describe 'when higher access is not available', ->
  380. beforeEach ->
  381. @req = new MockRequest()
  382. @res = new MockResponse()
  383. @next = sinon.stub()
  384. @req.params['read_and_write_token'] = @readAndWriteToken
  385. @TokenAccessHandler.findProjectWithReadAndWriteToken = sinon.stub()
  386. .callsArgWith(1, null, null)
  387. @TokenAccessHandler.findProjectWithHigherAccess =
  388. sinon.stub()
  389. .callsArgWith(2, null, null)
  390. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  391. .callsArgWith(2, null)
  392. @ProjectController.loadEditor = sinon.stub()
  393. @TokenAccessController.readAndWriteToken @req, @res, @next
  394. it 'should try to find a project with this token', (done) ->
  395. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.callCount)
  396. .to.equal 1
  397. expect(@TokenAccessHandler.findProjectWithReadAndWriteToken.calledWith(
  398. @readAndWriteToken
  399. )).to.equal true
  400. done()
  401. it 'should check if user has higher access to the token project', (done) ->
  402. expect(
  403. @TokenAccessHandler.findProjectWithHigherAccess.callCount
  404. ).to.equal 1
  405. done()
  406. it 'should not add the user to the project with read-write access', (done) ->
  407. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  408. .to.equal 0
  409. done()
  410. it 'should not pass control to loadEditor', (done) ->
  411. expect(@ProjectController.loadEditor.callCount).to.equal 0
  412. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  413. done()
  414. it 'should call next with a not-found error', (done) ->
  415. expect(@next.callCount).to.equal 1
  416. expect(@next.lastCall.args[0]).to.be.instanceof Error
  417. done()
  418. describe 'when adding user to project produces an error', ->
  419. beforeEach ->
  420. @req = new MockRequest()
  421. @res = new MockResponse()
  422. @next = sinon.stub()
  423. @req.params['read_only_token'] = @readOnlyToken
  424. @TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
  425. .callsArgWith(1, null, @project)
  426. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  427. .callsArgWith(2, new Error('woops'))
  428. @ProjectController.loadEditor = sinon.stub()
  429. @TokenAccessController.readOnlyToken @req, @res, @next
  430. it 'should try to find a project with this token', (done) ->
  431. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.callCount)
  432. .to.equal 1
  433. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(@readOnlyToken))
  434. .to.equal true
  435. done()
  436. it 'should add the user to the project with read-only access', (done) ->
  437. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  438. .to.equal 1
  439. expect(@TokenAccessHandler.addReadOnlyUserToProject.calledWith(
  440. @userId.toString(), @projectId
  441. ))
  442. .to.equal true
  443. done()
  444. it 'should not pass control to loadEditor', (done) ->
  445. expect(@ProjectController.loadEditor.callCount).to.equal 0
  446. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  447. done()
  448. it 'should call next with an error', (done) ->
  449. expect(@next.callCount).to.equal 1
  450. expect(@next.lastCall.args[0]).to.be.instanceof Error
  451. done()
  452. describe 'anonymous', ->
  453. beforeEach ->
  454. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(null)
  455. @TokenAccessHandler.grantSessionTokenAccess = sinon.stub()
  456. describe 'when all goes well', ->
  457. beforeEach ->
  458. @req = new MockRequest()
  459. @res = new MockResponse()
  460. @next = sinon.stub()
  461. @req.params['read_only_token'] = @readOnlyToken
  462. @TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
  463. .callsArgWith(1, null, @project)
  464. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  465. .callsArgWith(2, null)
  466. @ProjectController.loadEditor = sinon.stub()
  467. @TokenAccessController.readOnlyToken @req, @res, @next
  468. it 'should try to find a project with this token', (done) ->
  469. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.callCount)
  470. .to.equal 1
  471. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(@readOnlyToken))
  472. .to.equal true
  473. done()
  474. it 'should give the user session read-only access', (done) ->
  475. expect(@TokenAccessHandler.grantSessionTokenAccess.callCount)
  476. .to.equal 1
  477. expect(@TokenAccessHandler.grantSessionTokenAccess.calledWith(
  478. @req, @projectId, @readOnlyToken
  479. ))
  480. .to.equal true
  481. done()
  482. it 'should not add the user to the project with read-only access', (done) ->
  483. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  484. .to.equal 0
  485. done()
  486. it 'should pass control to loadEditor', (done) ->
  487. expect(@req.params.Project_id).to.equal @projectId.toString()
  488. expect(@req._anonymousAccessToken).to.equal @readOnlyToken
  489. expect(@ProjectController.loadEditor.callCount).to.equal 1
  490. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal true
  491. done()
  492. describe 'when findProject produces an error', ->
  493. beforeEach ->
  494. @req = new MockRequest()
  495. @res = new MockResponse()
  496. @next = sinon.stub()
  497. @req.params['read_only_token'] = @readOnlyToken
  498. @TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
  499. .callsArgWith(1, new Error('woops'))
  500. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  501. .callsArgWith(2, null)
  502. @ProjectController.loadEditor = sinon.stub()
  503. @TokenAccessController.readOnlyToken @req, @res, @next
  504. it 'should try to find a project with this token', (done) ->
  505. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.callCount)
  506. .to.equal 1
  507. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(@readOnlyToken))
  508. .to.equal true
  509. done()
  510. it 'should not give the user session read-only access', (done) ->
  511. expect(@TokenAccessHandler.grantSessionTokenAccess.callCount)
  512. .to.equal 0
  513. done()
  514. it 'should not add the user to the project with read-only access', (done) ->
  515. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  516. .to.equal 0
  517. done()
  518. it 'should not pass control to loadEditor', (done) ->
  519. expect(@ProjectController.loadEditor.callCount).to.equal 0
  520. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  521. done()
  522. it 'should call next with an error', (done) ->
  523. expect(@next.callCount).to.equal 1
  524. expect(@next.lastCall.args[0]).to.be.instanceof Error
  525. done()
  526. describe 'when findProject does not find a project', ->
  527. beforeEach ->
  528. @req = new MockRequest()
  529. @res = new MockResponse()
  530. @next = sinon.stub()
  531. @req.params['read_only_token'] = @readOnlyToken
  532. @TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
  533. .callsArgWith(1, null, null)
  534. @TokenAccessHandler.addReadOnlyUserToProject = sinon.stub()
  535. .callsArgWith(2, null)
  536. @ProjectController.loadEditor = sinon.stub()
  537. @TokenAccessController.readOnlyToken @req, @res, @next
  538. it 'should try to find a project with this token', (done) ->
  539. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.callCount)
  540. .to.equal 1
  541. expect(@TokenAccessHandler.findProjectWithReadOnlyToken.calledWith(@readOnlyToken))
  542. .to.equal true
  543. done()
  544. it 'should not give the user session read-only access', (done) ->
  545. expect(@TokenAccessHandler.grantSessionTokenAccess.callCount)
  546. .to.equal 0
  547. done()
  548. it 'should not pass control to loadEditor', (done) ->
  549. expect(@ProjectController.loadEditor.callCount).to.equal 0
  550. expect(@ProjectController.loadEditor.calledWith(@req, @res, @next)).to.equal false
  551. done()
  552. it 'should not add the user to the project with read-only access', (done) ->
  553. expect(@TokenAccessHandler.addReadOnlyUserToProject.callCount)
  554. .to.equal 0
  555. done()
  556. it 'should call next with a not-found error', (done) ->
  557. expect(@next.callCount).to.equal 1
  558. expect(@next.lastCall.args[0]).to.be.instanceof Error
  559. done()