AuthorizationManagerTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Security/AuthorizationManager'
  5. MockClient = require "../helpers/MockClient"
  6. describe "AuthorizationManager", ->
  7. beforeEach ->
  8. @client = new MockClient()
  9. @AuthorizationManager = SandboxedModule.require modulePath, requires:
  10. '../../managers/SecurityManager':{}
  11. describe "ensureClientCanViewProject", ->
  12. beforeEach ->
  13. @client.set("project_id", "project-id")
  14. it "should let the request through for a readOnly privilege", (done) ->
  15. @client.set("privilege_level", "readOnly")
  16. @AuthorizationManager.ensureClientCanViewProject @client, done
  17. it "should let the request through for a readAndWrite privilege", (done) ->
  18. @client.set("privilege_level", "readAndWrite")
  19. @AuthorizationManager.ensureClientCanViewProject @client, done
  20. it "should let the request through for a owner privilege", (done) ->
  21. @client.set("privilege_level", "owner")
  22. @AuthorizationManager.ensureClientCanViewProject @client, done
  23. it "should ignore an empty privilege", ->
  24. @AuthorizationManager.ensureClientCanViewProject @client, () ->
  25. throw new Error("Should not be called")
  26. describe "ensureClientCanEditProject", ->
  27. beforeEach ->
  28. @client.set("project_id", "project-id")
  29. it "should ignore a readOnly privilege", ->
  30. @client.set("privilege_level", "readOnly")
  31. @AuthorizationManager.ensureClientCanEditProject @client, () ->
  32. throw new Error("Should not be called")
  33. it "should let the request through for a readAndWrite privilege", (done) ->
  34. @client.set("privilege_level", "readAndWrite")
  35. @AuthorizationManager.ensureClientCanEditProject @client, done
  36. it "should let the request through for a owner privilege", (done) ->
  37. @client.set("privilege_level", "owner")
  38. @AuthorizationManager.ensureClientCanEditProject @client, done
  39. it "should ignore an empty privilege", ->
  40. @AuthorizationManager.ensureClientCanEditProject @client, () ->
  41. throw new Error("Should not be called")
  42. describe "ensureClientCanAdminProject", ->
  43. beforeEach ->
  44. @client.set("project_id", "project-id")
  45. it "should ignore a readOnly privilege", ->
  46. @client.set("privilege_level", "readOnly")
  47. @AuthorizationManager.ensureClientCanAdminProject @client, () ->
  48. throw new Error("Should not be called")
  49. it "should ignore a readAndWrite privilege", ->
  50. @client.set("privilege_level", "readAndWrite")
  51. @AuthorizationManager.ensureClientCanAdminProject @client, () ->
  52. throw new Error("Should not be called")
  53. it "should let the request through for a owner privilege", (done) ->
  54. @client.set("privilege_level", "owner")
  55. @AuthorizationManager.ensureClientCanAdminProject @client, done
  56. it "should ignore an empty privilege", ->
  57. @AuthorizationManager.ensureClientCanAdminProject @client, () ->
  58. throw new Error("Should not be called")
  59. describe "ensureClientHasPrivilegeLevelForProject", ->
  60. it "should ignore callback if privilege_level is not set", ->
  61. @client.set("project_id", "project-id")
  62. @AuthorizationManager.ensureClientHasPrivilegeLevelForProject @client,
  63. ["owner"], (error, project_id) ->
  64. throw new Error("Should not be called")
  65. it "should ignore callback if project_id is not set", ->
  66. @client.set("privilege_level", "owner")
  67. @AuthorizationManager.ensureClientHasPrivilegeLevelForProject @client,
  68. ["owner"], (error, project_id) ->
  69. throw new Error("Should not be called")
  70. it "should return the project_id", (done) ->
  71. @client.set("privilege_level", "owner")
  72. @client.set("project_id", "project-id-123")
  73. @AuthorizationManager.ensureClientHasPrivilegeLevelForProject @client,
  74. ["owner"], (error, project_id) ->
  75. project_id.should.equal "project-id-123"
  76. done()