AuthorizationManagerTests.coffee 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Authorization/AuthorizationManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. Errors = require "../../../../app/js/Features/Errors/Errors.js"
  8. describe "AuthorizationManager", ->
  9. beforeEach ->
  10. @AuthorizationManager = SandboxedModule.require modulePath, requires:
  11. "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
  12. "../../models/Project": Project: @Project = {}
  13. "../../models/User": User: @User = {}
  14. "../Errors/Errors": Errors
  15. "../TokenAccess/TokenAccessHandler": @TokenAccessHandler = {
  16. isValidToken: sinon.stub().callsArgWith(2, null, false, false)
  17. }
  18. @user_id = "user-id-1"
  19. @project_id = "project-id-1"
  20. @token = 'some-token'
  21. @callback = sinon.stub()
  22. describe "getPrivilegeLevelForProject", ->
  23. beforeEach ->
  24. @Project.findOne = sinon.stub()
  25. @AuthorizationManager.isUserSiteAdmin = sinon.stub()
  26. @CollaboratorsHandler.getMemberIdPrivilegeLevel = sinon.stub()
  27. describe 'with a token-based project', ->
  28. beforeEach ->
  29. @Project.findOne
  30. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  31. .yields(null, { publicAccesLevel: "tokenBased" })
  32. describe "with a user_id with a privilege level", ->
  33. beforeEach ->
  34. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  35. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  36. .withArgs(@user_id, @project_id)
  37. .yields(null, "readOnly")
  38. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  39. it "should return the user's privilege level", ->
  40. @callback.calledWith(null, "readOnly", false).should.equal true
  41. describe "with a user_id with no privilege level", ->
  42. beforeEach ->
  43. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  44. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  45. .withArgs(@user_id, @project_id)
  46. .yields(null, false)
  47. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  48. it "should return false", ->
  49. @callback.calledWith(null, false, false).should.equal true
  50. describe "with a user_id who is an admin", ->
  51. beforeEach ->
  52. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, true)
  53. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  54. .withArgs(@user_id, @project_id)
  55. .yields(null, false)
  56. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  57. it "should return the user as an owner", ->
  58. @callback.calledWith(null, "owner", false).should.equal true
  59. describe "with no user (anonymous)", ->
  60. describe 'when the token is not valid', ->
  61. beforeEach ->
  62. @TokenAccessHandler.isValidToken = sinon.stub()
  63. .withArgs(@project_id, @token)
  64. .yields(null, false, false)
  65. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  66. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  67. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  68. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  69. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  70. it 'should check if the token is valid', ->
  71. @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
  72. it "should return false", ->
  73. @callback.calledWith(null, false, false).should.equal true
  74. describe 'when the token is valid for read-and-write', ->
  75. describe 'when read-write-sharing is not enabled', ->
  76. beforeEach ->
  77. @TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = false
  78. @TokenAccessHandler.isValidToken = sinon.stub()
  79. .withArgs(@project_id, @token)
  80. .yields(null, true, false)
  81. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  82. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  83. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  84. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  85. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  86. it 'should check if the token is valid', ->
  87. @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
  88. it "should deny access", ->
  89. @callback.calledWith(null, false, false).should.equal true
  90. describe 'when read-write-sharing is enabled', ->
  91. beforeEach ->
  92. @TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = true
  93. @TokenAccessHandler.isValidToken = sinon.stub()
  94. .withArgs(@project_id, @token)
  95. .yields(null, true, false)
  96. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  97. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  98. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  99. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  100. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  101. it 'should check if the token is valid', ->
  102. @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
  103. it "should give read-write access", ->
  104. @callback.calledWith(null, "readAndWrite", false).should.equal true
  105. describe 'when the token is valid for read-only', ->
  106. beforeEach ->
  107. @TokenAccessHandler.isValidToken = sinon.stub()
  108. .withArgs(@project_id, @token)
  109. .yields(null, false, true)
  110. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  111. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  112. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  113. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  114. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  115. it 'should check if the token is valid', ->
  116. @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
  117. it "should give read-only access", ->
  118. @callback.calledWith(null, "readOnly", false).should.equal true
  119. describe "with a private project", ->
  120. beforeEach ->
  121. @Project.findOne
  122. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  123. .yields(null, { publicAccesLevel: "private" })
  124. describe "with a user_id with a privilege level", ->
  125. beforeEach ->
  126. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  127. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  128. .withArgs(@user_id, @project_id)
  129. .yields(null, "readOnly")
  130. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  131. it "should return the user's privilege level", ->
  132. @callback.calledWith(null, "readOnly", false).should.equal true
  133. describe "with a user_id with no privilege level", ->
  134. beforeEach ->
  135. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  136. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  137. .withArgs(@user_id, @project_id)
  138. .yields(null, false)
  139. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  140. it "should return false", ->
  141. @callback.calledWith(null, false, false).should.equal true
  142. describe "with a user_id who is an admin", ->
  143. beforeEach ->
  144. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, true)
  145. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  146. .withArgs(@user_id, @project_id)
  147. .yields(null, false)
  148. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  149. it "should return the user as an owner", ->
  150. @callback.calledWith(null, "owner", false).should.equal true
  151. describe "with no user (anonymous)", ->
  152. beforeEach ->
  153. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  154. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  155. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  156. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  157. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  158. it "should return false", ->
  159. @callback.calledWith(null, false, false).should.equal true
  160. describe "with a public project", ->
  161. beforeEach ->
  162. @Project.findOne
  163. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  164. .yields(null, { publicAccesLevel: "readAndWrite" })
  165. describe "with a user_id with a privilege level", ->
  166. beforeEach ->
  167. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  168. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  169. .withArgs(@user_id, @project_id)
  170. .yields(null, "readOnly")
  171. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  172. it "should return the user's privilege level", ->
  173. @callback.calledWith(null, "readOnly", false).should.equal true
  174. describe "with a user_id with no privilege level", ->
  175. beforeEach ->
  176. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  177. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  178. .withArgs(@user_id, @project_id)
  179. .yields(null, false)
  180. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  181. it "should return the public privilege level", ->
  182. @callback.calledWith(null, "readAndWrite", true).should.equal true
  183. describe "with a user_id who is an admin", ->
  184. beforeEach ->
  185. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, true)
  186. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  187. .withArgs(@user_id, @project_id)
  188. .yields(null, false)
  189. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  190. it "should return the user as an owner", ->
  191. @callback.calledWith(null, "owner", false).should.equal true
  192. describe "with no user (anonymous)", ->
  193. beforeEach ->
  194. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  195. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  196. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  197. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  198. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  199. it "should return the public privilege level", ->
  200. @callback.calledWith(null, "readAndWrite", true).should.equal true
  201. describe "when the project doesn't exist", ->
  202. beforeEach ->
  203. @Project.findOne
  204. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  205. .yields(null, null)
  206. it "should return a NotFoundError", ->
  207. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, (error) ->
  208. error.should.be.instanceof Errors.NotFoundError
  209. describe "when the project id is not valid", ->
  210. beforeEach ->
  211. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  212. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  213. .withArgs(@user_id, @project_id)
  214. .yields(null, "readOnly")
  215. it "should return a error", (done)->
  216. @AuthorizationManager.getPrivilegeLevelForProject undefined, "not project id", @token, (err) =>
  217. @Project.findOne.called.should.equal false
  218. expect(err).to.exist
  219. done()
  220. describe "canUserReadProject", ->
  221. beforeEach ->
  222. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  223. describe "when user is owner", ->
  224. beforeEach ->
  225. @AuthorizationManager.getPrivilegeLevelForProject
  226. .withArgs(@user_id, @project_id, @token)
  227. .yields(null, "owner", false)
  228. it "should return true", (done) ->
  229. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  230. expect(canRead).to.equal true
  231. done()
  232. describe "when user has read-write access", ->
  233. beforeEach ->
  234. @AuthorizationManager.getPrivilegeLevelForProject
  235. .withArgs(@user_id, @project_id, @token)
  236. .yields(null, "readAndWrite", false)
  237. it "should return true", (done) ->
  238. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  239. expect(canRead).to.equal true
  240. done()
  241. describe "when user has read-only access", ->
  242. beforeEach ->
  243. @AuthorizationManager.getPrivilegeLevelForProject
  244. .withArgs(@user_id, @project_id, @token)
  245. .yields(null, "readOnly", false)
  246. it "should return true", (done) ->
  247. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  248. expect(canRead).to.equal true
  249. done()
  250. describe "when user has no access", ->
  251. beforeEach ->
  252. @AuthorizationManager.getPrivilegeLevelForProject
  253. .withArgs(@user_id, @project_id, @token)
  254. .yields(null, false, false)
  255. it "should return false", (done) ->
  256. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  257. expect(canRead).to.equal false
  258. done()
  259. describe "canUserWriteProjectContent", ->
  260. beforeEach ->
  261. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  262. describe "when user is owner", ->
  263. beforeEach ->
  264. @AuthorizationManager.getPrivilegeLevelForProject
  265. .withArgs(@user_id, @project_id, @token)
  266. .yields(null, "owner", false)
  267. it "should return true", (done) ->
  268. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  269. expect(canWrite).to.equal true
  270. done()
  271. describe "when user has read-write access", ->
  272. beforeEach ->
  273. @AuthorizationManager.getPrivilegeLevelForProject
  274. .withArgs(@user_id, @project_id, @token)
  275. .yields(null, "readAndWrite", false)
  276. it "should return true", (done) ->
  277. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  278. expect(canWrite).to.equal true
  279. done()
  280. describe "when user has read-only access", ->
  281. beforeEach ->
  282. @AuthorizationManager.getPrivilegeLevelForProject
  283. .withArgs(@user_id, @project_id, @token)
  284. .yields(null, "readOnly", false)
  285. it "should return false", (done) ->
  286. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  287. expect(canWrite).to.equal false
  288. done()
  289. describe "when user has no access", ->
  290. beforeEach ->
  291. @AuthorizationManager.getPrivilegeLevelForProject
  292. .withArgs(@user_id, @project_id, @token)
  293. .yields(null, false, false)
  294. it "should return false", (done) ->
  295. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  296. expect(canWrite).to.equal false
  297. done()
  298. describe "canUserWriteProjectSettings", ->
  299. beforeEach ->
  300. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  301. describe "when user is owner", ->
  302. beforeEach ->
  303. @AuthorizationManager.getPrivilegeLevelForProject
  304. .withArgs(@user_id, @project_id, @token)
  305. .yields(null, "owner", false)
  306. it "should return true", (done) ->
  307. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  308. expect(canWrite).to.equal true
  309. done()
  310. describe "when user has read-write access as a collaborator", ->
  311. beforeEach ->
  312. @AuthorizationManager.getPrivilegeLevelForProject
  313. .withArgs(@user_id, @project_id, @token)
  314. .yields(null, "readAndWrite", false)
  315. it "should return true", (done) ->
  316. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  317. expect(canWrite).to.equal true
  318. done()
  319. describe "when user has read-write access as the public", ->
  320. beforeEach ->
  321. @AuthorizationManager.getPrivilegeLevelForProject
  322. .withArgs(@user_id, @project_id, @token)
  323. .yields(null, "readAndWrite", true)
  324. it "should return false", (done) ->
  325. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  326. expect(canWrite).to.equal false
  327. done()
  328. describe "when user has read-only access", ->
  329. beforeEach ->
  330. @AuthorizationManager.getPrivilegeLevelForProject
  331. .withArgs(@user_id, @project_id, @token)
  332. .yields(null, "readOnly", false)
  333. it "should return false", (done) ->
  334. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  335. expect(canWrite).to.equal false
  336. done()
  337. describe "when user has no access", ->
  338. beforeEach ->
  339. @AuthorizationManager.getPrivilegeLevelForProject
  340. .withArgs(@user_id, @project_id, @token)
  341. .yields(null, false, false)
  342. it "should return false", (done) ->
  343. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  344. expect(canWrite).to.equal false
  345. done()
  346. describe "canUserAdminProject", ->
  347. beforeEach ->
  348. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  349. describe "when user is owner", ->
  350. beforeEach ->
  351. @AuthorizationManager.getPrivilegeLevelForProject
  352. .withArgs(@user_id, @project_id, @token)
  353. .yields(null, "owner", false)
  354. it "should return true", (done) ->
  355. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  356. expect(canAdmin).to.equal true
  357. done()
  358. describe "when user has read-write access", ->
  359. beforeEach ->
  360. @AuthorizationManager.getPrivilegeLevelForProject
  361. .withArgs(@user_id, @project_id, @token)
  362. .yields(null, "readAndWrite", false)
  363. it "should return false", (done) ->
  364. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  365. expect(canAdmin).to.equal false
  366. done()
  367. describe "when user has read-only access", ->
  368. beforeEach ->
  369. @AuthorizationManager.getPrivilegeLevelForProject
  370. .withArgs(@user_id, @project_id, @token)
  371. .yields(null, "readOnly", false)
  372. it "should return false", (done) ->
  373. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  374. expect(canAdmin).to.equal false
  375. done()
  376. describe "when user has no access", ->
  377. beforeEach ->
  378. @AuthorizationManager.getPrivilegeLevelForProject
  379. .withArgs(@user_id, @project_id, @token)
  380. .yields(null, false, false)
  381. it "should return false", (done) ->
  382. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  383. expect(canAdmin).to.equal false
  384. done()
  385. describe "isUserSiteAdmin", ->
  386. beforeEach ->
  387. @User.findOne = sinon.stub()
  388. describe "when user is admin", ->
  389. beforeEach ->
  390. @User.findOne
  391. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  392. .yields(null, { isAdmin: true })
  393. it "should return true", (done) ->
  394. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  395. expect(isAdmin).to.equal true
  396. done()
  397. describe "when user is not admin", ->
  398. beforeEach ->
  399. @User.findOne
  400. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  401. .yields(null, { isAdmin: false })
  402. it "should return false", (done) ->
  403. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  404. expect(isAdmin).to.equal false
  405. done()
  406. describe "when user is not found", ->
  407. beforeEach ->
  408. @User.findOne
  409. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  410. .yields(null, null)
  411. it "should return false", (done) ->
  412. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  413. expect(isAdmin).to.equal false
  414. done()
  415. describe "when no user is passed", ->
  416. it "should return false", (done) ->
  417. @AuthorizationManager.isUserSiteAdmin null, (error, isAdmin) =>
  418. @User.findOne.called.should.equal false
  419. expect(isAdmin).to.equal false
  420. done()