Skip to content

Commit

Permalink
fix: Newed up police is used instead of the constructor function
Browse files Browse the repository at this point in the history
  • Loading branch information
mblarsen committed Oct 20, 2017
1 parent af8a1b4 commit ed990b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Acl {
policy(policy, subject) {
const policy_ = typeof policy === 'function' ? new policy() : policy
const subjectName = this.subjectMapper(subject)
this.policies.set(subjectName, policy)
this.policies.set(subjectName, policy_)
return this
}

Expand Down
21 changes: 21 additions & 0 deletions test/Acl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ describe('Multiple', () => {
rotten.rotten = true
expect(user.can.some('eat', [fine, rotten])).toBe(true)
})

test('User can eat some apples', () => {
const acl = new Acl()
acl.mixin(User)
acl.rule('eat', Apple, (_, a) => !Boolean(a.rotten))
const user = new User()
const fine = new Apple()
const rotten = new Apple()
rotten.rotten = true
expect(user.can.every('eat', [fine, rotten])).toBe(false)
expect(user.can.every('eat', [fine, new Job()])).toBe(false)
})
})

describe('Strict mode', () => {
Expand Down Expand Up @@ -211,4 +223,13 @@ describe('More complex cases', () => {
expect(owner.can('view', job)).toBe(true)
expect(coworker.can('view', job)).toBe(true)
})
test('Policy newed', () => {
const acl = new Acl()
function JobPolicy() {
this.view = true
}
acl.policy(JobPolicy, Job)
expect(acl.policies.get('Job')).toBeInstanceOf(JobPolicy)
expect(acl.can({}, 'view', new Job())).toBe(true)
})
})

0 comments on commit ed990b4

Please sign in to comment.