Skip to content

Commit 4f5ed78

Browse files
committed
Convert checkAccess to promise.
1 parent fb7235d commit 4f5ed78

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

lib/acl-checker.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ class ACLChecker {
4646
resource, // The resource we want to access
4747
accessType, // accessTo or defaultForNew
4848
acl, // The current Acl file!
49-
(err) => { return next(!err || err) },
5049
options
51-
)
50+
).then(next, next)
5251
})
5352
},
5453
function handleNoAccess (err) {
@@ -87,17 +86,15 @@ class ACLChecker {
8786
* @param resource {String} URI of the resource being accessed
8887
* @param accessType {String} One of `accessTo`, or `default`
8988
* @param acl {String} URI of this current .acl resource
90-
* @param callback {Function}
9189
* @param options {Object} Options hashmap
9290
* @param [options.origin] Request's `Origin:` header
9391
* @param [options.host] Request's host URI (with protocol)
9492
*/
95-
checkAccess (graph, user, mode, resource, accessType, acl, callback,
96-
options = {}) {
93+
checkAccess (graph, user, mode, resource, accessType, acl, options = {}) {
9794
const debug = this.debug
9895
if (!graph || graph.length === 0) {
9996
debug('ACL ' + acl + ' is empty')
100-
return callback(new Error('No policy found - empty ACL'))
97+
return Promise.reject(new Error('No policy found - empty ACL'))
10198
}
10299
let isContainer = accessType.startsWith('default')
103100
let aclOptions = {
@@ -111,21 +108,21 @@ class ACLChecker {
111108
aclUrlFor: (uri) => { return this.aclUrlFor(uri) }
112109
}
113110
let acls = new PermissionSet(resource, acl, isContainer, aclOptions)
114-
acls.checkAccess(resource, user, mode)
111+
return acls.checkAccess(resource, user, mode)
115112
.then(hasAccess => {
116113
if (hasAccess) {
117114
debug(`${mode} access permitted to ${user}`)
118-
return callback()
115+
return true
119116
} else {
120117
debug(`${mode} access NOT permitted to ${user}` +
121118
aclOptions.strictOrigin ? ` and origin ${options.origin}` : '')
122-
return callback(new Error('ACL file found but no matching policy found'))
119+
throw new Error('ACL file found but no matching policy found')
123120
}
124121
})
125122
.catch(err => {
126123
debug(`${mode} access denied to ${user}`)
127124
debug(err)
128-
return callback(err)
125+
throw err
129126
})
130127
}
131128

test/unit/acl-checker-test.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict'
22
const proxyquire = require('proxyquire')
3-
const chai = require('chai')
4-
const { assert } = chai
5-
const dirtyChai = require('dirty-chai')
6-
chai.use(dirtyChai)
7-
const sinonChai = require('sinon-chai')
8-
chai.use(sinonChai)
9-
chai.should()
103
const debug = require('../../lib/debug').ACL
4+
const chai = require('chai')
5+
const { expect } = chai
6+
chai.use(require('chai-as-promised'))
117

128
class PermissionSetAlwaysGrant {
139
checkAccess () {
@@ -26,46 +22,37 @@ class PermissionSetAlwaysError {
2622
}
2723

2824
describe('ACLChecker unit test', () => {
29-
it('should callback with null on grant success', done => {
25+
it('should callback with null on grant success', () => {
3026
let ACLChecker = proxyquire('../../lib/acl-checker', {
3127
'solid-permissions': { PermissionSet: PermissionSetAlwaysGrant }
3228
})
3329
let graph = {}
3430
let accessType = ''
3531
let user, mode, resource, aclUrl
3632
let acl = new ACLChecker({ debug })
37-
acl.checkAccess(graph, user, mode, resource, accessType, aclUrl, (err) => {
38-
assert.isUndefined(err,
39-
'Granted permission should result in an empty callback!')
40-
done()
41-
})
33+
return expect(acl.checkAccess(graph, user, mode, resource, accessType, aclUrl))
34+
.to.eventually.be.true
4235
})
43-
it('should callback with error on grant failure', done => {
36+
it('should callback with error on grant failure', () => {
4437
let ACLChecker = proxyquire('../../lib/acl-checker', {
4538
'solid-permissions': { PermissionSet: PermissionSetNeverGrant }
4639
})
4740
let graph = {}
4841
let accessType = ''
4942
let user, mode, resource, aclUrl
5043
let acl = new ACLChecker({ debug })
51-
acl.checkAccess(graph, user, mode, resource, accessType, aclUrl, (err) => {
52-
assert.ok(err instanceof Error,
53-
'Denied permission should result in an error callback!')
54-
done()
55-
})
44+
return expect(acl.checkAccess(graph, user, mode, resource, accessType, aclUrl))
45+
.to.be.rejectedWith('ACL file found but no matching policy found')
5646
})
57-
it('should callback with error on grant error', done => {
47+
it('should callback with error on grant error', () => {
5848
let ACLChecker = proxyquire('../../lib/acl-checker', {
5949
'solid-permissions': { PermissionSet: PermissionSetAlwaysError }
6050
})
6151
let graph = {}
6252
let accessType = ''
6353
let user, mode, resource, aclUrl
6454
let acl = new ACLChecker({ debug })
65-
acl.checkAccess(graph, user, mode, resource, accessType, aclUrl, (err) => {
66-
assert.ok(err instanceof Error,
67-
'Error during checkAccess should result in an error callback!')
68-
done()
69-
})
55+
return expect(acl.checkAccess(graph, user, mode, resource, accessType, aclUrl))
56+
.to.be.rejectedWith('Error thrown during checkAccess()')
7057
})
7158
})

0 commit comments

Comments
 (0)