AuthorizationMiddlewearTests.coffee 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Authorization/AuthorizationMiddlewear.js"
  6. SandboxedModule = require('sandboxed-module')
  7. Errors = require "../../../../app/js/Features/Errors/Errors.js"
  8. describe "AuthorizationMiddlewear", ->
  9. beforeEach ->
  10. @user_id = "user-id-123"
  11. @project_id = "project-id-123"
  12. @AuthenticationController =
  13. getLoggedInUserId: sinon.stub().returns(@user_id)
  14. isUserLoggedIn: sinon.stub().returns(true)
  15. @AuthorizationMiddlewear = SandboxedModule.require modulePath, requires:
  16. "./AuthorizationManager": @AuthorizationManager = {}
  17. "logger-sharelatex": {log: () ->}
  18. "mongojs": ObjectId: @ObjectId = {}
  19. "../Errors/Errors": Errors
  20. '../Authentication/AuthenticationController': @AuthenticationController
  21. @req = {}
  22. @res = {}
  23. @ObjectId.isValid = sinon.stub()
  24. @ObjectId.isValid.withArgs(@project_id).returns true
  25. @next = sinon.stub()
  26. METHODS_TO_TEST = {
  27. "ensureUserCanReadProject": "canUserReadProject"
  28. "ensureUserCanWriteProjectSettings": "canUserWriteProjectSettings"
  29. "ensureUserCanWriteProjectContent": "canUserWriteProjectContent"
  30. "ensureUserCanAdminProject": "canUserAdminProject"
  31. }
  32. for middlewearMethod, managerMethod of METHODS_TO_TEST
  33. do (middlewearMethod, managerMethod) ->
  34. describe middlewearMethod, ->
  35. beforeEach ->
  36. @req.params =
  37. project_id: @project_id
  38. @AuthorizationManager[managerMethod] = sinon.stub()
  39. @AuthorizationMiddlewear.redirectToRestricted = sinon.stub()
  40. describe "with missing project_id", ->
  41. beforeEach ->
  42. @req.params = {}
  43. it "should return an error to next", ->
  44. @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
  45. @next.calledWith(new Error()).should.equal true
  46. describe "with logged in user", ->
  47. beforeEach ->
  48. @AuthenticationController.getLoggedInUserId.returns(@user_id)
  49. describe "when user has permission", ->
  50. beforeEach ->
  51. @AuthorizationManager[managerMethod]
  52. .withArgs(@user_id, @project_id)
  53. .yields(null, true)
  54. it "should return next", ->
  55. @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
  56. @next.called.should.equal true
  57. describe "when user doesn't have permission", ->
  58. beforeEach ->
  59. @AuthorizationManager[managerMethod]
  60. .withArgs(@user_id, @project_id)
  61. .yields(null, false)
  62. it "should redirect to redirectToRestricted", ->
  63. @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
  64. @next.called.should.equal false
  65. @AuthorizationMiddlewear.redirectToRestricted
  66. .calledWith(@req, @res, @next)
  67. .should.equal true
  68. describe "with anonymous user", ->
  69. describe "when user has permission", ->
  70. beforeEach ->
  71. @AuthenticationController.getLoggedInUserId.returns(null)
  72. @AuthorizationManager[managerMethod]
  73. .withArgs(null, @project_id)
  74. .yields(null, true)
  75. it "should return next", ->
  76. @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
  77. @next.called.should.equal true
  78. describe "when user doesn't have permission", ->
  79. beforeEach ->
  80. @AuthenticationController.getLoggedInUserId.returns(null)
  81. @AuthorizationManager[managerMethod]
  82. .withArgs(null, @project_id)
  83. .yields(null, false)
  84. it "should redirect to redirectToRestricted", ->
  85. @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
  86. @next.called.should.equal false
  87. @AuthorizationMiddlewear.redirectToRestricted
  88. .calledWith(@req, @res, @next)
  89. .should.equal true
  90. describe "with malformed project id", ->
  91. beforeEach ->
  92. @req.params =
  93. project_id: "blah"
  94. @ObjectId.isValid = sinon.stub().returns false
  95. it "should return a not found error", (done) ->
  96. @AuthorizationMiddlewear[middlewearMethod] @req, @res, (error) ->
  97. error.should.be.instanceof Errors.NotFoundError
  98. done()
  99. describe "ensureUserIsSiteAdmin", ->
  100. beforeEach ->
  101. @AuthorizationManager.isUserSiteAdmin = sinon.stub()
  102. @AuthorizationMiddlewear.redirectToRestricted = sinon.stub()
  103. describe "with logged in user", ->
  104. beforeEach ->
  105. @AuthenticationController.getLoggedInUserId.returns(@user_id)
  106. describe "when user has permission", ->
  107. beforeEach ->
  108. @AuthorizationManager.isUserSiteAdmin
  109. .withArgs(@user_id)
  110. .yields(null, true)
  111. it "should return next", ->
  112. @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
  113. @next.called.should.equal true
  114. describe "when user doesn't have permission", ->
  115. beforeEach ->
  116. @AuthorizationManager.isUserSiteAdmin
  117. .withArgs(@user_id)
  118. .yields(null, false)
  119. it "should redirect to redirectToRestricted", ->
  120. @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
  121. @next.called.should.equal false
  122. @AuthorizationMiddlewear.redirectToRestricted
  123. .calledWith(@req, @res, @next)
  124. .should.equal true
  125. describe "with anonymous user", ->
  126. describe "when user has permission", ->
  127. beforeEach ->
  128. @AuthenticationController.getLoggedInUserId.returns(null)
  129. @AuthorizationManager.isUserSiteAdmin
  130. .withArgs(null)
  131. .yields(null, true)
  132. it "should return next", ->
  133. @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
  134. @next.called.should.equal true
  135. describe "when user doesn't have permission", ->
  136. beforeEach ->
  137. @AuthenticationController.getLoggedInUserId.returns(null)
  138. @AuthorizationManager.isUserSiteAdmin
  139. .withArgs(null)
  140. .yields(null, false)
  141. it "should redirect to redirectToRestricted", ->
  142. @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
  143. @next.called.should.equal false
  144. @AuthorizationMiddlewear.redirectToRestricted
  145. .calledWith(@req, @res, @next)
  146. .should.equal true
  147. describe "ensureUserCanReadMultipleProjects", ->
  148. beforeEach ->
  149. @AuthorizationManager.canUserReadProject = sinon.stub()
  150. @AuthorizationMiddlewear.redirectToRestricted = sinon.stub()
  151. @req.query =
  152. project_ids: "project1,project2"
  153. describe "with logged in user", ->
  154. beforeEach ->
  155. @AuthenticationController.getLoggedInUserId.returns(@user_id)
  156. describe "when user has permission to access all projects", ->
  157. beforeEach ->
  158. @AuthorizationManager.canUserReadProject
  159. .withArgs(@user_id, "project1")
  160. .yields(null, true)
  161. @AuthorizationManager.canUserReadProject
  162. .withArgs(@user_id, "project2")
  163. .yields(null, true)
  164. it "should return next", ->
  165. @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
  166. @next.called.should.equal true
  167. describe "when user doesn't have permission to access one of the projects", ->
  168. beforeEach ->
  169. @AuthorizationManager.canUserReadProject
  170. .withArgs(@user_id, "project1")
  171. .yields(null, true)
  172. @AuthorizationManager.canUserReadProject
  173. .withArgs(@user_id, "project2")
  174. .yields(null, false)
  175. it "should redirect to redirectToRestricted", ->
  176. @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
  177. @next.called.should.equal false
  178. @AuthorizationMiddlewear.redirectToRestricted
  179. .calledWith(@req, @res, @next)
  180. .should.equal true
  181. describe "with anonymous user", ->
  182. describe "when user has permission", ->
  183. describe "when user has permission to access all projects", ->
  184. beforeEach ->
  185. @AuthenticationController.getLoggedInUserId.returns(null)
  186. @AuthorizationManager.canUserReadProject
  187. .withArgs(null, "project1")
  188. .yields(null, true)
  189. @AuthorizationManager.canUserReadProject
  190. .withArgs(null, "project2")
  191. .yields(null, true)
  192. it "should return next", ->
  193. @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
  194. @next.called.should.equal true
  195. describe "when user doesn't have permission to access one of the projects", ->
  196. beforeEach ->
  197. @AuthenticationController.getLoggedInUserId.returns(null)
  198. @AuthorizationManager.canUserReadProject
  199. .withArgs(null, "project1")
  200. .yields(null, true)
  201. @AuthorizationManager.canUserReadProject
  202. .withArgs(null, "project2")
  203. .yields(null, false)
  204. it "should redirect to redirectToRestricted", ->
  205. @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
  206. @next.called.should.equal false
  207. @AuthorizationMiddlewear.redirectToRestricted
  208. .calledWith(@req, @res, @next)
  209. .should.equal true