AuthorizationManagerTests.coffee 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "canUserReadProject", ->
  114. beforeEach ->
  115. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  116. describe "when user is owner", ->
  117. beforeEach ->
  118. @AuthorizationManager.getPrivilegeLevelForProject
  119. .withArgs(@user_id, @project_id)
  120. .yields(null, "owner", false)
  121. it "should return true", (done) ->
  122. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  123. expect(canRead).to.equal true
  124. done()
  125. describe "when user has read-write access", ->
  126. beforeEach ->
  127. @AuthorizationManager.getPrivilegeLevelForProject
  128. .withArgs(@user_id, @project_id)
  129. .yields(null, "readAndWrite", false)
  130. it "should return true", (done) ->
  131. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  132. expect(canRead).to.equal true
  133. done()
  134. describe "when user has read-only access", ->
  135. beforeEach ->
  136. @AuthorizationManager.getPrivilegeLevelForProject
  137. .withArgs(@user_id, @project_id)
  138. .yields(null, "readOnly", false)
  139. it "should return true", (done) ->
  140. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  141. expect(canRead).to.equal true
  142. done()
  143. describe "when user has no access", ->
  144. beforeEach ->
  145. @AuthorizationManager.getPrivilegeLevelForProject
  146. .withArgs(@user_id, @project_id)
  147. .yields(null, false, false)
  148. it "should return false", (done) ->
  149. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  150. expect(canRead).to.equal false
  151. done()
  152. describe "canUserWriteProjectContent", ->
  153. beforeEach ->
  154. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  155. describe "when user is owner", ->
  156. beforeEach ->
  157. @AuthorizationManager.getPrivilegeLevelForProject
  158. .withArgs(@user_id, @project_id)
  159. .yields(null, "owner", false)
  160. it "should return true", (done) ->
  161. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  162. expect(canWrite).to.equal true
  163. done()
  164. describe "when user has read-write access", ->
  165. beforeEach ->
  166. @AuthorizationManager.getPrivilegeLevelForProject
  167. .withArgs(@user_id, @project_id)
  168. .yields(null, "readAndWrite", false)
  169. it "should return true", (done) ->
  170. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  171. expect(canWrite).to.equal true
  172. done()
  173. describe "when user has read-only access", ->
  174. beforeEach ->
  175. @AuthorizationManager.getPrivilegeLevelForProject
  176. .withArgs(@user_id, @project_id)
  177. .yields(null, "readOnly", false)
  178. it "should return false", (done) ->
  179. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  180. expect(canWrite).to.equal false
  181. done()
  182. describe "when user has no access", ->
  183. beforeEach ->
  184. @AuthorizationManager.getPrivilegeLevelForProject
  185. .withArgs(@user_id, @project_id)
  186. .yields(null, false, false)
  187. it "should return false", (done) ->
  188. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  189. expect(canWrite).to.equal false
  190. done()
  191. describe "canUserWriteProjectSettings", ->
  192. beforeEach ->
  193. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  194. describe "when user is owner", ->
  195. beforeEach ->
  196. @AuthorizationManager.getPrivilegeLevelForProject
  197. .withArgs(@user_id, @project_id)
  198. .yields(null, "owner", false)
  199. it "should return true", (done) ->
  200. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  201. expect(canWrite).to.equal true
  202. done()
  203. describe "when user has read-write access as a collaborator", ->
  204. beforeEach ->
  205. @AuthorizationManager.getPrivilegeLevelForProject
  206. .withArgs(@user_id, @project_id)
  207. .yields(null, "readAndWrite", false)
  208. it "should return true", (done) ->
  209. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  210. expect(canWrite).to.equal true
  211. done()
  212. describe "when user has read-write access as the public", ->
  213. beforeEach ->
  214. @AuthorizationManager.getPrivilegeLevelForProject
  215. .withArgs(@user_id, @project_id)
  216. .yields(null, "readAndWrite", true)
  217. it "should return false", (done) ->
  218. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  219. expect(canWrite).to.equal false
  220. done()
  221. describe "when user has read-only access", ->
  222. beforeEach ->
  223. @AuthorizationManager.getPrivilegeLevelForProject
  224. .withArgs(@user_id, @project_id)
  225. .yields(null, "readOnly", false)
  226. it "should return false", (done) ->
  227. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  228. expect(canWrite).to.equal false
  229. done()
  230. describe "when user has no access", ->
  231. beforeEach ->
  232. @AuthorizationManager.getPrivilegeLevelForProject
  233. .withArgs(@user_id, @project_id)
  234. .yields(null, false, false)
  235. it "should return false", (done) ->
  236. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  237. expect(canWrite).to.equal false
  238. done()
  239. describe "canUserAdminProject", ->
  240. beforeEach ->
  241. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  242. describe "when user is owner", ->
  243. beforeEach ->
  244. @AuthorizationManager.getPrivilegeLevelForProject
  245. .withArgs(@user_id, @project_id)
  246. .yields(null, "owner", false)
  247. it "should return true", (done) ->
  248. @AuthorizationManager.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  249. expect(canAdmin).to.equal true
  250. done()
  251. describe "when user has read-write access", ->
  252. beforeEach ->
  253. @AuthorizationManager.getPrivilegeLevelForProject
  254. .withArgs(@user_id, @project_id)
  255. .yields(null, "readAndWrite", false)
  256. it "should return false", (done) ->
  257. @AuthorizationManager.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  258. expect(canAdmin).to.equal false
  259. done()
  260. describe "when user has read-only access", ->
  261. beforeEach ->
  262. @AuthorizationManager.getPrivilegeLevelForProject
  263. .withArgs(@user_id, @project_id)
  264. .yields(null, "readOnly", false)
  265. it "should return false", (done) ->
  266. @AuthorizationManager.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  267. expect(canAdmin).to.equal false
  268. done()
  269. describe "when user has no access", ->
  270. beforeEach ->
  271. @AuthorizationManager.getPrivilegeLevelForProject
  272. .withArgs(@user_id, @project_id)
  273. .yields(null, false, false)
  274. it "should return false", (done) ->
  275. @AuthorizationManager.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  276. expect(canAdmin).to.equal false
  277. done()
  278. describe "isUserSiteAdmin", ->
  279. beforeEach ->
  280. @User.findOne = sinon.stub()
  281. describe "when user is admin", ->
  282. beforeEach ->
  283. @User.findOne
  284. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  285. .yields(null, { isAdmin: true })
  286. it "should return true", (done) ->
  287. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  288. expect(isAdmin).to.equal true
  289. done()
  290. describe "when user is not admin", ->
  291. beforeEach ->
  292. @User.findOne
  293. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  294. .yields(null, { isAdmin: false })
  295. it "should return false", (done) ->
  296. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  297. expect(isAdmin).to.equal false
  298. done()
  299. describe "when user is not found", ->
  300. beforeEach ->
  301. @User.findOne
  302. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  303. .yields(null, null)
  304. it "should return false", (done) ->
  305. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  306. expect(isAdmin).to.equal false
  307. done()
  308. describe "when no user is passed", ->
  309. it "should return false", (done) ->
  310. @AuthorizationManager.isUserSiteAdmin null, (error, isAdmin) =>
  311. @User.findOne.called.should.equal false
  312. expect(isAdmin).to.equal false
  313. done()