AuthorizationMiddlewearTests.coffee 7.9 KB

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