PermissionsManagerTests.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath =
  4. '../../../../app/src/Features/Authorization/PermissionsManager.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. const { ForbiddenError } = require('../../../../app/src/Features/Errors/Errors')
  7. describe('PermissionsManager', function () {
  8. beforeEach(function () {
  9. this.PermissionsManager = SandboxedModule.require(modulePath, {
  10. requires: {
  11. '../../infrastructure/Modules': (this.Modules = {
  12. promises: {
  13. hooks: {
  14. fire: (this.hooksFire = sinon.stub().resolves([[]])),
  15. },
  16. },
  17. }),
  18. },
  19. })
  20. this.PermissionsManager.registerCapability('capability1', {
  21. default: true,
  22. })
  23. this.PermissionsManager.registerCapability('capability2', {
  24. default: true,
  25. })
  26. this.PermissionsManager.registerCapability('capability3', {
  27. default: true,
  28. })
  29. this.PermissionsManager.registerCapability('capability4', {
  30. default: false,
  31. })
  32. this.PermissionsManager.registerPolicy('openPolicy', {
  33. capability1: true,
  34. capability2: true,
  35. })
  36. this.PermissionsManager.registerPolicy('restrictivePolicy', {
  37. capability1: true,
  38. capability2: false,
  39. })
  40. this.openPolicyResponseSet = [
  41. [
  42. {
  43. managedUsersEnabled: true,
  44. groupPolicy: { openPolicy: true },
  45. },
  46. {
  47. managedUsersEnabled: true,
  48. groupPolicy: { openPolicy: true },
  49. },
  50. ],
  51. ]
  52. this.restrictivePolicyResponseSet = [
  53. [
  54. {
  55. managedUsersEnabled: true,
  56. groupPolicy: { openPolicy: true },
  57. },
  58. {
  59. managedUsersEnabled: true,
  60. groupPolicy: { restrictivePolicy: true },
  61. },
  62. ],
  63. ]
  64. })
  65. describe('validatePolicies', function () {
  66. it('accepts empty object', function () {
  67. expect(() => this.PermissionsManager.validatePolicies({})).not.to.throw
  68. })
  69. it('accepts object with registered policies', function () {
  70. expect(() =>
  71. this.PermissionsManager.validatePolicies({
  72. openPolicy: true,
  73. restrictivePolicy: false,
  74. })
  75. ).not.to.throw
  76. })
  77. it('accepts object with policies containing non-boolean values', function () {
  78. expect(() =>
  79. this.PermissionsManager.validatePolicies({
  80. openPolicy: 1,
  81. })
  82. ).to.throw('policy value must be a boolean: openPolicy = 1')
  83. expect(() =>
  84. this.PermissionsManager.validatePolicies({
  85. openPolicy: undefined,
  86. })
  87. ).to.throw('policy value must be a boolean: openPolicy = undefined')
  88. expect(() =>
  89. this.PermissionsManager.validatePolicies({
  90. openPolicy: null,
  91. })
  92. ).to.throw('policy value must be a boolean: openPolicy = null')
  93. })
  94. it('throws error on object with policies that are not registered', function () {
  95. expect(() =>
  96. this.PermissionsManager.validatePolicies({
  97. openPolicy: true,
  98. unregisteredPolicy: false,
  99. })
  100. ).to.throw('unknown policy: unregisteredPolicy')
  101. })
  102. })
  103. describe('hasPermission', function () {
  104. describe('when no policies apply to the user', function () {
  105. it('should return true if default permission is true', function () {
  106. const groupPolicy = {}
  107. const capability = 'capability1'
  108. const result = this.PermissionsManager.hasPermission(
  109. groupPolicy,
  110. capability
  111. )
  112. expect(result).to.be.true
  113. })
  114. it('should return false if the default permission is false', function () {
  115. const groupPolicy = {}
  116. const capability = 'capability4'
  117. const result = this.PermissionsManager.hasPermission(
  118. groupPolicy,
  119. capability
  120. )
  121. expect(result).to.be.false
  122. })
  123. })
  124. describe('when a policy applies to the user', function () {
  125. it('should return true if the user has the capability after the policy is applied', function () {
  126. this.PermissionsManager.registerPolicy('policy', {
  127. capability1: true,
  128. capability2: false,
  129. })
  130. const groupPolicy = {
  131. policy: true,
  132. }
  133. const capability = 'capability1'
  134. const result = this.PermissionsManager.hasPermission(
  135. groupPolicy,
  136. capability
  137. )
  138. expect(result).to.be.true
  139. })
  140. it('should return false if the user does not have the capability after the policy is applied', function () {
  141. this.PermissionsManager.registerPolicy('policy', {
  142. capability1: true,
  143. capability2: false,
  144. })
  145. const groupPolicy = {
  146. policy: true,
  147. }
  148. const capability = 'capability2'
  149. const result = this.PermissionsManager.hasPermission(
  150. groupPolicy,
  151. capability
  152. )
  153. expect(result).to.be.false
  154. })
  155. it('should return the default permission if the policy does not apply to the capability', function () {
  156. this.PermissionsManager.registerPolicy('policy', {
  157. capability1: true,
  158. capability2: false,
  159. })
  160. const groupPolicy = {
  161. policy: true,
  162. }
  163. {
  164. const capability = 'capability3'
  165. const result = this.PermissionsManager.hasPermission(
  166. groupPolicy,
  167. capability
  168. )
  169. expect(result).to.be.true
  170. }
  171. {
  172. const capability = 'capability4'
  173. const result = this.PermissionsManager.hasPermission(
  174. groupPolicy,
  175. capability
  176. )
  177. expect(result).to.be.false
  178. }
  179. })
  180. it('should return the default permission if the policy is not enforced', function () {
  181. this.PermissionsManager.registerPolicy('policy', {
  182. capability1: true,
  183. capability2: false,
  184. })
  185. const groupPolicy = {
  186. policy: false,
  187. }
  188. const capability1 = 'capability1'
  189. const result1 = this.PermissionsManager.hasPermission(
  190. groupPolicy,
  191. capability1
  192. )
  193. const capability2 = 'capability2'
  194. const result2 = this.PermissionsManager.hasPermission(
  195. groupPolicy,
  196. capability2
  197. )
  198. expect(result1).to.be.true
  199. expect(result2).to.be.true
  200. })
  201. })
  202. describe('when multiple policies apply to the user', function () {
  203. it('should return true if all policies allow the capability', function () {
  204. this.PermissionsManager.registerPolicy('policy1', {
  205. capability1: true,
  206. capability2: true,
  207. })
  208. this.PermissionsManager.registerPolicy('policy2', {
  209. capability1: true,
  210. capability2: true,
  211. })
  212. const groupPolicy = {
  213. policy1: true,
  214. policy2: true,
  215. }
  216. const capability = 'capability1'
  217. const result = this.PermissionsManager.hasPermission(
  218. groupPolicy,
  219. capability
  220. )
  221. expect(result).to.be.true
  222. })
  223. it('should return false if any policy denies the capability', function () {
  224. this.PermissionsManager.registerPolicy('policy1', {
  225. capability1: true,
  226. capability2: true,
  227. })
  228. this.PermissionsManager.registerPolicy('policy2', {
  229. capability1: false,
  230. capability2: true,
  231. })
  232. const groupPolicy = {
  233. policy1: true,
  234. policy2: true,
  235. }
  236. const capability = 'capability1'
  237. const result = this.PermissionsManager.hasPermission(
  238. groupPolicy,
  239. capability
  240. )
  241. expect(result).to.be.false
  242. })
  243. it('should return the default permssion when the applicable policy is not enforced', function () {
  244. this.PermissionsManager.registerPolicy('policy1', {
  245. capability1: true,
  246. capability2: true,
  247. })
  248. this.PermissionsManager.registerPolicy('policy2', {
  249. capability1: false,
  250. capability2: true,
  251. })
  252. const groupPolicy = {
  253. policy1: true,
  254. policy2: false,
  255. }
  256. const capability = 'capability1'
  257. const result = this.PermissionsManager.hasPermission(
  258. groupPolicy,
  259. capability
  260. )
  261. expect(result).to.be.true
  262. })
  263. it('should return the default permission if the policies do not restrict to the capability', function () {
  264. this.PermissionsManager.registerPolicy('policy', {
  265. capability1: true,
  266. capability2: false,
  267. })
  268. const groupPolicy = {
  269. policy: true,
  270. }
  271. {
  272. const capability = 'capability3'
  273. const result = this.PermissionsManager.hasPermission(
  274. groupPolicy,
  275. capability
  276. )
  277. expect(result).to.be.true
  278. }
  279. {
  280. const capability = 'capability4'
  281. const result = this.PermissionsManager.hasPermission(
  282. groupPolicy,
  283. capability
  284. )
  285. expect(result).to.be.false
  286. }
  287. })
  288. })
  289. })
  290. describe('getUserCapabilities', function () {
  291. it('should return the default capabilities when no group policy is provided', function () {
  292. const groupPolicy = {}
  293. const capabilities =
  294. this.PermissionsManager.getUserCapabilities(groupPolicy)
  295. expect(capabilities).to.deep.equal(
  296. new Set(['capability1', 'capability2', 'capability3'])
  297. )
  298. })
  299. it('should return a reduced capability set when a group policy is provided', function () {
  300. this.PermissionsManager.registerPolicy('policy', {
  301. capability1: true,
  302. capability2: false,
  303. })
  304. const groupPolicy = {
  305. policy: true,
  306. }
  307. const capabilities =
  308. this.PermissionsManager.getUserCapabilities(groupPolicy)
  309. expect(capabilities).to.deep.equal(
  310. new Set(['capability1', 'capability3'])
  311. )
  312. })
  313. it('should return a reduced capability set when multiple group policies are provided', function () {
  314. this.PermissionsManager.registerPolicy('policy1', {
  315. capability1: true,
  316. capability2: false,
  317. })
  318. this.PermissionsManager.registerPolicy('policy2', {
  319. capability1: false,
  320. capability2: true,
  321. })
  322. const groupPolicy = {
  323. policy1: true,
  324. policy2: true,
  325. }
  326. const capabilities =
  327. this.PermissionsManager.getUserCapabilities(groupPolicy)
  328. expect(capabilities).to.deep.equal(new Set(['capability3']))
  329. })
  330. it('should return an empty capability set when group policies remove all permissions', function () {
  331. this.PermissionsManager.registerPolicy('policy1', {
  332. capability1: true,
  333. capability2: false,
  334. })
  335. this.PermissionsManager.registerPolicy('policy2', {
  336. capability1: false,
  337. capability2: true,
  338. })
  339. this.PermissionsManager.registerPolicy('policy3', {
  340. capability1: true,
  341. capability2: true,
  342. capability3: false,
  343. })
  344. const groupPolicy = {
  345. policy1: true,
  346. policy2: true,
  347. policy3: true,
  348. }
  349. const capabilities =
  350. this.PermissionsManager.getUserCapabilities(groupPolicy)
  351. expect(capabilities).to.deep.equal(new Set())
  352. })
  353. })
  354. describe('getUserValidationStatus', function () {
  355. it('should return the status for the policy when the user conforms', async function () {
  356. this.PermissionsManager.registerPolicy(
  357. 'policy',
  358. {},
  359. {
  360. validator: async ({ user, subscription }) => {
  361. return user.prop === 'allowed' && subscription.prop === 'managed'
  362. },
  363. }
  364. )
  365. const groupPolicy = {
  366. policy: true,
  367. }
  368. const user = { prop: 'allowed' }
  369. const subscription = { prop: 'managed' }
  370. const result =
  371. await this.PermissionsManager.promises.getUserValidationStatus({
  372. user,
  373. groupPolicy,
  374. subscription,
  375. })
  376. expect(result).to.deep.equal(new Map([['policy', true]]))
  377. })
  378. it('should return the status for the policy when the user does not conform', async function () {
  379. this.PermissionsManager.registerPolicy(
  380. 'policy',
  381. {},
  382. {
  383. validator: async ({ user, subscription }) => {
  384. return user.prop === 'allowed' && subscription.prop === 'managed'
  385. },
  386. }
  387. )
  388. const groupPolicy = {
  389. policy: true,
  390. }
  391. const user = { prop: 'not allowed' }
  392. const subscription = { prop: 'managed' }
  393. const result =
  394. await this.PermissionsManager.promises.getUserValidationStatus({
  395. user,
  396. groupPolicy,
  397. subscription,
  398. })
  399. expect(result).to.deep.equal(new Map([['policy', false]]))
  400. })
  401. it('should return the status for multiple policies according to whether the user conforms', async function () {
  402. this.PermissionsManager.registerPolicy(
  403. 'policy1',
  404. {},
  405. {
  406. validator: async ({ user, subscription }) => {
  407. return user.prop === 'allowed' && subscription.prop === 'managed'
  408. },
  409. }
  410. )
  411. this.PermissionsManager.registerPolicy(
  412. 'policy2',
  413. {},
  414. {
  415. validator: async ({ user, subscription }) => {
  416. return user.prop === 'other' && subscription.prop === 'managed'
  417. },
  418. }
  419. )
  420. this.PermissionsManager.registerPolicy(
  421. 'policy3',
  422. {},
  423. {
  424. validator: async ({ user, subscription }) => {
  425. return user.prop === 'allowed' && subscription.prop === 'managed'
  426. },
  427. }
  428. )
  429. const groupPolicy = {
  430. policy1: true,
  431. policy2: true,
  432. policy3: false, // this policy is not enforced
  433. }
  434. const user = { prop: 'allowed' }
  435. const subscription = { prop: 'managed' }
  436. const result =
  437. await this.PermissionsManager.promises.getUserValidationStatus({
  438. user,
  439. groupPolicy,
  440. subscription,
  441. })
  442. expect(result).to.deep.equal(
  443. new Map([
  444. ['policy1', true],
  445. ['policy2', false],
  446. ])
  447. )
  448. })
  449. })
  450. describe('assertUserPermissions', function () {
  451. describe('allowed', function () {
  452. it('should not error when managedUsersEnabled is not enabled for user', async function () {
  453. const result =
  454. await this.PermissionsManager.promises.assertUserPermissions(
  455. { _id: 'user123' },
  456. ['add-secondary-email']
  457. )
  458. expect(result).to.be.undefined
  459. })
  460. it('should not error when default capability is true', async function () {
  461. this.PermissionsManager.registerCapability('some-policy-to-check', {
  462. default: true,
  463. })
  464. this.hooksFire.resolves([
  465. [
  466. {
  467. managedUsersEnabled: true,
  468. groupPolicy: {},
  469. },
  470. ],
  471. ])
  472. const result =
  473. await this.PermissionsManager.promises.assertUserPermissions(
  474. { _id: 'user123' },
  475. ['some-policy-to-check']
  476. )
  477. expect(result).to.be.undefined
  478. })
  479. it('should not error when default permission is false but user has permission', async function () {
  480. this.PermissionsManager.registerCapability('some-policy-to-check', {
  481. default: false,
  482. })
  483. this.PermissionsManager.registerPolicy('userCanDoSomePolicy', {
  484. 'some-policy-to-check': true,
  485. })
  486. this.hooksFire.resolves([
  487. [
  488. {
  489. managedUsersEnabled: true,
  490. groupPolicy: {
  491. userCanDoSomePolicy: true,
  492. },
  493. },
  494. ],
  495. ])
  496. const result =
  497. await this.PermissionsManager.promises.assertUserPermissions(
  498. { _id: 'user123' },
  499. ['some-policy-to-check']
  500. )
  501. expect(result).to.be.undefined
  502. })
  503. })
  504. describe('not allowed', function () {
  505. it('should return error when managedUsersEnabled is enabled for user but there is no group policy', async function () {
  506. this.hooksFire.resolves([[{ managedUsersEnabled: true }]])
  507. await expect(
  508. this.PermissionsManager.promises.assertUserPermissions(
  509. { _id: 'user123' },
  510. ['add-secondary-email']
  511. )
  512. ).to.be.rejectedWith(Error, 'unknown capability: add-secondary-email')
  513. })
  514. it('should return error when default permission is false', async function () {
  515. this.PermissionsManager.registerCapability('some-policy-to-check', {
  516. default: false,
  517. })
  518. this.hooksFire.resolves([
  519. [
  520. {
  521. managedUsersEnabled: true,
  522. groupPolicy: {},
  523. },
  524. ],
  525. ])
  526. await expect(
  527. this.PermissionsManager.promises.assertUserPermissions(
  528. { _id: 'user123' },
  529. ['some-policy-to-check']
  530. )
  531. ).to.be.rejectedWith(ForbiddenError)
  532. })
  533. it('should return error when default permission is true but user does not have permission', async function () {
  534. this.PermissionsManager.registerCapability('some-policy-to-check', {
  535. default: true,
  536. })
  537. this.PermissionsManager.registerPolicy('userCannotDoSomePolicy', {
  538. 'some-policy-to-check': false,
  539. })
  540. this.hooksFire.resolves([
  541. [
  542. {
  543. managedUsersEnabled: true,
  544. groupPolicy: { userCannotDoSomePolicy: true },
  545. },
  546. ],
  547. ])
  548. await expect(
  549. this.PermissionsManager.promises.assertUserPermissions(
  550. { _id: 'user123' },
  551. ['some-policy-to-check']
  552. )
  553. ).to.be.rejectedWith(ForbiddenError)
  554. })
  555. })
  556. })
  557. describe('registerAllowedProperty', function () {
  558. it('allows us to register a property', async function () {
  559. this.PermissionsManager.registerAllowedProperty('metadata1')
  560. const result = await this.PermissionsManager.getAllowedProperties()
  561. expect(result).to.deep.equal(new Set(['metadata1']))
  562. })
  563. // used if multiple modules would require the same prop, since we dont know which will load first, both must register
  564. it('should handle multiple registrations of the same property', async function () {
  565. this.PermissionsManager.registerAllowedProperty('metadata1')
  566. this.PermissionsManager.registerAllowedProperty('metadata1')
  567. const result = await this.PermissionsManager.getAllowedProperties()
  568. expect(result).to.deep.equal(new Set(['metadata1']))
  569. })
  570. })
  571. describe('combineAllowedProperties', function () {
  572. it('should handle multiple occurences of the same property, preserving the first occurence', async function () {
  573. const policy1 = {
  574. groupPolicy: {
  575. policy: false,
  576. },
  577. prop1: 'some other value here',
  578. }
  579. const policy2 = {
  580. groupPolicy: {
  581. policy: false,
  582. },
  583. prop1: 'some value here',
  584. }
  585. const results = [policy1, policy2]
  586. this.PermissionsManager.registerAllowedProperty('prop1')
  587. const combinedProps =
  588. this.PermissionsManager.combineAllowedProperties(results)
  589. expect(combinedProps).to.deep.equal({
  590. prop1: 'some other value here',
  591. })
  592. })
  593. it('should add registered properties to the set', async function () {
  594. const policy = {
  595. groupPolicy: {
  596. policy: false,
  597. },
  598. prop1: 'some value here',
  599. propNotMeThough: 'dont copy please',
  600. }
  601. const policy2 = {
  602. groupPolicy: {
  603. policy: false,
  604. },
  605. prop2: 'some value here',
  606. }
  607. const results = [policy, policy2]
  608. this.PermissionsManager.registerAllowedProperty('prop1')
  609. this.PermissionsManager.registerAllowedProperty('prop2')
  610. const combinedProps =
  611. this.PermissionsManager.combineAllowedProperties(results)
  612. expect(combinedProps).to.deep.equal({
  613. prop1: 'some value here',
  614. prop2: 'some value here',
  615. })
  616. })
  617. it('should not add unregistered properties to the req object', async function () {
  618. const policy = {
  619. groupPolicy: {
  620. policy: false,
  621. },
  622. prop1: 'some value here',
  623. }
  624. const policy2 = {
  625. groupPolicy: {
  626. policy: false,
  627. },
  628. prop2: 'some value here',
  629. }
  630. this.PermissionsManager.registerAllowedProperty('prop1')
  631. const results = [policy, policy2]
  632. const combinedProps =
  633. this.PermissionsManager.combineAllowedProperties(results)
  634. expect(combinedProps).to.deep.equal({ prop1: 'some value here' })
  635. })
  636. it('should handle an empty array', async function () {
  637. const results = []
  638. const combinedProps =
  639. this.PermissionsManager.combineAllowedProperties(results)
  640. expect(combinedProps).to.deep.equal({})
  641. })
  642. })
  643. describe('combineGroupPolicies', function () {
  644. it('should return an empty object when an empty array is passed', async function () {
  645. const results = []
  646. const combinedPolicy =
  647. this.PermissionsManager.combineGroupPolicies(results)
  648. expect(combinedPolicy).to.deep.equal({})
  649. })
  650. it('should combine multiple group policies into a single policy object', async function () {
  651. const groupPolicy = {
  652. policy1: true,
  653. }
  654. const groupPolicy2 = {
  655. policy2: false,
  656. policy3: true,
  657. }
  658. this.PermissionsManager.registerAllowedProperty('prop1')
  659. const results = [groupPolicy, groupPolicy2]
  660. const combinedPolicy =
  661. this.PermissionsManager.combineGroupPolicies(results)
  662. expect(combinedPolicy).to.deep.equal({
  663. policy1: true,
  664. policy3: true,
  665. })
  666. })
  667. it('should handle duplicate enforced policies across different group policies', async function () {
  668. const groupPolicy = {
  669. policy1: false,
  670. policy2: true,
  671. }
  672. const groupPolicy2 = {
  673. policy2: true,
  674. policy3: true,
  675. }
  676. this.PermissionsManager.registerAllowedProperty('prop1')
  677. const results = [groupPolicy, groupPolicy2]
  678. const combinedPolicy =
  679. this.PermissionsManager.combineGroupPolicies(results)
  680. expect(combinedPolicy).to.deep.equal({
  681. policy2: true,
  682. policy3: true,
  683. })
  684. })
  685. it('should handle group policies with no enforced policies', async function () {
  686. const groupPolicy = {
  687. policy1: false,
  688. policy2: false,
  689. }
  690. const groupPolicy2 = {
  691. policy2: false,
  692. policy3: true,
  693. }
  694. this.PermissionsManager.registerAllowedProperty('prop1')
  695. const results = [groupPolicy, groupPolicy2]
  696. const combinedPolicy =
  697. this.PermissionsManager.combineGroupPolicies(results)
  698. expect(combinedPolicy).to.deep.equal({ policy3: true })
  699. })
  700. it('should choose the stricter option between two policy values', async function () {
  701. const groupPolicy = {
  702. policy1: false,
  703. policy2: true,
  704. policy4: true,
  705. }
  706. const groupPolicy2 = {
  707. policy2: false,
  708. policy3: true,
  709. policy4: false,
  710. }
  711. this.PermissionsManager.registerAllowedProperty('prop1')
  712. const results = [groupPolicy, groupPolicy2]
  713. const combinedPolicy =
  714. this.PermissionsManager.combineGroupPolicies(results)
  715. expect(combinedPolicy).to.deep.equal({
  716. policy2: true,
  717. policy3: true,
  718. policy4: true,
  719. })
  720. })
  721. })
  722. describe('checkUserListPermissions', function () {
  723. it('should return true when all users have permissions required', async function () {
  724. const userList = ['user1', 'user2', 'user3']
  725. const capabilities = ['capability1', 'capability2']
  726. this.hooksFire.onCall(0).resolves(this.openPolicyResponseSet)
  727. this.hooksFire.onCall(1).resolves(this.openPolicyResponseSet)
  728. this.hooksFire.onCall(2).resolves(this.openPolicyResponseSet)
  729. const usersHavePermission =
  730. await this.PermissionsManager.promises.checkUserListPermissions(
  731. userList,
  732. capabilities
  733. )
  734. expect(usersHavePermission).to.equal(true)
  735. })
  736. it('should return false if any user does not have permission', async function () {
  737. const userList = ['user1', 'user2', 'user3']
  738. const capabilities = ['capability1', 'capability2']
  739. this.hooksFire.onCall(0).resolves(this.openPolicyResponseSet)
  740. this.hooksFire.onCall(1).resolves(this.restrictivePolicyResponseSet)
  741. this.hooksFire.onCall(2).resolves(this.openPolicyResponseSet)
  742. const usersHavePermission =
  743. await this.PermissionsManager.promises.checkUserListPermissions(
  744. userList,
  745. capabilities
  746. )
  747. expect(usersHavePermission).to.equal(false)
  748. })
  749. })
  750. })