This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shogun
committed
Mar 30, 2018
1 parent
030bec5
commit ef1f278
Showing
5 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const expect = require('code').expect | ||
const Lab = require('lab') | ||
const lab = exports.lab = Lab.script() | ||
const udaru = require('../..')() | ||
|
||
const organizationId = 'WONKA' | ||
|
||
lab.experiment('Promises', () => { | ||
let testUserId | ||
|
||
lab.before((done) => { | ||
udaru.policies.list({organizationId}, (err, policies) => { | ||
if (err) return done(err) | ||
|
||
udaru.users.create({name: 'Salman', organizationId}, (err, result) => { | ||
if (err) return done(err) | ||
|
||
testUserId = result.id | ||
udaru.users.replacePolicies({ id: testUserId, policies: [_.find(policies, {name: 'Director'}).id], organizationId }, done) | ||
}) | ||
}) | ||
}) | ||
|
||
lab.after((done) => { | ||
udaru.users.delete({id: testUserId, organizationId: 'WONKA'}, done) | ||
}) | ||
|
||
lab.afterEach((done) => { | ||
udaru.clearHook('authorize:isUserAuthorized') | ||
done() | ||
}) | ||
|
||
lab.test('should support callback based access', (done) => { | ||
udaru.authorize.isUserAuthorized({userId: testUserId, resource: 'database:pg01:balancesheet', action: 'finance:ReadBalanceSheet', organizationId}, (err, result) => { | ||
if (err) return done(err) | ||
|
||
expect(err).to.not.exist() | ||
expect(result.access).to.be.true() | ||
|
||
done() | ||
}) | ||
}) | ||
|
||
lab.test('should support promise based access and return their results', (done) => { | ||
udaru.authorize.isUserAuthorized({userId: testUserId, resource: 'database:pg01:balancesheet', action: 'finance:ReadBalanceSheet', organizationId}) | ||
.then(result => { | ||
expect(result.access).to.be.true() | ||
|
||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
|
||
lab.test('should support promise based access and return their errors', (done) => { | ||
udaru.authorize.isUserAuthorized({userId: testUserId, organizationId}) | ||
.catch(err => { | ||
expect(err).to.exist() | ||
expect(err).to.be.an.error('child "action" fails because ["action" is required]') | ||
done() | ||
}) | ||
}) | ||
|
||
lab.test('should support promise based access and return their errors, executing hooks', (done) => { | ||
let handlerArgs = {} | ||
|
||
const handler = function (error, input, result, cb) { | ||
handlerArgs = [error, input, result] | ||
cb() | ||
} | ||
|
||
udaru.addHook('authorize:isUserAuthorized', handler) | ||
|
||
udaru.authorize.isUserAuthorized({userId: testUserId, organizationId}) | ||
.catch(err => { | ||
expect(err).to.exist() | ||
expect(handlerArgs[0]).to.be.an.error('child "action" fails because ["action" is required]') | ||
done() | ||
}) | ||
}) | ||
|
||
lab.test('should support promise base access and handle hooks errors', (done) => { | ||
udaru.addHook('authorize:isUserAuthorized', (...args) => { | ||
args.pop()(new Error('hook error')) | ||
}) | ||
|
||
udaru.authorize.isUserAuthorized({userId: testUserId, resource: 'database:pg01:balancesheet', action: 'finance:ReadBalanceSheet', organizationId}) | ||
.catch(err => { | ||
expect(err).to.be.an.error('hook error') | ||
done() | ||
}) | ||
}) | ||
}) |