-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for policy builder (#107)
- Loading branch information
Showing
4 changed files
with
185 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { EntityType, ValueOperators } from '@narval/authz-shared' | ||
import { | ||
ApprovalsCriterion, | ||
Criterion, | ||
ERC1155TransfersCriterion, | ||
IntentAmountCriterion, | ||
NonceRequiredCriterion, | ||
Policy, | ||
Then, | ||
WalletAddressCriterion | ||
} from '../../types/policy.type' | ||
import { criterionToString, reasonToString } from '../../utils/opa.utils' | ||
|
||
describe('criterionToString', () => { | ||
it('returns criterion if args are null', () => { | ||
const item: NonceRequiredCriterion = { | ||
criterion: Criterion.CHECK_NONCE_EXISTS, | ||
args: null | ||
} | ||
expect(criterionToString(item)).toEqual(Criterion.CHECK_NONCE_EXISTS) | ||
}) | ||
|
||
it('returns criterion if args is an array of strings', () => { | ||
const item: WalletAddressCriterion = { | ||
criterion: Criterion.CHECK_WALLET_ADDRESS, | ||
args: ['0x123', '0x456'] | ||
} | ||
expect(criterionToString(item)).toEqual(`${Criterion.CHECK_WALLET_ADDRESS}({"0x123", "0x456"})`) | ||
}) | ||
|
||
it('returns criterion if args is an array of objects', () => { | ||
const item: ERC1155TransfersCriterion = { | ||
criterion: Criterion.CHECK_ERC1155_TRANSFERS, | ||
args: [{ tokenId: 'eip155:137/erc1155:0x12345/123', operator: ValueOperators.LESS_THAN_OR_EQUAL, value: '5' }] | ||
} | ||
expect(criterionToString(item)).toEqual( | ||
`${Criterion.CHECK_ERC1155_TRANSFERS}([${item.args.map((el) => JSON.stringify(el)).join(', ')}])` | ||
) | ||
}) | ||
|
||
it('returns criterion if args is an object', () => { | ||
const item: IntentAmountCriterion = { | ||
criterion: Criterion.CHECK_INTENT_AMOUNT, | ||
args: { | ||
currency: '*', | ||
operator: ValueOperators.LESS_THAN_OR_EQUAL, | ||
value: '1000000000000000000' | ||
} | ||
} | ||
expect(criterionToString(item)).toEqual(`${Criterion.CHECK_INTENT_AMOUNT}(${JSON.stringify(item.args)})`) | ||
}) | ||
|
||
it('returns approvals criterion', () => { | ||
const item: ApprovalsCriterion = { | ||
criterion: Criterion.CHECK_APPROVALS, | ||
args: [ | ||
{ | ||
approvalCount: 2, | ||
countPrincipal: false, | ||
approvalEntityType: EntityType.User, | ||
entityIds: ['aa@narval.xyz'] | ||
} | ||
] | ||
} | ||
expect(criterionToString(item)).toEqual( | ||
`approvals = ${Criterion.CHECK_APPROVALS}([${item.args.map((el) => JSON.stringify(el)).join(', ')}])` | ||
) | ||
}) | ||
}) | ||
|
||
describe('reasonToString', () => { | ||
it('returns reason for PERMIT rules', () => { | ||
const item: Policy = { | ||
then: Then.PERMIT, | ||
name: 'policyId', | ||
when: [] | ||
} | ||
expect(reasonToString(item)).toBe( | ||
'reason = {"type":"permit","policyId":"policyId","approvalsSatisfied":approvals.approvalsSatisfied,"approvalsMissing":approvals.approvalsMissing}' | ||
) | ||
}) | ||
|
||
it('returns reason for FORBID rules', () => { | ||
const item: Policy = { | ||
then: Then.FORBID, | ||
name: 'policyId', | ||
when: [] | ||
} | ||
expect(reasonToString(item)).toBe( | ||
'reason = {"type":"forbid","policyId":"policyId","approvalsSatisfied":[],"approvalsMissing":[]}' | ||
) | ||
}) | ||
}) |
Oops, something went wrong.