AuthorizationManagerTests.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. describe "AuthorizationManager", ->
  8. beforeEach ->
  9. @AuthorizationManager = SandboxedModule.require modulePath, requires:
  10. "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
  11. "../../models/Project": Project: @Project = {}
  12. "../../models/User": User: @User = {}
  13. @user_id = "user-id-1"
  14. @project_id = "project-id-1"
  15. @callback = sinon.stub()
  16. describe "getPrivilegeLevelForProject", ->
  17. beforeEach ->
  18. @Project.findOne = sinon.stub()
  19. @CollaboratorsHandler.getMemberIdPrivilegeLevel = sinon.stub()
  20. describe "with a private project", ->
  21. beforeEach ->
  22. @Project.findOne
  23. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  24. .yields(null, { publicAccesLevel: "private" })
  25. describe "with a user_id with a privilege level", ->
  26. beforeEach ->
  27. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  28. .withArgs(@user_id, @project_id)
  29. .yields(null, "readOnly")
  30. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @callback
  31. it "should return the user's privilege level", ->
  32. @callback.calledWith(null, "readOnly", false).should.equal true
  33. describe "with a user_id with no privilege level", ->
  34. beforeEach ->
  35. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  36. .withArgs(@user_id, @project_id)
  37. .yields(null, false)
  38. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @callback
  39. it "should return false", ->
  40. @callback.calledWith(null, false, false).should.equal true
  41. describe "with no user (anonymous)", ->
  42. beforeEach ->
  43. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @callback
  44. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  45. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  46. it "should return false", ->
  47. @callback.calledWith(null, false, false).should.equal true
  48. describe "with a public project", ->
  49. beforeEach ->
  50. @Project.findOne
  51. .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
  52. .yields(null, { publicAccesLevel: "readAndWrite" })
  53. describe "with a user_id with a privilege level", ->
  54. beforeEach ->
  55. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  56. .withArgs(@user_id, @project_id)
  57. .yields(null, "readOnly")
  58. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @callback
  59. it "should return the user's privilege level", ->
  60. @callback.calledWith(null, "readOnly", false).should.equal true
  61. describe "with a user_id with no privilege level", ->
  62. beforeEach ->
  63. @CollaboratorsHandler.getMemberIdPrivilegeLevel
  64. .withArgs(@user_id, @project_id)
  65. .yields(null, false)
  66. @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @callback
  67. it "should return the public privilege level", ->
  68. @callback.calledWith(null, "readAndWrite", true).should.equal true
  69. describe "with no user (anonymous)", ->
  70. beforeEach ->
  71. @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @callback
  72. it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
  73. @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
  74. it "should return the public privilege level", ->
  75. @callback.calledWith(null, "readAndWrite", true).should.equal true
  76. describe "canUserReadProject", ->
  77. beforeEach ->
  78. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  79. describe "when user is owner", ->
  80. beforeEach ->
  81. @AuthorizationManager.getPrivilegeLevelForProject
  82. .withArgs(@user_id, @project_id)
  83. .yields(null, "owner", false)
  84. it "should return true", (done) ->
  85. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  86. expect(canRead).to.equal true
  87. done()
  88. describe "when user has read-write access", ->
  89. beforeEach ->
  90. @AuthorizationManager.getPrivilegeLevelForProject
  91. .withArgs(@user_id, @project_id)
  92. .yields(null, "readAndWrite", false)
  93. it "should return true", (done) ->
  94. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  95. expect(canRead).to.equal true
  96. done()
  97. describe "when user has read-only access", ->
  98. beforeEach ->
  99. @AuthorizationManager.getPrivilegeLevelForProject
  100. .withArgs(@user_id, @project_id)
  101. .yields(null, "readOnly", false)
  102. it "should return true", (done) ->
  103. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  104. expect(canRead).to.equal true
  105. done()
  106. describe "when user has no access", ->
  107. beforeEach ->
  108. @AuthorizationManager.getPrivilegeLevelForProject
  109. .withArgs(@user_id, @project_id)
  110. .yields(null, false, false)
  111. it "should return false", (done) ->
  112. @AuthorizationManager.canUserReadProject @user_id, @project_id, (error, canRead) ->
  113. expect(canRead).to.equal false
  114. done()
  115. describe "canUserWriteProjectContent", ->
  116. beforeEach ->
  117. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  118. describe "when user is owner", ->
  119. beforeEach ->
  120. @AuthorizationManager.getPrivilegeLevelForProject
  121. .withArgs(@user_id, @project_id)
  122. .yields(null, "owner", false)
  123. it "should return true", (done) ->
  124. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  125. expect(canWrite).to.equal true
  126. done()
  127. describe "when user has read-write access", ->
  128. beforeEach ->
  129. @AuthorizationManager.getPrivilegeLevelForProject
  130. .withArgs(@user_id, @project_id)
  131. .yields(null, "readAndWrite", false)
  132. it "should return true", (done) ->
  133. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  134. expect(canWrite).to.equal true
  135. done()
  136. describe "when user has read-only access", ->
  137. beforeEach ->
  138. @AuthorizationManager.getPrivilegeLevelForProject
  139. .withArgs(@user_id, @project_id)
  140. .yields(null, "readOnly", false)
  141. it "should return false", (done) ->
  142. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  143. expect(canWrite).to.equal false
  144. done()
  145. describe "when user has no access", ->
  146. beforeEach ->
  147. @AuthorizationManager.getPrivilegeLevelForProject
  148. .withArgs(@user_id, @project_id)
  149. .yields(null, false, false)
  150. it "should return false", (done) ->
  151. @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, (error, canWrite) ->
  152. expect(canWrite).to.equal false
  153. done()
  154. describe "canUserWriteProjectSettings", ->
  155. beforeEach ->
  156. @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
  157. describe "when user is owner", ->
  158. beforeEach ->
  159. @AuthorizationManager.getPrivilegeLevelForProject
  160. .withArgs(@user_id, @project_id)
  161. .yields(null, "owner", false)
  162. it "should return true", (done) ->
  163. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  164. expect(canWrite).to.equal true
  165. done()
  166. describe "when user has read-write access as a collaborator", ->
  167. beforeEach ->
  168. @AuthorizationManager.getPrivilegeLevelForProject
  169. .withArgs(@user_id, @project_id)
  170. .yields(null, "readAndWrite", false)
  171. it "should return true", (done) ->
  172. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  173. expect(canWrite).to.equal true
  174. done()
  175. describe "when user has read-write access as the public", ->
  176. beforeEach ->
  177. @AuthorizationManager.getPrivilegeLevelForProject
  178. .withArgs(@user_id, @project_id)
  179. .yields(null, "readAndWrite", true)
  180. it "should return false", (done) ->
  181. @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  182. expect(canWrite).to.equal false
  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.canUserWriteProjectSettings @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.canUserWriteProjectSettings @user_id, @project_id, (error, canWrite) ->
  200. expect(canWrite).to.equal false
  201. done()
  202. describe "canUserAdminProject", ->
  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.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  212. expect(canAdmin).to.equal true
  213. done()
  214. describe "when user has read-write access", ->
  215. beforeEach ->
  216. @AuthorizationManager.getPrivilegeLevelForProject
  217. .withArgs(@user_id, @project_id)
  218. .yields(null, "readAndWrite", false)
  219. it "should return false", (done) ->
  220. @AuthorizationManager.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  221. expect(canAdmin).to.equal false
  222. done()
  223. describe "when user has read-only access", ->
  224. beforeEach ->
  225. @AuthorizationManager.getPrivilegeLevelForProject
  226. .withArgs(@user_id, @project_id)
  227. .yields(null, "readOnly", false)
  228. it "should return false", (done) ->
  229. @AuthorizationManager.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  230. expect(canAdmin).to.equal false
  231. done()
  232. describe "when user has no access", ->
  233. beforeEach ->
  234. @AuthorizationManager.getPrivilegeLevelForProject
  235. .withArgs(@user_id, @project_id)
  236. .yields(null, false, false)
  237. it "should return false", (done) ->
  238. @AuthorizationManager.canUserAdminProject @user_id, @project_id, (error, canAdmin) ->
  239. expect(canAdmin).to.equal false
  240. done()
  241. describe "isUserSiteAdmin", ->
  242. beforeEach ->
  243. @User.findOne = sinon.stub()
  244. describe "when user is admin", ->
  245. beforeEach ->
  246. @User.findOne
  247. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  248. .yields(null, { isAdmin: true })
  249. it "should return true", (done) ->
  250. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  251. expect(isAdmin).to.equal true
  252. done()
  253. describe "when user is not admin", ->
  254. beforeEach ->
  255. @User.findOne
  256. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  257. .yields(null, { isAdmin: false })
  258. it "should return false", (done) ->
  259. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  260. expect(isAdmin).to.equal false
  261. done()
  262. describe "when user is not found", ->
  263. beforeEach ->
  264. @User.findOne
  265. .withArgs({ _id: @user_id }, { isAdmin: 1 })
  266. .yields(null, null)
  267. it "should return false", (done) ->
  268. @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
  269. expect(isAdmin).to.equal false
  270. done()
  271. describe "when no user is passed", ->
  272. it "should return false", (done) ->
  273. @AuthorizationManager.isUserSiteAdmin null, (error, isAdmin) =>
  274. @User.findOne.called.should.equal false
  275. expect(isAdmin).to.equal false
  276. done()