AuthorizationManagerTests.coffee 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. chai = require "chai"
  2. chai.should()
  3. expect = chai.expect
  4. sinon = require("sinon")
  5. SandboxedModule = require('sandboxed-module')
  6. path = require "path"
  7. modulePath = '../../../app/js/AuthorizationManager'
  8. describe 'AuthorizationManager', ->
  9. beforeEach ->
  10. @client =
  11. params: {}
  12. get: (param, cb) ->
  13. cb null, @params[param]
  14. set: (param, value, cb) ->
  15. @params[param] = value
  16. cb()
  17. del: (param, cb) ->
  18. delete @params[param]
  19. cb()
  20. @AuthorizationManager = SandboxedModule.require modulePath, requires: {}
  21. describe "assertClientCanViewProject", ->
  22. it "should allow the readOnly privilegeLevel", (done) ->
  23. @client.params.privilege_level = "readOnly"
  24. @AuthorizationManager.assertClientCanViewProject @client, (error) ->
  25. expect(error).to.be.null
  26. done()
  27. it "should allow the readAndWrite privilegeLevel", (done) ->
  28. @client.params.privilege_level = "readAndWrite"
  29. @AuthorizationManager.assertClientCanViewProject @client, (error) ->
  30. expect(error).to.be.null
  31. done()
  32. it "should allow the owner privilegeLevel", (done) ->
  33. @client.params.privilege_level = "owner"
  34. @AuthorizationManager.assertClientCanViewProject @client, (error) ->
  35. expect(error).to.be.null
  36. done()
  37. it "should return an error with any other privilegeLevel", (done) ->
  38. @client.params.privilege_level = "unknown"
  39. @AuthorizationManager.assertClientCanViewProject @client, (error) ->
  40. error.message.should.equal "not authorized"
  41. done()
  42. describe "assertClientCanEditProject", ->
  43. it "should not allow the readOnly privilegeLevel", (done) ->
  44. @client.params.privilege_level = "readOnly"
  45. @AuthorizationManager.assertClientCanEditProject @client, (error) ->
  46. error.message.should.equal "not authorized"
  47. done()
  48. it "should allow the readAndWrite privilegeLevel", (done) ->
  49. @client.params.privilege_level = "readAndWrite"
  50. @AuthorizationManager.assertClientCanEditProject @client, (error) ->
  51. expect(error).to.be.null
  52. done()
  53. it "should allow the owner privilegeLevel", (done) ->
  54. @client.params.privilege_level = "owner"
  55. @AuthorizationManager.assertClientCanEditProject @client, (error) ->
  56. expect(error).to.be.null
  57. done()
  58. it "should return an error with any other privilegeLevel", (done) ->
  59. @client.params.privilege_level = "unknown"
  60. @AuthorizationManager.assertClientCanEditProject @client, (error) ->
  61. error.message.should.equal "not authorized"
  62. done()
  63. # check doc access for project
  64. describe "assertClientCanViewProjectAndDoc", ->
  65. beforeEach () ->
  66. @doc_id = "12345"
  67. @callback = sinon.stub()
  68. @client.params = {}
  69. describe "when not authorised at the project level", ->
  70. beforeEach () ->
  71. @client.params.privilege_level = "unknown"
  72. it "should not allow access", () ->
  73. @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback
  74. @callback
  75. .calledWith(new Error("not authorised"))
  76. .should.equal true
  77. describe "even when authorised at the doc level", ->
  78. beforeEach (done) ->
  79. @AuthorizationManager.addAccessToDoc @client, @doc_id, done
  80. it "should not allow access", () ->
  81. @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback
  82. @callback
  83. .calledWith(new Error("not authorised"))
  84. .should.equal true
  85. describe "when authorised at the project level", ->
  86. beforeEach () ->
  87. @client.params.privilege_level = "readOnly"
  88. describe "and not authorised at the document level", ->
  89. it "should not allow access", () ->
  90. @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback
  91. @callback
  92. .calledWith(new Error("not authorised"))
  93. .should.equal true
  94. describe "and authorised at the document level", ->
  95. beforeEach (done) ->
  96. @AuthorizationManager.addAccessToDoc @client, @doc_id, done
  97. it "should allow access", () ->
  98. @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback
  99. @callback
  100. .calledWith(null)
  101. .should.equal true
  102. describe "when document authorisation is added and then removed", ->
  103. beforeEach (done) ->
  104. @AuthorizationManager.addAccessToDoc @client, @doc_id, () =>
  105. @AuthorizationManager.removeAccessToDoc @client, @doc_id, done
  106. it "should deny access", () ->
  107. @AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback
  108. @callback
  109. .calledWith(new Error("not authorised"))
  110. .should.equal true
  111. describe "assertClientCanEditProjectAndDoc", ->
  112. beforeEach () ->
  113. @doc_id = "12345"
  114. @callback = sinon.stub()
  115. @client.params = {}
  116. describe "when not authorised at the project level", ->
  117. beforeEach () ->
  118. @client.params.privilege_level = "readOnly"
  119. it "should not allow access", () ->
  120. @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback
  121. @callback
  122. .calledWith(new Error("not authorised"))
  123. .should.equal true
  124. describe "even when authorised at the doc level", ->
  125. beforeEach (done) ->
  126. @AuthorizationManager.addAccessToDoc @client, @doc_id, done
  127. it "should not allow access", () ->
  128. @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback
  129. @callback
  130. .calledWith(new Error("not authorised"))
  131. .should.equal true
  132. describe "when authorised at the project level", ->
  133. beforeEach () ->
  134. @client.params.privilege_level = "readAndWrite"
  135. describe "and not authorised at the document level", ->
  136. it "should not allow access", () ->
  137. @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback
  138. @callback
  139. .calledWith(new Error("not authorised"))
  140. .should.equal true
  141. describe "and authorised at the document level", ->
  142. beforeEach (done) ->
  143. @AuthorizationManager.addAccessToDoc @client, @doc_id, done
  144. it "should allow access", () ->
  145. @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback
  146. @callback
  147. .calledWith(null)
  148. .should.equal true
  149. describe "when document authorisation is added and then removed", ->
  150. beforeEach (done) ->
  151. @AuthorizationManager.addAccessToDoc @client, @doc_id, () =>
  152. @AuthorizationManager.removeAccessToDoc @client, @doc_id, done
  153. it "should deny access", () ->
  154. @AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback
  155. @callback
  156. .calledWith(new Error("not authorised"))
  157. .should.equal true