AuthorizationManagerTests.js 9.5 KB

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