AuthorizationManagerTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const chai = require('chai')
  13. chai.should()
  14. const { expect } = chai
  15. const sinon = require('sinon')
  16. const SandboxedModule = require('sandboxed-module')
  17. const path = require('path')
  18. const modulePath = '../../../app/js/AuthorizationManager'
  19. describe('AuthorizationManager', function () {
  20. beforeEach(function () {
  21. this.client = { ol_context: {} }
  22. return (this.AuthorizationManager = SandboxedModule.require(modulePath, {
  23. requires: {}
  24. }))
  25. })
  26. describe('assertClientCanViewProject', function () {
  27. it('should allow the readOnly privilegeLevel', function (done) {
  28. this.client.ol_context.privilege_level = 'readOnly'
  29. return this.AuthorizationManager.assertClientCanViewProject(
  30. this.client,
  31. (error) => {
  32. expect(error).to.be.null
  33. return done()
  34. }
  35. )
  36. })
  37. it('should allow the readAndWrite privilegeLevel', function (done) {
  38. this.client.ol_context.privilege_level = 'readAndWrite'
  39. return this.AuthorizationManager.assertClientCanViewProject(
  40. this.client,
  41. (error) => {
  42. expect(error).to.be.null
  43. return done()
  44. }
  45. )
  46. })
  47. it('should allow the owner privilegeLevel', function (done) {
  48. this.client.ol_context.privilege_level = 'owner'
  49. return this.AuthorizationManager.assertClientCanViewProject(
  50. this.client,
  51. (error) => {
  52. expect(error).to.be.null
  53. return done()
  54. }
  55. )
  56. })
  57. return it('should return an error with any other privilegeLevel', function (done) {
  58. this.client.ol_context.privilege_level = 'unknown'
  59. return this.AuthorizationManager.assertClientCanViewProject(
  60. this.client,
  61. (error) => {
  62. error.message.should.equal('not authorized')
  63. return done()
  64. }
  65. )
  66. })
  67. })
  68. describe('assertClientCanEditProject', function () {
  69. it('should not allow the readOnly privilegeLevel', function (done) {
  70. this.client.ol_context.privilege_level = 'readOnly'
  71. return this.AuthorizationManager.assertClientCanEditProject(
  72. this.client,
  73. (error) => {
  74. error.message.should.equal('not authorized')
  75. return done()
  76. }
  77. )
  78. })
  79. it('should allow the readAndWrite privilegeLevel', function (done) {
  80. this.client.ol_context.privilege_level = 'readAndWrite'
  81. return this.AuthorizationManager.assertClientCanEditProject(
  82. this.client,
  83. (error) => {
  84. expect(error).to.be.null
  85. return done()
  86. }
  87. )
  88. })
  89. it('should allow the owner privilegeLevel', function (done) {
  90. this.client.ol_context.privilege_level = 'owner'
  91. return this.AuthorizationManager.assertClientCanEditProject(
  92. this.client,
  93. (error) => {
  94. expect(error).to.be.null
  95. return done()
  96. }
  97. )
  98. })
  99. return it('should return an error with any other privilegeLevel', function (done) {
  100. this.client.ol_context.privilege_level = 'unknown'
  101. return this.AuthorizationManager.assertClientCanEditProject(
  102. this.client,
  103. (error) => {
  104. error.message.should.equal('not authorized')
  105. return done()
  106. }
  107. )
  108. })
  109. })
  110. // check doc access for project
  111. describe('assertClientCanViewProjectAndDoc', function () {
  112. beforeEach(function () {
  113. this.doc_id = '12345'
  114. this.callback = sinon.stub()
  115. return (this.client.ol_context = {})
  116. })
  117. describe('when not authorised at the project level', function () {
  118. beforeEach(function () {
  119. return (this.client.ol_context.privilege_level = 'unknown')
  120. })
  121. it('should not allow access', function () {
  122. return this.AuthorizationManager.assertClientCanViewProjectAndDoc(
  123. this.client,
  124. this.doc_id,
  125. (err) => err.message.should.equal('not authorized')
  126. )
  127. })
  128. return describe('even when authorised at the doc level', function () {
  129. beforeEach(function (done) {
  130. return this.AuthorizationManager.addAccessToDoc(
  131. this.client,
  132. this.doc_id,
  133. done
  134. )
  135. })
  136. return it('should not allow access', function () {
  137. return this.AuthorizationManager.assertClientCanViewProjectAndDoc(
  138. this.client,
  139. this.doc_id,
  140. (err) => err.message.should.equal('not authorized')
  141. )
  142. })
  143. })
  144. })
  145. return describe('when authorised at the project level', function () {
  146. beforeEach(function () {
  147. return (this.client.ol_context.privilege_level = 'readOnly')
  148. })
  149. describe('and not authorised at the document level', function () {
  150. return it('should not allow access', function () {
  151. return this.AuthorizationManager.assertClientCanViewProjectAndDoc(
  152. this.client,
  153. this.doc_id,
  154. (err) => err.message.should.equal('not authorized')
  155. )
  156. })
  157. })
  158. describe('and authorised at the document level', function () {
  159. beforeEach(function (done) {
  160. return this.AuthorizationManager.addAccessToDoc(
  161. this.client,
  162. this.doc_id,
  163. done
  164. )
  165. })
  166. return it('should allow access', function () {
  167. this.AuthorizationManager.assertClientCanViewProjectAndDoc(
  168. this.client,
  169. this.doc_id,
  170. this.callback
  171. )
  172. return this.callback.calledWith(null).should.equal(true)
  173. })
  174. })
  175. return describe('when document authorisation is added and then removed', function () {
  176. beforeEach(function (done) {
  177. return this.AuthorizationManager.addAccessToDoc(
  178. this.client,
  179. this.doc_id,
  180. () => {
  181. return this.AuthorizationManager.removeAccessToDoc(
  182. this.client,
  183. this.doc_id,
  184. done
  185. )
  186. }
  187. )
  188. })
  189. return it('should deny access', function () {
  190. return this.AuthorizationManager.assertClientCanViewProjectAndDoc(
  191. this.client,
  192. this.doc_id,
  193. (err) => err.message.should.equal('not authorized')
  194. )
  195. })
  196. })
  197. })
  198. })
  199. return describe('assertClientCanEditProjectAndDoc', function () {
  200. beforeEach(function () {
  201. this.doc_id = '12345'
  202. this.callback = sinon.stub()
  203. return (this.client.ol_context = {})
  204. })
  205. describe('when not authorised at the project level', function () {
  206. beforeEach(function () {
  207. return (this.client.ol_context.privilege_level = 'readOnly')
  208. })
  209. it('should not allow access', function () {
  210. return this.AuthorizationManager.assertClientCanEditProjectAndDoc(
  211. this.client,
  212. this.doc_id,
  213. (err) => err.message.should.equal('not authorized')
  214. )
  215. })
  216. return describe('even when authorised at the doc level', function () {
  217. beforeEach(function (done) {
  218. return this.AuthorizationManager.addAccessToDoc(
  219. this.client,
  220. this.doc_id,
  221. done
  222. )
  223. })
  224. return it('should not allow access', function () {
  225. return this.AuthorizationManager.assertClientCanEditProjectAndDoc(
  226. this.client,
  227. this.doc_id,
  228. (err) => err.message.should.equal('not authorized')
  229. )
  230. })
  231. })
  232. })
  233. return describe('when authorised at the project level', function () {
  234. beforeEach(function () {
  235. return (this.client.ol_context.privilege_level = 'readAndWrite')
  236. })
  237. describe('and not authorised at the document level', function () {
  238. return it('should not allow access', function () {
  239. return this.AuthorizationManager.assertClientCanEditProjectAndDoc(
  240. this.client,
  241. this.doc_id,
  242. (err) => err.message.should.equal('not authorized')
  243. )
  244. })
  245. })
  246. describe('and authorised at the document level', function () {
  247. beforeEach(function (done) {
  248. return this.AuthorizationManager.addAccessToDoc(
  249. this.client,
  250. this.doc_id,
  251. done
  252. )
  253. })
  254. return it('should allow access', function () {
  255. this.AuthorizationManager.assertClientCanEditProjectAndDoc(
  256. this.client,
  257. this.doc_id,
  258. this.callback
  259. )
  260. return this.callback.calledWith(null).should.equal(true)
  261. })
  262. })
  263. return describe('when document authorisation is added and then removed', function () {
  264. beforeEach(function (done) {
  265. return this.AuthorizationManager.addAccessToDoc(
  266. this.client,
  267. this.doc_id,
  268. () => {
  269. return this.AuthorizationManager.removeAccessToDoc(
  270. this.client,
  271. this.doc_id,
  272. done
  273. )
  274. }
  275. )
  276. })
  277. return it('should deny access', function () {
  278. return this.AuthorizationManager.assertClientCanEditProjectAndDoc(
  279. this.client,
  280. this.doc_id,
  281. (err) => err.message.should.equal('not authorized')
  282. )
  283. })
  284. })
  285. })
  286. })
  287. })