-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for ValidatorEnforcer
- Loading branch information
1 parent
d6708c3
commit df65e96
Showing
1 changed file
with
79 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ValidatorEnforcer } from '../src/validatorEnforcer'; | ||
|
||
describe('ValidatorEnforcer', () => { | ||
describe('validateMatcher', () => { | ||
it('should not throw error for valid matcher', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.sub == p.sub && r.obj == p.obj && r.act == p.act'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should throw error for invalid properties', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.invalid == p.sub'); | ||
}).toThrow('Invalid properties: r.invalid'); | ||
}); | ||
|
||
it('should throw error for extra dots', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r..sub == p.sub'); | ||
}).toThrow('Found extra dots'); | ||
}); | ||
|
||
it('should throw error for unnecessary comma', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.sub == p.sub,'); | ||
}).toThrow('Unnecessary comma'); | ||
}); | ||
|
||
it('should throw error for mismatched parentheses', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('(r.sub == p.sub'); | ||
}).toThrow('Mismatched parentheses'); | ||
}); | ||
|
||
it('should throw error for invalid operators', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.sub & p.sub'); | ||
}).toThrow('Invalid operator in matcher'); | ||
}); | ||
}); | ||
|
||
describe('validatePolicyPriority', () => { | ||
it('should not throw error for same priority', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validatePolicyPriority(['alice', 'data1', 'read', '1'], ['bob', 'data2', 'write', '1'], 3); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should throw error for different priority', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validatePolicyPriority(['alice', 'data1', 'read', '1'], ['bob', 'data2', 'write', '2'], 3); | ||
}).toThrow('new rule should have the same priority with old rule.'); | ||
}); | ||
}); | ||
|
||
describe('validateRequiredSections', () => { | ||
it('should not throw error when all required sections are present', () => { | ||
const model = new Map([ | ||
['r', new Map()], | ||
['p', new Map()], | ||
['e', new Map()], | ||
['m', new Map()], | ||
]); | ||
expect(() => { | ||
ValidatorEnforcer.validateRequiredSections(model); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should throw error when required sections are missing', () => { | ||
const model = new Map([ | ||
['r', new Map()], | ||
['p', new Map()], | ||
]); | ||
expect(() => { | ||
ValidatorEnforcer.validateRequiredSections(model); | ||
}).toThrow('missing required sections: policy_effect,matchers'); | ||
}); | ||
}); | ||
}); |