AuthorizationManagerTests.coffee 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 private project", ->
  28. beforeEach ->
  29. @Project.findOne
  30. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  31. .yields(null, { publicAccesLevel: "private" })
  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. beforeEach ->
  61. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  62. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  63. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  64. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  65. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  66. it "should return false", ->
  67. @callback.calledWith(null, false, false).should.equal true
  68. describe "with a public project", ->
  69. beforeEach ->
  70. @Project.findOne
  71. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  72. .yields(null, { publicAccesLevel: "readAndWrite" })
  73. describe "with a user_id with a privilege level", ->
  74. beforeEach ->
  75. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  76. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  77. .withArgs(@user_id, @project_id)
  78. .yields(null, "readOnly")
  79. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  80. it "should return the user's privilege level", ->
  81. @callback.calledWith(null, "readOnly", false).should.equal true
  82. describe "with a user_id with no privilege level", ->
  83. beforeEach ->
  84. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  85. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  86. .withArgs(@user_id, @project_id)
  87. .yields(null, false)
  88. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  89. it "should return the public privilege level", ->
  90. @callback.calledWith(null, "readAndWrite", true).should.equal true
  91. describe "with a user_id who is an admin", ->
  92. beforeEach ->
  93. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, true)
  94. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  95. .withArgs(@user_id, @project_id)
  96. .yields(null, false)
  97. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
  98. it "should return the user as an owner", ->
  99. @callback.calledWith(null, "owner", false).should.equal true
  100. describe "with no user (anonymous)", ->
  101. beforeEach ->
  102. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
  103. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  104. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  105. it "should not call AuthorizationManager.isUserSiteAdmin", ->
  106. @AuthorizationManager.isUserSiteAdmin.called.should.equal false
  107. it "should return the public privilege level", ->
  108. @callback.calledWith(null, "readAndWrite", true).should.equal true
  109. describe "when the project doesn't exist", ->
  110. beforeEach ->
  111. @Project.findOne
  112. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  113. .yields(null, null)
  114. it "should return a NotFoundError", ->
  115. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, (error) ->
  116. error.should.be.instanceof Errors.NotFoundError
  117. describe "when the project id is not valid", ->
  118. beforeEach ->
  119. @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
  120. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  121. .withArgs(@user_id, @project_id)
  122. .yields(null, "readOnly")
  123. it "should return a error", (done)->
  124. @AuthorizationManager.getPrivilegeLevelForProject undefined, "not project id", @token, (err) =>
  125. @Project.findOne.called.should.equal false
  126. expect(err).to.exist
  127. done()
  128. describe "canUserReadProject", ->
  129. beforeEach ->
  130. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  131. describe "when user is owner", ->
  132. beforeEach ->
  133. @AuthorizationManager.getPrivilegeLevelForProject
  134. .withArgs(@user_id, @project_id, @token)
  135. .yields(null, "owner", false)
  136. it "should return true", (done) ->
  137. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  138. expect(canRead).to.equal true
  139. done()
  140. describe "when user has read-write access", ->
  141. beforeEach ->
  142. @AuthorizationManager.getPrivilegeLevelForProject
  143. .withArgs(@user_id, @project_id, @token)
  144. .yields(null, "readAndWrite", false)
  145. it "should return true", (done) ->
  146. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  147. expect(canRead).to.equal true
  148. done()
  149. describe "when user has read-only access", ->
  150. beforeEach ->
  151. @AuthorizationManager.getPrivilegeLevelForProject
  152. .withArgs(@user_id, @project_id, @token)
  153. .yields(null, "readOnly", false)
  154. it "should return true", (done) ->
  155. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  156. expect(canRead).to.equal true
  157. done()
  158. describe "when user has no access", ->
  159. beforeEach ->
  160. @AuthorizationManager.getPrivilegeLevelForProject
  161. .withArgs(@user_id, @project_id, @token)
  162. .yields(null, false, false)
  163. it "should return false", (done) ->
  164. @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
  165. expect(canRead).to.equal false
  166. done()
  167. describe "canUserWriteProjectContent", ->
  168. beforeEach ->
  169. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  170. describe "when user is owner", ->
  171. beforeEach ->
  172. @AuthorizationManager.getPrivilegeLevelForProject
  173. .withArgs(@user_id, @project_id, @token)
  174. .yields(null, "owner", false)
  175. it "should return true", (done) ->
  176. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  177. expect(canWrite).to.equal true
  178. done()
  179. describe "when user has read-write access", ->
  180. beforeEach ->
  181. @AuthorizationManager.getPrivilegeLevelForProject
  182. .withArgs(@user_id, @project_id, @token)
  183. .yields(null, "readAndWrite", false)
  184. it "should return true", (done) ->
  185. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  186. expect(canWrite).to.equal true
  187. done()
  188. describe "when user has read-only access", ->
  189. beforeEach ->
  190. @AuthorizationManager.getPrivilegeLevelForProject
  191. .withArgs(@user_id, @project_id, @token)
  192. .yields(null, "readOnly", false)
  193. it "should return false", (done) ->
  194. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  195. expect(canWrite).to.equal false
  196. done()
  197. describe "when user has no access", ->
  198. beforeEach ->
  199. @AuthorizationManager.getPrivilegeLevelForProject
  200. .withArgs(@user_id, @project_id, @token)
  201. .yields(null, false, false)
  202. it "should return false", (done) ->
  203. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
  204. expect(canWrite).to.equal false
  205. done()
  206. describe "canUserWriteProjectSettings", ->
  207. beforeEach ->
  208. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  209. describe "when user is owner", ->
  210. beforeEach ->
  211. @AuthorizationManager.getPrivilegeLevelForProject
  212. .withArgs(@user_id, @project_id, @token)
  213. .yields(null, "owner", false)
  214. it "should return true", (done) ->
  215. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  216. expect(canWrite).to.equal true
  217. done()
  218. describe "when user has read-write access as a collaborator", ->
  219. beforeEach ->
  220. @AuthorizationManager.getPrivilegeLevelForProject
  221. .withArgs(@user_id, @project_id, @token)
  222. .yields(null, "readAndWrite", false)
  223. it "should return true", (done) ->
  224. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  225. expect(canWrite).to.equal true
  226. done()
  227. describe "when user has read-write access as the public", ->
  228. beforeEach ->
  229. @AuthorizationManager.getPrivilegeLevelForProject
  230. .withArgs(@user_id, @project_id, @token)
  231. .yields(null, "readAndWrite", true)
  232. it "should return false", (done) ->
  233. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  234. expect(canWrite).to.equal false
  235. done()
  236. describe "when user has read-only access", ->
  237. beforeEach ->
  238. @AuthorizationManager.getPrivilegeLevelForProject
  239. .withArgs(@user_id, @project_id, @token)
  240. .yields(null, "readOnly", false)
  241. it "should return false", (done) ->
  242. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  243. expect(canWrite).to.equal false
  244. done()
  245. describe "when user has no access", ->
  246. beforeEach ->
  247. @AuthorizationManager.getPrivilegeLevelForProject
  248. .withArgs(@user_id, @project_id, @token)
  249. .yields(null, false, false)
  250. it "should return false", (done) ->
  251. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
  252. expect(canWrite).to.equal false
  253. done()
  254. describe "canUserAdminProject", ->
  255. beforeEach ->
  256. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  257. describe "when user is owner", ->
  258. beforeEach ->
  259. @AuthorizationManager.getPrivilegeLevelForProject
  260. .withArgs(@user_id, @project_id, @token)
  261. .yields(null, "owner", false)
  262. it "should return true", (done) ->
  263. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  264. expect(canAdmin).to.equal true
  265. done()
  266. describe "when user has read-write access", ->
  267. beforeEach ->
  268. @AuthorizationManager.getPrivilegeLevelForProject
  269. .withArgs(@user_id, @project_id, @token)
  270. .yields(null, "readAndWrite", false)
  271. it "should return false", (done) ->
  272. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  273. expect(canAdmin).to.equal false
  274. done()
  275. describe "when user has read-only access", ->
  276. beforeEach ->
  277. @AuthorizationManager.getPrivilegeLevelForProject
  278. .withArgs(@user_id, @project_id, @token)
  279. .yields(null, "readOnly", false)
  280. it "should return false", (done) ->
  281. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  282. expect(canAdmin).to.equal false
  283. done()
  284. describe "when user has no access", ->
  285. beforeEach ->
  286. @AuthorizationManager.getPrivilegeLevelForProject
  287. .withArgs(@user_id, @project_id, @token)
  288. .yields(null, false, false)
  289. it "should return false", (done) ->
  290. @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
  291. expect(canAdmin).to.equal false
  292. done()
  293. describe "isUserSiteAdmin", ->
  294. beforeEach ->
  295. @User.findOne = sinon.stub()
  296. describe "when user is admin", ->
  297. beforeEach ->
  298. @User.findOne
  299. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  300. .yields(null, { isAdmin: true })
  301. it "should return true", (done) ->
  302. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  303. expect(isAdmin).to.equal true
  304. done()
  305. describe "when user is not admin", ->
  306. beforeEach ->
  307. @User.findOne
  308. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  309. .yields(null, { isAdmin: false })
  310. it "should return false", (done) ->
  311. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  312. expect(isAdmin).to.equal false
  313. done()
  314. describe "when user is not found", ->
  315. beforeEach ->
  316. @User.findOne
  317. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  318. .yields(null, null)
  319. it "should return false", (done) ->
  320. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  321. expect(isAdmin).to.equal false
  322. done()
  323. describe "when no user is passed", ->
  324. it "should return false", (done) ->
  325. @AuthorizationManager.isUserSiteAdmin null, (error, isAdmin) =>
  326. @User.findOne.called.should.equal false
  327. expect(isAdmin).to.equal false
  328. done()