AuthorizationManagerTests.coffee 14 KB

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